{"id":18434743,"url":"https://github.com/moroshko/autosuggest-trie","last_synced_at":"2025-04-07T19:31:59.702Z","repository":{"id":31530904,"uuid":"35095436","full_name":"moroshko/autosuggest-trie","owner":"moroshko","description":"Minimalistic trie implementation for autosuggest and autocomplete components","archived":false,"fork":false,"pushed_at":"2017-05-02T23:49:52.000Z","size":3106,"stargazers_count":25,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T00:05:12.835Z","etag":null,"topics":["autocomplete","autosuggest","javascript","trie","typeahead"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/moroshko.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-05T11:32:29.000Z","updated_at":"2023-12-01T07:18:04.000Z","dependencies_parsed_at":"2022-08-28T13:00:30.595Z","dependency_job_id":null,"html_url":"https://github.com/moroshko/autosuggest-trie","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moroshko%2Fautosuggest-trie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moroshko%2Fautosuggest-trie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moroshko%2Fautosuggest-trie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/moroshko%2Fautosuggest-trie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/moroshko","download_url":"https://codeload.github.com/moroshko/autosuggest-trie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247716378,"owners_count":20984231,"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":["autocomplete","autosuggest","javascript","trie","typeahead"],"created_at":"2024-11-06T06:05:11.508Z","updated_at":"2025-04-07T19:31:59.418Z","avatar_url":"https://github.com/moroshko.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://img.shields.io/codeship/a3eddcc0-d548-0132-ef15-420032d7f4bd/master.svg?style=flat-square)](https://codeship.com/projects/77991)\n[![Coverage Status](https://img.shields.io/codecov/c/github/moroshko/autosuggest-trie/master.svg?style=flat-square)](https://codecov.io/gh/moroshko/autosuggest-trie)\n[![npm Version](https://img.shields.io/npm/v/autosuggest-trie.svg?style=flat-square)](https://npmjs.org/package/autosuggest-trie)\n![gzip size](http://img.badgesize.io/https://unpkg.com/autosuggest-trie/dist/index.js?compression=gzip\u0026style=flat-square)\n\n# Autosuggest Trie\n\nMinimalistic trie implementation for autosuggest and autocomplete components.\n\n## Installation\n\n```shell\nnpm install autosuggest-trie --save\n```\n\n## Basic Usage\n\n```js\nimport createTrie from 'autosuggest-trie';\n\nconst locations = [\n  {\n    id: 1,\n    name: 'East Richmond 1234 VIC',\n    population: 10000\n  },\n  {\n    id: 2,\n    name: 'East Eagle 1235 VIC',\n    population: 5000\n  },\n  {\n    id: 3,\n    name: 'Richmond West 5678 VIC',\n    population: 4000\n  },\n  {\n    id: 4,\n    name: 'Cheltenham 3192 Melbourne VIC',\n    population: 7000\n  },\n  {\n    id: 5,\n    name: 'Richmond 6776 VIC',\n    population: 3000\n  }\n];\n\nconst trie = createTrie(locations, 'name');\n\nconsole.log(trie.getMatches('richmond e'));\n/*\n  [ { id: 1, name: 'East Richmond 1234 VIC', population: 10000 } ]\n*/\n\nconsole.log(trie.getMatches('ri', { limit: 2 }));\n/*\n  [ { id: 3, name: 'Richmond West 5678 VIC', population: 4000 },\n    { id: 5, name: 'Richmond 6776 VIC', population: 3000 } ]\n*/\n```\n\n## API\n\n| Function | Description |\n| :--- | :--- |\n| [`createTrie(items, textKey, options)`](#createTrieFunction) | Creates a trie containing the given items. |\n| [`getMatches(query, options)`](#getMatchesFunction) | Returns items that match the given query. |\n\n\u003ca name=\"createTrieFunction\"\u003e\u003c/a\u003e\n### createTrie(items, textKey, options)\n\nCreates a trie containing the given items.\n\n| Parameter | Type | Required | Description |\n| :--- | :--- | :---: | :--- |\n| `items` | Array | ✓ | Array of items. Every item must be an object. |\n| `textKey` | String | ✓ | Key that every `item` in `items` must have.\u003cbr /\u003e`item` will be inserted to the trie based on `item[textKey]`. |\n| `options` | Object | | Additional options |\n\nPossible options:\n\n| Option | Type | Default | Description |\n| :--- | :--- | :---: | :--- |\n| `comparator` | Function | none | Items comparator, similar to [`Array#sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)'s `compareFunction`.\u003cbr /\u003eIt gets two items, and should return a number.\u003cbr /\u003e\u003cbr /\u003e**Note:** Matches in the first word (let's call it \"group 1\") are prioritized over matches in the second word (\"group 2\"), which are prioritized over matches in the third word (\"group 3\"), and so on.\u003cbr /\u003e`comparator` will only sort the matches **within each group**.\u003cbr /\u003e\u003cbr /\u003eWhen `comparator` is not specified, items within each group will preserve their order in `items`. |\n| `splitRegex` | RegExp | `/\\s+/` | Used to split items' `textKey` into words. |\n\n\u003ca name=\"getMatchesFunction\"\u003e\u003c/a\u003e\n### getMatches(query, options)\n\nReturns items that match the given query.\n\n| Parameter | Type | Required | Description |\n| :--- | :--- | :---: | :--- |\n| `query` | String | ✓ | Non-blank `query` string.\u003cbr /\u003e\u003cbr /\u003eIf `query` is blank, `[]` is returned. |\n| `options` | Object | | Additional `query` options. |\n\nPossible `options`:\n\n| Option | Type | Default | Description |\n| :--- | :--- | :---: | :--- |\n| `limit` | Number | `Infinity` | Integer \u003e= 1\u003cbr /\u003e\u003cbr /\u003e**For example:** `getMatches('me', { limit: 3 })` will return no more than 3 items that match `'me'`. |\n| `splitRegex` | RegExp | `/\\s+/` | Used to split the `query` into words. |\n\n## Running Tests\n\n```shell\nnpm test\n```\n\n## License\n\n[MIT](http://moroshko.mit-license.org)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoroshko%2Fautosuggest-trie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmoroshko%2Fautosuggest-trie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmoroshko%2Fautosuggest-trie/lists"}