TS 中文文档 TS 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 入门教程

    • TypeScript 手册
    • 基础知识
    • 日常类型
    • 类型缩小
    • 更多关于函数
    • 对象类型
    • 从类型创建类型
    • 泛型
    • Keyof 类型运算符
    • Typeof 类型运算符
    • 索引访问类型
    • 条件类型
    • 映射类型
    • 模板字面类型
    • 类
    • 模块
  • 参考手册

  • 项目配置

TypeScript Collections


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.

This projects supports UMD (Universal Module Definition)

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 typescript-collections --save

ES6 import ... from

  1. ``` ts
  2. import * as Collections from 'typescript-collections';
  3. ```

or TypeScript import ... require

  1. ``` ts
  2. import Collections = require('typescript-collections');
  3. ```

or JavaScript var ... require

  1. ``` js
  2. var Collections = require('typescript-collections');
  3. ```

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 : http://www.nuget.org/packages/typescript.collections/ Thanks to https://github.com/georgiosd

Example


  1. ``` ts
  2. import * as Collections from 'typescript-collections';

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

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

  11. console.log(myQueue.dequeue()); // prints 1
  12. console.log(myQueue.dequeue()); // prints 2
  13. ```

Typings resolution


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

In browser usage


You should include umd.js or umd.min.js from dist/lib/ directory.

  1. ``` html
  2. <script src="[server public path]/typescript-collections/dist/lib/umd.min.js"></script>
  3. ```

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:

  1. ``` ts
  2. import * as Collections from 'typescript-collections';

  3. class Car {
  4.     constructor(public company: string, public type: string, public year: number) {
  5.     }
  6.     toString() {
  7.         // Short hand. Adds each own property
  8.         return Collections.util.makeString(this);
  9.     }
  10. }

  11. console.log(new Car("BMW", "A", 2016).toString());
  12. ```

Output:

  1. ``` sh
  2. {company:BMW,type:A,year:2016}

  3. ```

A Sample on Dictionary


  1. ``` ts
  2. import * as Collections from 'typescript-collections';

  3. class Person {
  4.     constructor(public name: string, public yearOfBirth: number,public city?:string) {
  5.     }
  6.     toString() {
  7.         return this.name + "-" + this.yearOfBirth; // City is not a part of the key.
  8.     }
  9. }

  10. class Car {
  11.     constructor(public company: string, public type: string, public year: number) {
  12.     }
  13.     toString() {
  14.         // Short hand. Adds each own property
  15.         return Collections.util.makeString(this);
  16.     }
  17. }
  18. var dict = new Collections.Dictionary<Person, Car>();
  19. dict.setValue(new Person("john", 1970,"melbourne"), new Car("honda", "city", 2002));
  20. dict.setValue(new Person("gavin", 1984), new Car("ferrari", "F50", 2006));
  21. console.log("Orig");
  22. console.log(dict);

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

  29. // Showing getting / setting a single car:
  30. console.log("Single Item");
  31. var person = new Person("john", 1970);
  32. console.log("-Person:");
  33. console.log(person);

  34. var car = dict.getValue(person);
  35. console.log("-Car:");
  36. console.log(car.toString());
  37. ```

Output:

  1. ``` sh
  2. Orig
  3. {
  4.     john-1970 : {company:honda,type:city,year:2002}
  5.     gavin-1984 : {company:ferrari,type:F50,year:2006}
  6. }
  7. Updated
  8. {
  9.     john-1970 : {company:honda,type:accord,year:2006}
  10.     gavin-1984 : {company:ferrari,type:F50,year:2006}
  11.     john-1971 : {company:nissan,type:micra,year:2010}
  12. }
  13. Single Item
  14. -Person:
  15. john-1970
  16. -Car:
  17. {company:honda,type:accord,year:2006}

  18. ```

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

  1. ``` sh
  2. If it supports JavaScript, it probably supports this library.

  3. ```

Contact


bas AT basarat.com

Project is based on the excellent original javascript version called buckets
Last Updated: 2023-09-03 17:10:52