easily building data structures that are serializable, validatable, and fully typed.

Overview

@davecode/structures [beta/wip]

Structures is a TypeScript library for easily building data structure classes that are

  • Serializable: Can convert rich objects to JSON objects or Uint8Arrays.
  • Validatable: All structures have built-in validation which can include custom validations.
  • Fully Typed: Custom structures' properties are fully typed, and even nullable ones are maked optional.

i'm still exploring applications of this library. it was originally intended to structure my database tables, but i stopped that after implementing "partial" structures and the fact it performs a validation every time a property is changed. i use prisma's generated types for my database structures, though the idea of having a defined class you can add arbitrary methods on such as .toURL() is still extremely interesting to me, and i'll figure something out eventually.

yarn add @davecode/structures

Usage

Let's take a look at defining a custom structure.

import { Structure, types } from '@davecode/structures';

const Person = new Structure('Person')
  .prop('name', types.String)
  .prop('age', types.Number)
  .prop('occupation', types.String.nullable)
  .method('sayHello', function () {
    console.log(`${this.name} says Hello.`);
  })
  .create();

// For TypeScript users
type Person = InstanceType<typeof Person>;

// Creating a structure
const dave = new Person({
  // name and age are REQUIRED here (will cause compile and runtime error)
  // however occupation is OPTIONAL here, since it is nullable
  name: 'dave caruso',
  age: 17,
});

Person, types.String, and many others are instances of DataType<T>, which provide serialization and validation.

Person.validate(person); // true

const json = Person.toJSON(person);
const person2 = Person.fromJSON(json);

types.ArrayOf(types.String).validate(['hello']); // true
types.ArrayOf(types.String).validate([124]); // false

See the [nonexistant docs] for more information on what is available.

You might also like...

A collection of all the data structures implemented in javascript code

Data structures A collection of all the data structures implemented in javascript code, interview questions & problems ( with solutions ) Contributors

May 1, 2022

Code accompanying my illustrated Data structures video series on YouTube

Code accompanying my illustrated Data structures video series on YouTube

Dec 10, 2022

Serialization library for data-oriented design structures in JavaScript

Data-oriented Serialization for SoA/AoA A zero-dependency serialization library for data-oriented design structures like SoA (Structure of Arrays) and

Sep 27, 2022

Data Structures

Data Structures

Data-Structures Data structures implementaion using Javascript 🧑‍💻 Data-Structures is a Github repository that contains the implementaion of most of

Sep 14, 2022

The basics data structures implemented with javascript.

The basics data structures implemented with javascript.

Estrutura de Dados com Javascript Tudo que está escrito e codificado aqui dentro desse repositório foi retirado do livro Estrutura de dados e algoritm

Oct 18, 2022

Backgrid.js is a set of components for building semantic and easily stylable data grid widgets

Backgrid.js is a set of components for building semantic and easily stylable data grid widgets. It offers a simple, intuitive programming interface that makes easy things easy, but hard things possible when dealing with tabular data.

Nov 21, 2022

A zero-dependency, strongly-typed web framework for Bun, Node and Cloudflare workers

nbit A simple, declarative, type-safe way to build web services and REST APIs for Bun, Node and Cloudflare Workers. Examples See some quick examples b

Sep 16, 2022

Demonstration of how you build full-stack, typed, NPM packages, the right way

NPM Packages - The Right way This repository aims to demonstrate how to build NPM packages the right way. my-package should import the shared code, bu

Nov 27, 2022

Typed HyperText Markup Language

Typed HyperText Markup Language You can add types to the HyperText Markup Language. Types To declare two types, they must be separated by a period and

Jan 22, 2022
Owner
Dave Caruso
Dave Caruso
📦 Fully typed and immutable store made on top of Immer with mutation, action, subscription and validation!

Riux is a fully typed and immutable store made on top of Immer with mutation, action, subscription and validation! Table of contents ?? Installation U

null 10 Aug 27, 2022
A fully-typed, low-level, and HyperScript-like Frontend Library 🚀

A fully-typed, low-level, and HyperScript-like Frontend Library ??

Eliaz Bobadilla 5 Apr 4, 2022
Lazy minting of ERC721 NFTs using EIP712 standard for typed, structured data. ✨

ERC721 - Signature minting Lazy minting of ERC721 NFTs using EIP712 standard for typed, structured data. ✨ How it works Lazy minting or Signature mint

Sunrit Jana 21 Oct 20, 2022
Data structures & algorithms implementations and coding problem solutions. Written in Typescript and tested with Jest. Coding problems are pulled from LeetCode and Daily Coding Problem.

technical-interview-prep Data structures & algorithms implementations and coding problem solutions. Written in Typescript and tested with Jest. Coding

Lesley Chang 7 Aug 5, 2022
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

?? Algorithms and data structures implemented in JavaScript with explanations and links to further readings

Oleksii Trekhleb 157.8k Dec 29, 2022
🥞Data Structures and Algorithms explained and implemented in JavaScript + eBook

Data Structures and Algorithms in JavaScript This is the coding implementations of the DSA.js book and the repo for the NPM package. In this repositor

Adrian Mejia 7k Jan 4, 2023
Bringing an all Open-Source Platform to study Data Structures and Algorithms ⚡

NeoAlgo-Docs Bringing an all Open-Source Platform to study Data Structures and Algorithms ⚡ ?? Installation You will need to have NodeJS and Yarn inst

Tesseract Coding 24 Jun 2, 2022
A Typescript companion to the book A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow

This repository aims to be a companion to the book A Common-Sense Guide to Data Structures and Algorithms by Jay Wengrow. I rewrote most of the data s

Alexandre Lim 29 Dec 3, 2022
Ace your next Javascript coding interview by mastering data structures and algorithms.

The Coding Interview: Algorithms + Data Structures Ace your next Javascript coding interview by mastering data structures and algorithms. Problem 1: S

Wallflower 5 Sep 19, 2022
Algorithms and Data Structures implemented in TypeScript for beginners, following best practices.

The Algorithms - TypeScript TypeScript Repository of TheAlgorithms, which implements various algorithms and data structures in TypeScript. These imple

The Algorithms 166 Dec 31, 2022