{"id":19451164,"url":"https://github.com/thekashey/search-trie","last_synced_at":"2025-10-28T04:03:23.743Z","repository":{"id":65412213,"uuid":"121064932","full_name":"theKashey/search-trie","owner":"theKashey","description":"Yet another O(n) trie. This time just and only for string search.","archived":false,"fork":false,"pushed_at":"2023-10-15T05:10:28.000Z","size":453,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-16T13:42:19.330Z","etag":null,"topics":["search","string-matching","trie"],"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/theKashey.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2018-02-10T23:47:55.000Z","updated_at":"2024-07-06T14:27:55.000Z","dependencies_parsed_at":"2024-06-19T01:42:27.969Z","dependency_job_id":null,"html_url":"https://github.com/theKashey/search-trie","commit_stats":{"total_commits":14,"total_committers":1,"mean_commits":14.0,"dds":0.0,"last_synced_commit":"2420835d39f5e1f94a629af66eab3f7c159781e7"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Fsearch-trie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Fsearch-trie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Fsearch-trie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/theKashey%2Fsearch-trie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/theKashey","download_url":"https://codeload.github.com/theKashey/search-trie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250754501,"owners_count":21481823,"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":["search","string-matching","trie"],"created_at":"2024-11-10T16:40:30.049Z","updated_at":"2025-10-28T04:03:23.638Z","avatar_url":"https://github.com/theKashey.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# search-trie\n\nA simple trie structure to perform prefix search on texts in O(n) time, where n - number of characters in searched word.\n\u003e Trie is a basic Tree structure, also known as [prefix tree](https://en.wikipedia.org/wiki/Trie)\nSuper simple, Super fast, super compact - less then 0.5kb.\n\n\n### Search-trie\n\nThe single purpose of this package is to find longest match between given strings and the search key.\nFor example:\n- given a couple of directories (`/src`, `/src/a`, `/src/b`, `/src/b/c`)\n- find the best match for a given file (`/src/b/c/index.tx` -\u003e `/src/b/c`)\n\n## Usage\n\nThis package provides two functions to build two different tries:\n- `buildCharacterTrie` - to create \"per character\" trie. Working great if you need to search something in the compressed json (short names)\n- `buildWordTrie` - to create trie where \"word\" is a key. Working great if there are many \"long keys\", for example directories you want to traverse faster\n\u003e one has more smaller nodes, another one has fewer larger ones. It's all about memory locality and algorithm сonvergence. \n\nThey have almost identical API, and if performance matters - you need to benchmark your data to understand which one is more efficient\n\n# Example\n```ts\nimport {buildWordTrie} from 'search-trie';\n\n// map package info into trie\n// using word trie as we operate with directory names\nconst trie = buildWordTrie(\n    packages.map(pkg =\u003e ({key: pkg.dir.split(path.sep), value: pkg})\n);\n    \n// it's always possible to insert new data. But `delete` operation is not defined    \ntrie.put({key:'another/package', value: pkg})\n\n// find longest (nearest to the search key) package. It will be a package containing this file\nconst getOwnerPackage = (fileName) =\u003e trie.findNearest(fileName).value;\n```\n\n# Used in\n- [proxy-equal](https://github.com/theKashey/proxyequal/blob/c0e167b932eb948f9b3fb15b0a56b40e492413bb/src/objectTrie.js) uses `buildCharacterTrie` to understand factual usage of an object.\n- [eslint-plugin-relations](https://github.com/theKashey/eslint-plugin-relations/blob/b80d8a4a6222107d59034bddaa5fe2cb14baab55/src/utils/mapping/mapping.ts#L31) - uses `buildWordTrie` to trim long imports to the nearest allowed\n- [idea-exclude](https://github.com/theKashey/idea-exclude/blob/a5f886a8298b909ef08108efd68e348bd0fb7907/src/utils.ts#L10) uses `buildWordTrie` to remove nested directories, ie creating trie containing shortest versions\n\n# Licence\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthekashey%2Fsearch-trie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthekashey%2Fsearch-trie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthekashey%2Fsearch-trie/lists"}