UNPKG

5.96 kBMarkdownView Raw
1[TypeScript Collections](https://github.com/basarat/typescript-collections/)
2====================
3
4It is a complete, fully tested data structure library written in TypeScript.
5
6This project uses TypeScript Generics so you need TS 0.9 and above.
7
8[This projects supports UMD (Universal Module Definition)](https://github.com/umdjs/umd)
9
10[![NPM](https://nodei.co/npm-dl/typescript-collections.png?height=3)](https://nodei.co/npm/typescript-collections/)
11
12Included data structures
13---------------------
14
15- Linked List
16- Dictionary - [Example](#a-sample-on-dictionary)
17- Multi Dictionary
18- Linked Dictionary
19- Default Dictionary - [Info](#default-dictionary)
20- Binary Search Tree
21- Stack
22- Queue
23- Set - [Example](#example)
24- Bag
25- Binary Heap
26- Priority Queue
27
28It also includes several functions for manipulating arrays.
29
30Usage
31--------------------
32
33`npm install typescript-collections --save`
34
35ES6 `import ... from`
36
37```typescript
38import * as Collections from 'typescript-collections';
39```
40
41or TypeScript `import ... require`
42
43```typescript
44import Collections = require('typescript-collections');
45```
46
47or JavaScript `var ... require`
48
49```js
50var Collections = require('typescript-collections');
51```
52
53![](https://zippy.gfycat.com/SeriousPointlessCob.gif)
54
55Visual Studio or other TypeScript IDE, will provide you with complete Intellisense (autocomplete) for your types.
56The compiler will ensure that the collections contain the correct elements.
57
58A sample Visual Studio project is in the demo folder.
59
60Also available on NuGet : <http://www.nuget.org/packages/typescript.collections/>
61Thanks to <https://github.com/georgiosd>
62
63Example
64--------------------
65
66```typescript
67import * as Collections from 'typescript-collections';
68
69var mySet = new Collections.Set<number>();
70mySet.add(123);
71mySet.add(123); // Duplicates not allowed in a set
72// The following will give error due to wrong type:
73// mySet.add("asdf"); // Can only add numbers since that is the type argument.
74
75var myQueue = new Collections.Queue();
76myQueue.enqueue(1);
77myQueue.enqueue(2);
78
79console.log(myQueue.dequeue()); // prints 1
80console.log(myQueue.dequeue()); // prints 2
81```
82
83Typings resolution
84-------------------
85
86Remember to set `"moduleResolution": "node"`, so TypeScript compiler can resolve typings in the `node_modules/typescript-collections` directory.
87
88In browser usage
89-------------------
90
91You should include `umd.js` or `umd.min.js` from `dist/lib/` directory.
92
93```html
94<script src="[server public path]/typescript-collections/dist/lib/umd.min.js"></script>
95```
96
97A note on Equality
98-------------------
99
100Equality is important for hashing (e.g. dictionary / sets). Javascript only allows strings to be keys for the base dictionary {}.
101This is why the implementation for these data structures uses the item's toString() method.
102
103makeString utility function (aka. JSON.stringify)
104-------------------
105
106A simple function is provided for you when you need a quick toString that uses all properties. E.g:
107
108```typescript
109import * as Collections from 'typescript-collections';
110
111class Car {
112 constructor(public company: string, public type: string, public year: number) {
113 }
114 toString() {
115 // Short hand. Adds each own property
116 return Collections.util.makeString(this);
117 }
118}
119
120console.log(new Car("BMW", "A", 2016).toString());
121```
122
123Output:
124
125```text
126{company:BMW,type:A,year:2016}
127```
128
129A Sample on Dictionary
130---------------------
131
132```typescript
133import * as Collections from 'typescript-collections';
134
135class Person {
136 constructor(public name: string, public yearOfBirth: number,public city?:string) {
137 }
138 toString() {
139 return this.name + "-" + this.yearOfBirth; // City is not a part of the key.
140 }
141}
142
143class Car {
144 constructor(public company: string, public type: string, public year: number) {
145 }
146 toString() {
147 // Short hand. Adds each own property
148 return Collections.util.makeString(this);
149 }
150}
151var dict = new Collections.Dictionary<Person, Car>();
152dict.setValue(new Person("john", 1970,"melbourne"), new Car("honda", "city", 2002));
153dict.setValue(new Person("gavin", 1984), new Car("ferrari", "F50", 2006));
154console.log("Orig");
155console.log(dict);
156
157// Changes the same john, since city is not part of key
158dict.setValue(new Person("john", 1970, "sydney"), new Car("honda", "accord", 2006));
159// Add a new john
160dict.setValue(new Person("john", 1971), new Car("nissan", "micra", 2010));
161console.log("Updated");
162console.log(dict);
163
164// Showing getting / setting a single car:
165console.log("Single Item");
166var person = new Person("john", 1970);
167console.log("-Person:");
168console.log(person);
169
170var car = dict.getValue(person);
171console.log("-Car:");
172console.log(car.toString());
173```
174
175Output:
176
177```text
178Orig
179{
180 john-1970 : {company:honda,type:city,year:2002}
181 gavin-1984 : {company:ferrari,type:F50,year:2006}
182}
183Updated
184{
185 john-1970 : {company:honda,type:accord,year:2006}
186 gavin-1984 : {company:ferrari,type:F50,year:2006}
187 john-1971 : {company:nissan,type:micra,year:2010}
188}
189Single Item
190-Person:
191john-1970
192-Car:
193{company:honda,type:accord,year:2006}
194```
195
196Default Dictionary
197---------------------
198
199Also known as `Factory Dictionary` [[ref.](https://github.com/basarat/typescript-collections/pull/47)]
200
201If a key doesn't exist, the Default Dictionary automatically creates it with `setDefault(defaultValue)`.
202
203Default Dictionary is a @michaelneu contribution which copies Python's [defaultDict](https://docs.python.org/2/library/collections.html#collections.defaultdict).
204
205Development and contributions
206--------------------
207
208Compile, test and check coverage
209`npm run all`
210
211Supported platforms
212--------------------
213
214- Every desktop and mobile browser (including IE6)
215- Node.js
216
217```text
218If it supports JavaScript, it probably supports this library.
219```
220
221Contact
222--------------------
223
224bas AT basarat.com
225
226Project is based on the excellent original javascript version called [buckets](https://github.com/mauriciosantos/buckets)