1 | [TypeScript Collections](https://github.com/basarat/typescript-collections/)
|
2 | ====================
|
3 |
|
4 | It is a complete, fully tested data structure library written in TypeScript.
|
5 |
|
6 | This 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 |
|
12 | Included 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 |
|
28 | It also includes several functions for manipulating arrays.
|
29 |
|
30 | Usage
|
31 | --------------------
|
32 |
|
33 | `npm install typescript-collections --save`
|
34 |
|
35 | ES6 `import ... from`
|
36 |
|
37 | ```typescript
|
38 | import * as Collections from 'typescript-collections';
|
39 | ```
|
40 |
|
41 | or TypeScript `import ... require`
|
42 |
|
43 | ```typescript
|
44 | import Collections = require('typescript-collections');
|
45 | ```
|
46 |
|
47 | or JavaScript `var ... require`
|
48 |
|
49 | ```js
|
50 | var Collections = require('typescript-collections');
|
51 | ```
|
52 |
|
53 | ![](https://zippy.gfycat.com/SeriousPointlessCob.gif)
|
54 |
|
55 | Visual Studio or other TypeScript IDE, will provide you with complete Intellisense (autocomplete) for your types.
|
56 | The compiler will ensure that the collections contain the correct elements.
|
57 |
|
58 | A sample Visual Studio project is in the demo folder.
|
59 |
|
60 | Also available on NuGet : <http://www.nuget.org/packages/typescript.collections/>
|
61 | Thanks to <https://github.com/georgiosd>
|
62 |
|
63 | Example
|
64 | --------------------
|
65 |
|
66 | ```typescript
|
67 | import * as Collections from 'typescript-collections';
|
68 |
|
69 | var mySet = new Collections.Set<number>();
|
70 | mySet.add(123);
|
71 | mySet.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 |
|
75 | var myQueue = new Collections.Queue();
|
76 | myQueue.enqueue(1);
|
77 | myQueue.enqueue(2);
|
78 |
|
79 | console.log(myQueue.dequeue()); // prints 1
|
80 | console.log(myQueue.dequeue()); // prints 2
|
81 | ```
|
82 |
|
83 | Typings resolution
|
84 | -------------------
|
85 |
|
86 | Remember to set `"moduleResolution": "node"`, so TypeScript compiler can resolve typings in the `node_modules/typescript-collections` directory.
|
87 |
|
88 | In browser usage
|
89 | -------------------
|
90 |
|
91 | You 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 |
|
97 | A note on Equality
|
98 | -------------------
|
99 |
|
100 | Equality is important for hashing (e.g. dictionary / sets). Javascript only allows strings to be keys for the base dictionary {}.
|
101 | This is why the implementation for these data structures uses the item's toString() method.
|
102 |
|
103 | makeString utility function (aka. JSON.stringify)
|
104 | -------------------
|
105 |
|
106 | A simple function is provided for you when you need a quick toString that uses all properties. E.g:
|
107 |
|
108 | ```typescript
|
109 | import * as Collections from 'typescript-collections';
|
110 |
|
111 | class 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 |
|
120 | console.log(new Car("BMW", "A", 2016).toString());
|
121 | ```
|
122 |
|
123 | Output:
|
124 |
|
125 | ```text
|
126 | {company:BMW,type:A,year:2016}
|
127 | ```
|
128 |
|
129 | A Sample on Dictionary
|
130 | ---------------------
|
131 |
|
132 | ```typescript
|
133 | import * as Collections from 'typescript-collections';
|
134 |
|
135 | class 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 |
|
143 | class 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 | }
|
151 | var dict = new Collections.Dictionary<Person, Car>();
|
152 | dict.setValue(new Person("john", 1970,"melbourne"), new Car("honda", "city", 2002));
|
153 | dict.setValue(new Person("gavin", 1984), new Car("ferrari", "F50", 2006));
|
154 | console.log("Orig");
|
155 | console.log(dict);
|
156 |
|
157 | // Changes the same john, since city is not part of key
|
158 | dict.setValue(new Person("john", 1970, "sydney"), new Car("honda", "accord", 2006));
|
159 | // Add a new john
|
160 | dict.setValue(new Person("john", 1971), new Car("nissan", "micra", 2010));
|
161 | console.log("Updated");
|
162 | console.log(dict);
|
163 |
|
164 | // Showing getting / setting a single car:
|
165 | console.log("Single Item");
|
166 | var person = new Person("john", 1970);
|
167 | console.log("-Person:");
|
168 | console.log(person);
|
169 |
|
170 | var car = dict.getValue(person);
|
171 | console.log("-Car:");
|
172 | console.log(car.toString());
|
173 | ```
|
174 |
|
175 | Output:
|
176 |
|
177 | ```text
|
178 | Orig
|
179 | {
|
180 | john-1970 : {company:honda,type:city,year:2002}
|
181 | gavin-1984 : {company:ferrari,type:F50,year:2006}
|
182 | }
|
183 | Updated
|
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 | }
|
189 | Single Item
|
190 | -Person:
|
191 | john-1970
|
192 | -Car:
|
193 | {company:honda,type:accord,year:2006}
|
194 | ```
|
195 |
|
196 | Default Dictionary
|
197 | ---------------------
|
198 |
|
199 | Also known as `Factory Dictionary` [[ref.](https://github.com/basarat/typescript-collections/pull/47)]
|
200 |
|
201 | If a key doesn't exist, the Default Dictionary automatically creates it with `setDefault(defaultValue)`.
|
202 |
|
203 | Default Dictionary is a @michaelneu contribution which copies Python's [defaultDict](https://docs.python.org/2/library/collections.html#collections.defaultdict).
|
204 |
|
205 | Development and contributions
|
206 | --------------------
|
207 |
|
208 | Compile, test and check coverage
|
209 | `npm run all`
|
210 |
|
211 | Supported platforms
|
212 | --------------------
|
213 |
|
214 | - Every desktop and mobile browser (including IE6)
|
215 | - Node.js
|
216 |
|
217 | ```text
|
218 | If it supports JavaScript, it probably supports this library.
|
219 | ```
|
220 |
|
221 | Contact
|
222 | --------------------
|
223 |
|
224 | bas AT basarat.com
|
225 |
|
226 | Project is based on the excellent original javascript version called [buckets](https://github.com/mauriciosantos/buckets)
|