{"id":15408916,"url":"https://github.com/sec-ant/trie-map","last_synced_at":"2026-03-16T14:06:25.715Z","repository":{"id":63127498,"uuid":"565429098","full_name":"Sec-ant/trie-map","owner":"Sec-ant","description":"TS/JS Map-like data structure backed by trie","archived":false,"fork":false,"pushed_at":"2024-04-22T03:00:59.000Z","size":33,"stargazers_count":3,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-23T00:38:38.447Z","etag":null,"topics":["data-structure","esm","iterable","map","trie","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@sec-ant/trie-map","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Sec-ant.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-13T11:41:18.000Z","updated_at":"2024-09-14T05:08:24.000Z","dependencies_parsed_at":"2024-04-22T04:22:37.651Z","dependency_job_id":"a3bfe237-30fe-4466-8f70-9c69cab3a8d7","html_url":"https://github.com/Sec-ant/trie-map","commit_stats":{"total_commits":45,"total_committers":2,"mean_commits":22.5,"dds":0.06666666666666665,"last_synced_commit":"d4744988c849bd79047d62006c3ac9c74073460a"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie-map","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie-map/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie-map/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie-map/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sec-ant","download_url":"https://codeload.github.com/Sec-ant/trie-map/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240384262,"owners_count":19792970,"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":["data-structure","esm","iterable","map","trie","typescript"],"created_at":"2024-10-01T16:35:58.280Z","updated_at":"2026-03-16T14:06:25.649Z","avatar_url":"https://github.com/Sec-ant.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @sec-ant/trie-map\n\n![GitHub top language](https://img.shields.io/github/languages/top/Sec-ant/trie-map) [![npm version](https://img.shields.io/npm/v/@sec-ant/trie-map)](https://www.npmjs.com/package/@sec-ant/trie-map) [![npm downloads](https://img.shields.io/npm/dm/@sec-ant/trie-map)](https://www.npmjs.com/package/@sec-ant/trie-map) [![](https://data.jsdelivr.com/v1/package/npm/@sec-ant/trie-map/badge?style=rounded)](https://www.jsdelivr.com/package/npm/@sec-ant/trie-map) ![GitHub search hit counter](https://img.shields.io/github/search/Sec-ant/trie-map/goto) [![Rate this package](https://badges.openbase.com/js/rating/@sec-ant/trie-map.svg?token=SBYugeYmOxDXIoCFLx5bHr1urYSXTjmWD51wO5PzyH0=)](https://openbase.com/js/@sec-ant/trie-map?utm_source=embedded\u0026amp;utm_medium=badge\u0026amp;utm_campaign=rate-badge)\n\nThis package impelements a Map-like data structure backed by [trie](https://en.wikipedia.org/wiki/Trie).\n\n## Why?\n\nIn the native [Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map) data structure provided by Javascript, key equality is based on the [SameValueZero](https://developer.mozilla.org/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality) algorithm. So, two different arrays with same contents are considered as two different keys:\n\n```js\nconst a = [\"hello\", \"world\"];\nconst b = [\"hello\", \"world\"];\n\nconst map = new Map();\n\nmap.set(a, \"foo\").set(b, \"bar\");\n\nmap.get(a); // =\u003e \"foo\"\nmap.get(b); // =\u003e \"bar\"\n\nmap.get([\"hello\", \"world\"]); // =\u003e undefined\n```\n\nHowever, in some use cases, we don't always preserve the references of the keys (and in some cases we just don't have them, e.g., when the map is imported from a third-party module) and we want arrays with same contents always point to the same value, e.g., in a file system:\n\n```js\nconst fs = new Map();\n\nfs.set([\"c\", \"program files\"], \"hello world.txt\");\n\nfs.get([\"c\", \"program files\"]); // =\u003e undefined, oops!\n```\n\nThis package treats iterable-reference-type (array or array-like object that implements the [iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)) keys as values and uses value-equality to check key equality. The underlying structure is a [trie](https://en.wikipedia.org/wiki/Trie).\n\n## Install\n\n```bash\nnpm i @sec-ant/trie-map\n```\n\nor\n\n```bash\nyarn add @sec-ant/trie-map\n```\n\n## Basic Usage\n\n```js\nimport { TrieMap } from \"@sec-ant/trie-map\";\n\nconst a = [\"hello\", \"world\"];\nconst b = [\"hello\", \"world\"];\n\nconst tmap = new TrieMap();\n\ntmap.set(a, \"foo\");\n\ntmap.get(a); // =\u003e \"foo\"\ntmap.get(b); // =\u003e \"foo\"\ntmap.get([\"hello\", \"world\"]); // =\u003e \"foo\"\n```\n\n## Features\n\nThis package is greatly inspired by and plays a similar role as [anko/array-keyed-map](https://github.com/anko/array-keyed-map). Other similar packages include [immutable.js Map](https://immutable-js.com/docs/v3.8.2/Map/#:~:text=Map%27s%20keys%20can%20be,used%20as%20a%20key.), [bemoje/trie-map](https://github.com/bemoje/trie-map), [isogon/tuple-map](https://github.com/isogon/tuple-map), etc.\n\nHowever, the following features make this package stand out:\n\n### Typescript Support\n\nThis packages is fully written in Typescript\n\n```ts\nimport { TrieMap } from \"@sec-ant/trie-map\";\n\nconst tmap = new TrieMap\u003cstring[], string\u003e();\n\ntmap.set([\"hello\", \"world\"], \"foo\");\n\n// value type will be inferred as string | undefined\nconst value = tmap.get([\"hello\", \"world\"]); // =\u003e \"foo\"\n```\n\n### Pure ECMAScript Module\n\n[ECMAScript modules (ESM)](https://developer.mozilla.org/docs/Web/JavaScript/Guide/Modules) is the now and future of javascript module system.\n\n### Fully Mimics All Native Map APIs\n\n\u003cdetails\u003e\n  \u003csummary\u003eMimic all methods and properties in \u003chref src=\"https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map#instance_properties\"\u003eMap\u003c/href\u003e\u003c/summary\u003e\n\n```ts\nimport { TrieMap } from \"@sec-ant/trie-map\";\n\n// construct, use [key, value] entries to init a TrieMap instance\nconst tmap = new TrieMap\u003cstring[], string\u003e([\n  [[\"hello\", \"world\"], \"foo\"],\n  [[\"hello\", \"TrieMap\"], \"bar\"],\n]);\n\n// set\ntmap.set([], \"empty\"); // =\u003e tmap\ntmap.set([\"\"], \"empty string\"); // =\u003e tmap\n\n// has\ntmap.has([\"hello\", \"world\"]); // =\u003e true\ntmap.has([\"hello\"]); // =\u003e false\n\n// get\ntmap.get([]); // =\u003e \"empty\"\ntmap.get([\"hello\"]); // =\u003e undefined\n\n// delete\ntmap.delete([]); // =\u003e true\ntmap.delete([\"hello\"]); // =\u003e false\n\n// size\ntmap.size; // =\u003e 3\n\n// entries\n[...tmap.entries()]; // =\u003e [[[\"hello\", \"world\"], \"foo\"], [[\"hello\", \"TrieMap\"], \"bar\"], [[\"\"], \"empty string\"]]\n\n// keys\n[...tmap.keys()]; // =\u003e [[\"hello\", \"world\"], [\"hello\", \"TrieMap\"], [\"\"]]\n\n// values\n[...tmap.values()]; // =\u003e [\"foo\", \"bar\", \"empty string\"]\n\n// forEach\ntmap.forEach((value, key) =\u003e console.log([key[0], value])); // =\u003e [[\"hello\", \"foo\"], [\"hello\", \"bar\"], [\"\", \"empty string\"]]\n\n// Symbol.iterator\n[...tmap]; // =\u003e same result as [...tmap.entries()]\n\n// Symbol.toStringTag\ntmap.toString(); // =\u003e [object TrieMap]\n\n// clear\ntmap.clear(); // =\u003e undefined, remove all key-value pairs\n```\n\n\u003c/details\u003e\n\n### Iterations in Insertion Order\n\nAll iterations (`entries`, `keys`, `values`, `forEach` and `Symbol.iterator`) are in insertion order.\n\n### Mixing of Primitives and Iterable References as Keys\n\n```ts\nimport { TrieMap } from \"@sec-ant/trie-map\";\n\nconst tmap = new TrieMap\u003cstring | number | string[], string\u003e();\n\ntmap.set(\"key\", \"string\").get(\"key\"); // =\u003e \"string\"\ntmap.set(2, \"number\").get(2); // =\u003e \"number\"\ntmap.set([\"string\"], \"array\").get([\"string\"]); // =\u003e \"array\"\n\n[...tmap]; // =\u003e [[\"key\", \"string\"], [2, \"number\"], [[\"string\"], \"array\"]]\n```\n\n### Value Comparison of Deeply Nested Iterable Keys\n\n```ts\nimport { TrieMap } from \"@sec-ant/trie-map\";\n\nconst tmap = new TrieMap\u003c(string | string[])[], string\u003e();\n\ntmap.set([], \"foo\").get([]); // =\u003e \"foo\"\ntmap.set([[]], \"bar\").get([[]]); // =\u003e \"bar\"\ntmap.set([[], []], \"baz\").get([[], []]); // =\u003e \"baz\"\ntmap.set([[\"1\"], [], \"2\", [\"3\"]], \"123\").get([[\"1\"], [], \"2\", [\"3\"]]); // =\u003e \"123\"\n```\n\n### Shallow Comparison of Keys\n\nThis package also provides an option to opt out from value comparison of **deeply** nested iterable keys, and only compares the keys **shallowly**:\n\n```ts\nimport { TrieMap, TrieMapOptions } from \"@sec-ant/trie-map\";\n\nconst options: TrieMapOptions = { deep: false }; // the default value of deep is true\n\nconst tmap = new TrieMap\u003c(string | string[])[], string\u003e(undefined, options);\n\nconst two = [\"2\"];\n\ntmap.set([], \"foo\").get([]); // =\u003e \"foo\"\ntmap.set([\"1\"], \"bar\").get([\"1\"]); // =\u003e \"bar\"\ntmap.set([\"1\", two, \"3\"], \"baz\").get([\"1\", two, \"3\"]); // =\u003e \"baz\"\ntmap.get([\"1\", [\"2\"], \"3\"]); // =\u003e undefined\n```\n\n### Class Private Fields and Unique Symbols for Encapsulation\n\nSee [`#private`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Classes/Private_class_fields) and [`Symbol()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)\n\n### No Dependencies\n\nThis package has no dependencies and the js code is only 1.37 kiB after minification.\n\n## Runtime Coverage\n\nThe runtime should support [ES modules](https://caniuse.com/?search=es%20modules), [Classes](https://caniuse.com/?search=classes), [Generators and `yield`](https://caniuse.com/?search=yield) and [Symbol](https://caniuse.com/?search=symbol). Although you should be able to transpile this package when using other front-end build or bundle tools.\n\nCheck the configured browserslist coverage [here](https://browsersl.ist/#q=supports+es6-module+and+last+4+versions+and+not+dead%2C+Chrome+%3E%3D+74%2C+Edge+%3E%3D+79%2C+Safari+%3E%3D+14.1%2C+Firefox+%3E%3D+90%2C+Opera+%3E%3D+62%2C+ChromeAndroid+%3E%3D+107%2C+iOS+%3E%3D+15%2C+Samsung+%3E%3D+11.1%2C+node+%3E%3D+14).\n\n## Notes\n\nAny two iterable reference type keys are considered equal, value-comparison-wise, as long as their iteration results are same:\n\n```ts\nimport { TrieMap } from \"@sec-ant/trie-map\";\n\nconst tmap = new TrieMap\u003cnumber[] | Uint8Array, string\u003e();\n\ntmap.set([1, 2], \"foo\");\n\ntmap.get(new Uint8Array([1, 2])); // =\u003e \"foo\"\n```\n\n## Todos\n\n- [ ] Type issues.\n- [ ] Unit tests.\n\n## LICENSE\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsec-ant%2Ftrie-map","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsec-ant%2Ftrie-map","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsec-ant%2Ftrie-map/lists"}