{"id":15408933,"url":"https://github.com/sec-ant/trie","last_synced_at":"2026-02-03T17:35:55.657Z","repository":{"id":213679265,"uuid":"725636343","full_name":"Sec-ant/trie","owner":"Sec-ant","description":"A simple Trie structure with wildcard support to match streams.","archived":false,"fork":false,"pushed_at":"2025-08-05T02:49:58.000Z","size":195,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-11-16T12:23:11.283Z","etag":null,"topics":["reset","stream","trie","wildcard"],"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/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":"2023-11-30T15:01:58.000Z","updated_at":"2024-09-03T10:32:00.000Z","dependencies_parsed_at":"2023-12-25T09:41:57.341Z","dependency_job_id":"8da33774-77a6-4a8a-80f4-bd37689333f0","html_url":"https://github.com/Sec-ant/trie","commit_stats":{"total_commits":16,"total_committers":2,"mean_commits":8.0,"dds":0.25,"last_synced_commit":"32272f42da3d3d6e3c4603c42e046ba10be9c581"},"previous_names":["sec-ant/trie"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/Sec-ant/trie","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sec-ant","download_url":"https://codeload.github.com/Sec-ant/trie/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sec-ant%2Ftrie/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29050941,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-03T15:43:47.601Z","status":"ssl_error","status_checked_at":"2026-02-03T15:43:46.709Z","response_time":96,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["reset","stream","trie","wildcard"],"created_at":"2024-10-01T16:36:01.570Z","updated_at":"2026-02-03T17:35:55.616Z","avatar_url":"https://github.com/Sec-ant.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @sec-ant/trie\n\nThis repository contains an implementation of a Trie data structure in TypeScript. A Trie, also known as a prefix tree, is a tree-like data structure that stores data in a way that allows for efficient retrieval, insertion, and deletion. This particular implementation, additionally, has the following features:\n\n- **Any Type of Values**: Path segments and data can be any types of values. Not only strings or numbers.\n- **Synchronous and Asynchronous Methods**: Both synchronous and asynchronous APIs are provided.\n- **Unique Symbols**: The internal structure uses unique symbols to prevent collisions with user input.\n- **Supports Wildcards**: The Trie implementation supports wildcard paths, allowing for flexible and powerful queries.\n- **Accepts Iterables**: Accept synchronous and asynchronous iterables as paths. This makes it easy to work with iterable types other than strings or arrays.\n- **Generator Results**: When using the Trie to match against a path, the result is a generator. This allows for efficient memory usage and instant outputs.\n- **TypeScript Support**: This package is fully written in TypeScript and provides strong typing for better development experience.\n\n\u003ctable align=\"center\"\u003e\n\u003ctbody\u003e\n\u003ctr align=\"center\"\u003e\n\u003ctd\u003eWhen constructing a Trie like this, \u003c/td\u003e\n\u003ctd\u003ethe internal structure will be like\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd\u003e\n\n```ts\nnew Trie([\n  [[1, 2], \"12\"],\n  [[2, w(5), 3], \"2*****3\"],\n  [[2, w(3), 4], \"2***4\"],\n]);\n```\n\n\u003c/td\u003e\n\u003ctd\u003e\n\n```\nMap {\n  1 =\u003e Map {\n    2 =\u003e Map {\n      [sym:d] =\u003e \"12\",\n    },\n  },\n  2 =\u003e Map {\n    [sym:*] =\u003e Map {\n      [sym:c] =\u003e 3,\n      [sym:*] =\u003e Map {\n        [sym:c] =\u003e 2,\n        3 =\u003e Map {\n          [sym:d] =\u003e \"2*****3\",\n        },\n      },\n      4 =\u003e Map {\n        [sym:d] =\u003e \"2***4\",\n      },\n    },\n  },\n}\n```\n\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctbody\u003e\n\u003c/table\u003e\n\n## Installation\n\nTo install this package, use the following command:\n\n```bash\nnpm install @sec-ant/trie\n```\n\n## Usage\n\nHere's a basic example of how to use this Trie implementation:\n\n```typescript\nimport { Trie, w } from \"@sec-ant/trie\";\n\n// Create a new Trie\nconst trie = new Trie\u003cstring, string\u003e();\n\n// Set a path with no wildcards to the Trie\ntrie.setSync([\"path\", \"with\", \"no\", \"wildcard\"], \"value1\");\n\n// Set another path with wildcards to the Trie\ntrie.setSync([\"path\", \"with\", w(2), w(2), \"wildcards\"], \"value2\");\n\n// Check if the first path exists in the Trie\nconsole.log(trie.hasSync([\"path\", \"with\", \"no\", \"wildcard\"])); // true\n\n// Check if the second path exists in the Trie (you can combine consecutive wildcards into one)\nconsole.log(trie.hasSync([\"path\", \"with\", w(4), \"wildcards\"])); // true\n\n// Retrieve the value of the first path\nconsole.log(trie.getSync([\"path\", \"with\", \"no\", \"wildcard\"])); // \"value1\"\n\n// Retrieve the value of the second path (you can split consecutive wildcards into multi-parts)\nconsole.log(trie.getSync([\"path\", \"with\", w(1), w(3), \"wildcards\"])); // \"value2\"\n\n// Match against a path and generate values\nfor (const value of trie.matchSync([\n  \"path\",\n  \"with\",\n  \"no\",\n  \"wildcard\",\n  \"or\",\n  \"with\",\n  \"wildcards\",\n])) {\n  console.log(value); // \"value1\" then \"value2\"\n}\n\n// Delete a path from the Trie\ntrie.delete([\"path\", \"to\", w(1), w(1), w(1), \"wildcards\"]);\n\n// Clear all paths from the Trie\ntrie.clear();\n```\n\n## API\n\n### `w`\n\nThis is a function to create wildcard segments that can be used in a path when constructing the Trie:\n\n```ts\n/**\n * Creates a wildcard path segment with an optional count.\n * @param count - Optional count for the wildcard. Default is 1.\n * @returns A  wildcard path segment.\n */\nexport declare function w(count?: number): WildcardSegment;\n```\n\n### `Trie`\n\n#### Methods\n\nThe `Trie` class provides the following methods:\n\n- `constructor(initialEntries?: Iterable\u003c[Iterable\u003cI | WildcardSegment\u003e, V]\u003e)`: Constructs a new Trie. Optionally populates the Trie with initial entries.\n- `add(initialEntries: AnyIterable\u003c[AnyIterable\u003cI | WildcardSegment\u003e, V]\u003e)`: Asynchronously adds paths to the Trie with the specified values.\n- `addSync(initialEntries: Iterable\u003c[Iterable\u003cI | WildcardSegment\u003e, V]\u003e)`: Synchronously adds paths to the Trie with the specified values.\n- `set(path: AnyIterable\u003cI | WildcardSegment\u003e, value: V)`: Asynchronously sets the value for a path in the Trie.\n- `setSync(path: Iterable\u003cI | WildcardSegment\u003e, value: V)`: Synchronously sets the value for a path in the Trie.\n- `has(path: AnyIterable\u003cI | WildcardSegment\u003e)`: Asynchronously checks if a path exists in the Trie.\n- `hasSync(path: Iterable\u003cI | WildcardSegment\u003e)`: Synchronously checks if a path exists in the Trie.\n- `get(path: AnyIterable\u003cI | WildcardSegment\u003e)`: Asynchronously retrieves the value associated with a path in the Trie.\n- `getSync(path: Iterable\u003cI | WildcardSegment\u003e)`: Synchronously retrieves the value associated with a path in the Trie.\n- `delete(path: AnyIterable\u003cI | WildcardSegment\u003e)`: Asynchronously deletes a path from the Trie.\n- `deleteSync(path: Iterable\u003cI | WildcardSegment\u003e)`: Synchronously deletes a path from the Trie.\n- `clear()`: Clears all paths from the Trie.\n- `match(path: AnyIterable\u003cI\u003e)`: Asynchronously searches for values matching a given path. Returns an asynchronous generator.\n- `matchSync(path: Iterable\u003cI\u003e)`: Synchronously searches for values matching a given path. Returns a generator.\n\n#### Properties\n\nThe `Trie` class provides the following properties:\n\n- `size`: Get the size of the Trie, i.e., the number of paths. This value is read only.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsec-ant%2Ftrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsec-ant%2Ftrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsec-ant%2Ftrie/lists"}