Giter Site home page Giter Site logo

typescript-collections's Introduction

Build Status Libraries.io dependency status for latest release Gitpod ready-to-code code style: prettier npm NPM

It is a complete, fully tested data structure library written in TypeScript.

This project uses TypeScript Generics so you need TS 0.9 and above.

Included data structures

  • Linked List
  • Dictionary - Example
  • Multi Dictionary
  • Linked Dictionary
  • Default Dictionary - Info
  • Binary Search Tree
  • Binary Search Tree for Key-Value pairs
  • Stack
  • Queue
  • Set - Example
  • Bag
  • Binary Heap
  • Priority Queue

It also includes several functions for manipulating arrays.

Usage

npm install @buzz-dee/typescript-collections --save

ES6 import ... from

import * as Collections from '@buzz-dee/typescript-collections';

Visual Studio Code or other TypeScript IDE, will provide you with complete Intellisense (autocomplete) for your types. The compiler will ensure that the collections contain the correct elements.

Example

import * as Collections from '@buzz-dee/typescript-collections';

var mySet = new Collections.Set<number>();
mySet.add(123);
mySet.add(123); // Duplicates not allowed in a set
// The following will give error due to wrong type:
// mySet.add("asdf"); // Can only add numbers since that is the type argument.

var myQueue = new Collections.Queue();
myQueue.enqueue(1);
myQueue.enqueue(2);

console.log(myQueue.dequeue()); // prints 1
console.log(myQueue.dequeue()); // prints 2

Typings resolution

Remember to set "moduleResolution": "node", so TypeScript compiler can resolve typings in the node_modules/@buzz-dee/typescript-collections directory.

A note on Equality

Equality is important for hashing (e.g. dictionary / sets). Javascript only allows strings to be keys for the base dictionary {}. This is why the implementation for these data structures uses the item's toString() method.

makeString utility function (aka. JSON.stringify)

A simple function is provided for you when you need a quick toString that uses all properties. E.g:

import * as Collections from '@buzz-dee/typescript-collections';

class Car {
  constructor(
    public company: string,
    public type: string,
    public year: number
  ) {}
  toString() {
    // Short hand. Adds each own property
    return Collections.util.makeString(this);
  }
}

console.log(new Car('BMW', 'A', 2016).toString());

Output:

{company:BMW,type:A,year:2016}

A Sample on Dictionary

import * as Collections from '@buzz-dee/typescript-collections';

class Person {
  constructor(
    public name: string,
    public yearOfBirth: number,
    public city?: string
  ) {}
  toString() {
    return this.name + '-' + this.yearOfBirth; // City is not a part of the key.
  }
}

class Car {
  constructor(
    public company: string,
    public type: string,
    public year: number
  ) {}
  toString() {
    // Short hand. Adds each own property
    return Collections.util.makeString(this);
  }
}
var dict = new Collections.Dictionary<Person, Car>();
dict.setValue(
  new Person('john', 1970, 'melbourne'),
  new Car('honda', 'city', 2002)
);
dict.setValue(new Person('gavin', 1984), new Car('ferrari', 'F50', 2006));
console.log('Orig');
console.log(dict);

// Changes the same john, since city is not part of key
dict.setValue(
  new Person('john', 1970, 'sydney'),
  new Car('honda', 'accord', 2006)
);
// Add a new john
dict.setValue(new Person('john', 1971), new Car('nissan', 'micra', 2010));
console.log('Updated');
console.log(dict);

// Showing getting / setting a single car:
console.log('Single Item');
var person = new Person('john', 1970);
console.log('-Person:');
console.log(person);

var car = dict.getValue(person);
console.log('-Car:');
console.log(car.toString());

Output:

Orig
{
    john-1970 : {company:honda,type:city,year:2002}
    gavin-1984 : {company:ferrari,type:F50,year:2006}
}
Updated
{
    john-1970 : {company:honda,type:accord,year:2006}
    gavin-1984 : {company:ferrari,type:F50,year:2006}
    john-1971 : {company:nissan,type:micra,year:2010}
}
Single Item
-Person:
john-1970
-Car:
{company:honda,type:accord,year:2006}

Default Dictionary

Also known as Factory Dictionary [ref.]

If a key doesn't exist, the Default Dictionary automatically creates it with setDefault(defaultValue).

Default Dictionary is a @michaelneu contribution which copies Python's defaultDict.

Development and contributions

Compile, test and check coverage npm run all

Supported platforms

  • Every desktop and mobile browser (including IE6)
  • Node.js
If it supports JavaScript, it probably supports this library.

Contact

bas AT basarat.com

Project is based on the excellent original javascript version called buckets

typescript-collections's People

Contributors

adhulipa avatar basarat avatar buzz-dee avatar byxor avatar caselit avatar chriseppstein avatar cmandlbaur avatar cspotcode avatar dependabot[bot] avatar episage avatar glegoux avatar hmil avatar jawilkins avatar jimeh87 avatar josephliccini avatar michaelneu avatar murat-mehmet avatar outlandnish avatar phiresky avatar sroucheray avatar vladad avatar xstoudi avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.