A generically typed set of collections for use with TypeScript
A generically typed set of collections for use with TypeScript
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.
It also includes several functions for manipulating arrays.
npm install typescript-collections --save
ES6 import ... from
import * as Collections from 'typescript-collections';or TypeScript import ... require
import Collections = require('typescript-collections');or JavaScript var ... require
var Collections = require('typescript-collections');Visual Studio 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.
A sample Visual Studio project is in the demo folder.
Also available on NuGet : Thanks to
import * as Collections from 'typescript-collections';
var mySet = new Collections.Set();
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 2Remember to set "moduleResolution": "node", so TypeScript compiler can resolve typings in the node_modules/typescript-collections directory.
You should include umd.js or umd.min.js from dist/lib/ directory.
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.
A simple function is provided for you when you need a quick toString that uses all properties. E.g:
import * as Collections from '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}…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}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.
Compile, test and check coverage
npm run all
If it supports JavaScript, it probably supports this library.bas AT basarat.com
Project is based on the excellent original javascript version called buckets
No open issues yet, or sync has not completed.