{"id":13580792,"url":"https://github.com/basarat/typescript-collections","last_synced_at":"2025-04-12T21:18:55.914Z","repository":{"id":8384618,"uuid":"9958747","full_name":"basarat/typescript-collections","owner":"basarat","description":"A generically typed set of collections for use with TypeScript","archived":false,"fork":false,"pushed_at":"2023-01-12T08:48:56.000Z","size":3528,"stargazers_count":1214,"open_issues_count":12,"forks_count":159,"subscribers_count":44,"default_branch":"release","last_synced_at":"2025-04-09T23:38:22.719Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/basarat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-05-09T12:27:25.000Z","updated_at":"2025-03-26T03:06:52.000Z","dependencies_parsed_at":"2023-01-14T12:00:38.651Z","dependency_job_id":null,"html_url":"https://github.com/basarat/typescript-collections","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basarat%2Ftypescript-collections","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basarat%2Ftypescript-collections/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basarat%2Ftypescript-collections/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basarat%2Ftypescript-collections/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basarat","download_url":"https://codeload.github.com/basarat/typescript-collections/tar.gz/refs/heads/release","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248333608,"owners_count":21086199,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T15:01:55.124Z","updated_at":"2025-04-12T21:18:55.895Z","avatar_url":"https://github.com/basarat.png","language":"TypeScript","readme":"[TypeScript Collections](https://github.com/basarat/typescript-collections/)\n====================\n\nIt is a complete, fully tested data structure library written in TypeScript.\n\nThis project uses TypeScript Generics so you need TS 0.9 and above.\n\n[This projects supports UMD (Universal Module Definition)](https://github.com/umdjs/umd)\n\n[![NPM](https://nodei.co/npm-dl/typescript-collections.png?height=3)](https://nodei.co/npm/typescript-collections/)\n\nIncluded data structures\n---------------------\n\n- Linked List\n- Dictionary - [Example](#a-sample-on-dictionary)\n- Multi Dictionary\n- Linked Dictionary\n- Default Dictionary - [Info](#default-dictionary)\n- Binary Search Tree\n- Binary Search Tree for Key-Value pairs\n- Stack\n- Queue\n- Set - [Example](#example)\n- Bag\n- Binary Heap\n- Priority Queue\n\nIt also includes several functions for manipulating arrays.\n\nUsage\n--------------------\n\n`npm install typescript-collections --save`\n\nES6 `import ... from`\n\n```typescript\nimport * as Collections from 'typescript-collections';\n```\n\nor TypeScript `import ... require`\n\n```typescript\nimport Collections = require('typescript-collections');\n```\n\nor JavaScript `var ... require`\n\n```js\nvar Collections = require('typescript-collections');\n```\n\n![](https://zippy.gfycat.com/SeriousPointlessCob.gif)\n\nVisual Studio or other TypeScript IDE, will provide you with complete Intellisense (autocomplete) for your types.\nThe compiler will ensure that the collections contain the correct elements.\n\nA sample Visual Studio project is in the demo folder.\n\nAlso available on NuGet : \u003chttp://www.nuget.org/packages/typescript.collections/\u003e\nThanks to \u003chttps://github.com/georgiosd\u003e\n\nExample\n--------------------\n\n```typescript\nimport * as Collections from 'typescript-collections';\n\nvar mySet = new Collections.Set\u003cnumber\u003e();\nmySet.add(123);\nmySet.add(123); // Duplicates not allowed in a set\n// The following will give error due to wrong type:\n// mySet.add(\"asdf\"); // Can only add numbers since that is the type argument.\n\nvar myQueue = new Collections.Queue();\nmyQueue.enqueue(1);\nmyQueue.enqueue(2);\n\nconsole.log(myQueue.dequeue()); // prints 1\nconsole.log(myQueue.dequeue()); // prints 2\n```\n\nTypings resolution\n-------------------\n\nRemember to set `\"moduleResolution\": \"node\"`, so TypeScript compiler can resolve typings in the `node_modules/typescript-collections` directory.\n\nIn browser usage\n-------------------\n\nYou should include `umd.js` or `umd.min.js` from `dist/lib/` directory.\n\n```html\n\u003cscript src=\"[server public path]/typescript-collections/dist/lib/umd.min.js\"\u003e\u003c/script\u003e\n```\n\nA note on Equality\n-------------------\n\nEquality is important for hashing (e.g. dictionary / sets). Javascript only allows strings to be keys for the base dictionary {}.\nThis is why the implementation for these data structures uses the item's toString() method.\n\nmakeString utility function (aka. JSON.stringify)\n-------------------\n\nA simple function is provided for you when you need a quick toString that uses all properties. E.g:\n\n```typescript\nimport * as Collections from 'typescript-collections';\n\nclass Car {\n    constructor(public company: string, public type: string, public year: number) {\n    }\n    toString() {\n        // Short hand. Adds each own property\n        return Collections.util.makeString(this);\n    }\n}\n\nconsole.log(new Car(\"BMW\", \"A\", 2016).toString());\n```\n\nOutput:\n\n```text\n{company:BMW,type:A,year:2016}\n```\n\nA Sample on Dictionary\n---------------------\n\n```typescript\nimport * as Collections from 'typescript-collections';\n\nclass Person {\n    constructor(public name: string, public yearOfBirth: number,public city?:string) {\n    }\n    toString() {\n        return this.name + \"-\" + this.yearOfBirth; // City is not a part of the key.\n    }\n}\n\nclass Car {\n    constructor(public company: string, public type: string, public year: number) {\n    }\n    toString() {\n        // Short hand. Adds each own property\n        return Collections.util.makeString(this);\n    }\n}\nvar dict = new Collections.Dictionary\u003cPerson, Car\u003e();\ndict.setValue(new Person(\"john\", 1970,\"melbourne\"), new Car(\"honda\", \"city\", 2002));\ndict.setValue(new Person(\"gavin\", 1984), new Car(\"ferrari\", \"F50\", 2006));\nconsole.log(\"Orig\");\nconsole.log(dict);\n\n// Changes the same john, since city is not part of key\ndict.setValue(new Person(\"john\", 1970, \"sydney\"), new Car(\"honda\", \"accord\", 2006));\n// Add a new john\ndict.setValue(new Person(\"john\", 1971), new Car(\"nissan\", \"micra\", 2010));\nconsole.log(\"Updated\");\nconsole.log(dict);\n\n// Showing getting / setting a single car:\nconsole.log(\"Single Item\");\nvar person = new Person(\"john\", 1970);\nconsole.log(\"-Person:\");\nconsole.log(person);\n\nvar car = dict.getValue(person);\nconsole.log(\"-Car:\");\nconsole.log(car.toString());\n```\n\nOutput:\n\n```text\nOrig\n{\n    john-1970 : {company:honda,type:city,year:2002}\n    gavin-1984 : {company:ferrari,type:F50,year:2006}\n}\nUpdated\n{\n    john-1970 : {company:honda,type:accord,year:2006}\n    gavin-1984 : {company:ferrari,type:F50,year:2006}\n    john-1971 : {company:nissan,type:micra,year:2010}\n}\nSingle Item\n-Person:\njohn-1970\n-Car:\n{company:honda,type:accord,year:2006}\n```\n\nDefault Dictionary\n---------------------\n\nAlso known as `Factory Dictionary` [[ref.](https://github.com/basarat/typescript-collections/pull/47)]\n\nIf a key doesn't exist, the Default Dictionary automatically creates it with `setDefault(defaultValue)`.\n\nDefault Dictionary is a @michaelneu contribution which copies Python's [defaultDict](https://docs.python.org/2/library/collections.html#collections.defaultdict).\n\nDevelopment and contributions\n--------------------\n\nCompile, test and check coverage\n`npm run all`\n\nSupported platforms\n--------------------\n\n- Every desktop and mobile browser (including IE6)\n- Node.js\n\n```text\nIf it supports JavaScript, it probably supports this library.\n```\n\nContact\n--------------------\n\nbas AT basarat.com\n\nProject is based on the excellent original javascript version called [buckets](https://github.com/mauriciosantos/buckets)\n","funding_links":[],"categories":["TypeScript","Table of Contents","TypeScript 工具/库/框架"],"sub_categories":["数据结构"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasarat%2Ftypescript-collections","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasarat%2Ftypescript-collections","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasarat%2Ftypescript-collections/lists"}