• Stars
    star
    1,196
  • Rank 39,117 (Top 0.8 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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.

This projects supports UMD (Universal Module Definition)

NPM

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

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

Example

import * as Collections from '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/typescript-collections directory.

In browser usage

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

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

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 '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 '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

More Repositories

1

typescript-book

📚 The definitive guide to TypeScript and possibly the best TypeScript book 📖. Free and Open Source 🌹
TypeScript
19,831
star
2

algorithms

🌹 Code / tests for algorithm and data structure lessons using TypeScript / JavaScript
TypeScript
289
star
3

byots

Bring your own TypeScript with more internal definitions
TypeScript
267
star
4

typescript-script

Script tag support for TypeScript
JavaScript
214
star
5

typescript-react

Use TypeScript to develop react applications
TypeScript
185
star
6

typescript-for-professionals

TypeScript
151
star
7

ts-npm-module

Sample TypeScript npm module project
JavaScript
124
star
8

TypeScriptDeepDive

A presentation that takes a strong look into typescript
JavaScript
67
star
9

react-typescript

A demo based on https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html
JavaScript
61
star
10

ts-npm-module-consume

A sample project showing flawless consumption of a TypeScript NPM module using TypeScript
TypeScript
60
star
11

TypeScriptEditor

A typescript editor you can use in the browser
JavaScript
56
star
12

gls

General Layout System for React
MDX
55
star
13

video-angular-typescript

Accompanying repository for showing how you can quickly work on AngularJS with TypeScript when you use grunt-ts
TypeScript
42
star
14

coding-interview

Lessons for smashing the coding interview 🌹
TypeScript
41
star
15

takeme

A simple and effective routing solution
TypeScript
41
star
16

algorithms-book

The book a CS student can use to refresh their knowledge of Data Structures and Algorithms or just review how to do them in TypeScript
34
star
17

cypress-ts

A quick start for Cypress + TypeScript
JavaScript
30
star
18

awesome-enterprise-component-library

A list of public enterprise funded open source component libraries
26
star
19

mobx-typescript-react

A course on Effective MobX + TypeScript + React
TypeScript
26
star
20

typescript-compiler-docs

A work in progress documentation of how the TypeScript compiler works
22
star
21

typescript-amd

A project to demonstrated how to use Typescript with AMD
JavaScript
17
star
22

types-vs-interfaces

TypeScript
14
star
23

cypress-guide

A guide to maintainable web application testing with cypress
TypeScript
14
star
24

demo-tesseract

TypeScript
13
star
25

demo-angularjs-easeljs

TypeScript
12
star
26

monaco

An daily automated build of microsoft monaco
TypeScript
12
star
27

typescript-book-alpha

The beginners guide to TypeScript 🌹
11
star
28

chromes

A simple solution to run puppeteer automated tests 🌹
TypeScript
10
star
29

typescript-async

A course on TypeScript async await fundamentals
TypeScript
9
star
30

demo-project-references

JavaScript
9
star
31

demo-compiler

TypeScript
9
star
32

expandible

Making Expandibles / Collapsible / Accordian core easy 🌹
TypeScript
9
star
33

2019-holyjs

TypeScript
9
star
34

demo-vite

TypeScript
8
star
35

ChessClock

A game clock (also called chess clock) designed to work across all screen sizes (from mobile to ridiculous) using the latest web technologies ( TypeScript , Less and MVVM )
JavaScript
8
star
36

playwright-playbook

TypeScript
8
star
37

css-in-js-pref

TypeScript
7
star
38

demo-fullstack

A demo fullstack node.js application in TypeScript
JavaScript
7
star
39

demo-typestyle

TypeScript
6
star
40

tic-tac-toe

TypeScript
6
star
41

alertOnLeave

add an alert on leave check 🌹 ❤️
TypeScript
6
star
42

basarat.github.io

https://basarat.com
CSS
6
star
43

typescript-services

Open up TypeScript services for nodejs consumption
JavaScript
5
star
44

god-extra

Repository for issues and docs on God mode for VSCode
5
star
45

egghead-bootstrap-typescript-react

JavaScript
5
star
46

layoutjs

An abstraction over flexbox with semantic names for enhanced readability and maintainability
JavaScript
5
star
47

2019-02-react-melbourne

4
star
48

cypress-cra

TypeScript
4
star
49

tsc

DEPRECATED
JavaScript
4
star
50

todomvc-typescript

A demo project to show typescript + angularjs workflow
JavaScript
4
star
51

tsl

A library for TypeScript developers
JavaScript
4
star
52

uic

UI Components
TypeScript
3
star
53

conf-ndc-2017

TypeScript
3
star
54

youtube-typescript-in

TypeScript
3
star
55

starts

Simplify your live development workflow
TypeScript
3
star
56

demo-swr

TypeScript
3
star
57

demo-playwright-vrt

TypeScript
3
star
58

youtube-retry-async

Retry an async function
TypeScript
3
star
59

typescript-node

A sample of how to be effective with TypeScript and NPM / Node
TypeScript
3
star
60

demo-javascript-concurrency

Javascript / Promise / Async / Await / Concurrency
TypeScript
3
star
61

typescript-requirejs

A project to demonstrate the weakness of handling requirejs with current version of typescript
3
star
62

typescript-react-mobx

Because simple things can be simple 🌹
3
star
63

onresize

An optimized window.resize function
JavaScript
3
star
64

video-typescript-modules

Companion code for a video tutorial on the main ways TypeScript supports code organization. Both on the server and the client side.
JavaScript
3
star
65

demo-test-sentry

TypeScript
2
star
66

demo-react-native-assets

2
star
67

demo-jest

TypeScript
2
star
68

ddd-melb-2016

TypeScript
2
star
69

demo-state-management-playground

TypeScript
2
star
70

demo-valtio

TypeScript
2
star
71

sing

Youtube MP3 downloader
JavaScript
2
star
72

angular-for-react-devs

You know react? Great. Lets see how concepts map to angular 🌹
2
star
73

cab

Code Analysis Butler
TypeScript
2
star
74

cypress-workshop

TypeScript
2
star
75

demo-tailwind-space

TypeScript
2
star
76

eze

Eze demos / docs for Component / Styleguide authors 🌹
TypeScript
2
star
77

demo-playwright-fixtures

TypeScript
2
star
78

course-nextjs

TypeScript
2
star
79

react-native-for-professionals

TypeScript
2
star
80

video-angularjs-typescript-bootstrap

TypeScript
2
star
81

grunt-ts-demo

A demo for a video tutorial on grunt-ts https://github.com/basarat/grunt-ts
JavaScript
2
star
82

demo-gruntts-melbjs-sep2013

TypeScript
2
star
83

demo-trpc

TypeScript
2
star
84

one-minute-utilities

TypeScript
2
star
85

demo-conf-ndc-2022

TypeScript
2
star
86

screencast-angular-typescript

A demo project I use for demonstrating using angular with typescript
JavaScript
2
star
87

fuse-hmr

Carefully curated FuseBox HMR behaviours 🌹
TypeScript
2
star
88

demo-pages

TypeScript
2
star
89

demo-tailwind-grid

TypeScript
1
star
90

demo-typescript-for-javascript

JavaScript
1
star
91

demo-solid

TypeScript
1
star
92

demo-allowjs

JavaScript
1
star
93

video-angularjs-typescript-services

JavaScript
1
star
94

conf-angular-mini-2017

TypeScript
1
star
95

demo-testing-talks-2022

TypeScript
1
star
96

cypress-typescript-issues

A repo to show reproducible errors in the Cypress - TypeScript workflow 🌹
TypeScript
1
star
97

workshop-typescript

TypeScript
1
star
98

demo-typescript-opaque-types

TypeScript
1
star
99

ts-test

A repo I use for my typescript tests
JavaScript
1
star
100

demo-stateful

TypeScript
1
star