0.0.0-development.11 • Published 2 years ago

@stackomate/data-structures v0.0.0-development.11

Weekly downloads
1
License
MIT
Repository
github
Last release
2 years ago

Data-structures

styled with prettier Build Status

A collection of useful data-structures for Typescript Development.

Usage

  1. npm i @stackomate/data-structures
  2. Import desired data-structure into your project. See examples below:

Each data-structure contains a prefix to better differentiate similar methods

Composite Key Map (ckm prefix)

A map that allows multiple ordered keys (composite key) to be mapped to some value. Can contain an arbitrary number of keys. Keys are stored as pointers that contain maps to the child keys, in recursive tree structure. Important: Order of iteration is not preserved for the keys

Examples

See live example in Stackblitz: Demo

[root] => 10
[root, 'a'] => 20
[root, 'b', 'c'] => 30 (note that root.b is not a key) 

BijectiveMap

A stricter type of map that only allows Bijective (one-to-one) relationships.

Bijection Mapping Example

(Credit: Wikipedia)

  • Example: Create a mapping of usernames to their respective IDs (CodeSandbox)
import {BijectiveMap} from '@stackomate/data-structures';
const usernameID = new BijectiveMap<string, number>();
usernameID.set('Kyle', 1);
usernameID.set('Mary', 2);
usernameID.set('John', 1); // Error: 'Value has already been registered for another key.' 
  • BijectiveMaps allow for inversion:
let idUsername = usernameID.invert(); // 1 => 'Kyle', 2 => 'Mary'