{"id":19494706,"url":"https://github.com/ceejbot/prefix-completer","last_synced_at":"2025-04-25T21:31:57.893Z","repository":{"id":2273729,"uuid":"3230479","full_name":"ceejbot/prefix-completer","owner":"ceejbot","description":"Simple redis-backed node.js library for managing and querying an autocomplete dictionary.","archived":false,"fork":false,"pushed_at":"2017-07-15T22:53:16.000Z","size":100,"stargazers_count":4,"open_issues_count":57,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-18T20:25:37.027Z","etag":null,"topics":[],"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/ceejbot.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":"2012-01-20T22:35:42.000Z","updated_at":"2022-01-10T09:35:19.000Z","dependencies_parsed_at":"2022-09-10T10:36:40.017Z","dependency_job_id":null,"html_url":"https://github.com/ceejbot/prefix-completer","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/ceejbot%2Fprefix-completer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceejbot%2Fprefix-completer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceejbot%2Fprefix-completer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ceejbot%2Fprefix-completer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ceejbot","download_url":"https://codeload.github.com/ceejbot/prefix-completer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250899962,"owners_count":21504971,"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-11-10T21:32:12.045Z","updated_at":"2025-04-25T21:31:57.568Z","avatar_url":"https://github.com/ceejbot.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Simple Prefix Completer\n\n[![on npm](http://img.shields.io/npm/v/prefix-completer.svg?style=flat)](https://www.npmjs.org/package/prefix-completer)  [![Tests](http://img.shields.io/travis/ceejbot/prefix-completer.svg?style=flat)](http://travis-ci.org/ceejbot/prefix-completer) [![Coverage](http://img.shields.io/coveralls/ceejbot/prefix-completer.svg?style=flat)](https://coveralls.io/r/ceejbot/prefix-completer) [![Dependencies](http://img.shields.io/david/ceejbot/prefix-completer.svg?style=flat)](https://david-dm.org/ceejbot/prefix-completer)\n\nA simple prefix completion library for [node.js](http://nodejs.org/) \u0026 [Redis](http://redis.io/). Stores texts \u0026 their prefixes in redis according to the algorithm given by [antirez](https://github.com/antirez) [in this gist](https://gist.github.com/574044). It's inefficient with space but leverages the redis sorted set data structure cleverly.\n\nThe use case it was developed for is tag auto-completion, for which I needed no fancy datastructures or scoring approaches. The only scoring used is lexical sorting. No additional data is stored along with the texts. All texts are stored and searched in lower case.\n\nDepends only on redis.\n\n## Usage\n\n`npm install prefix-completer`\n\nHere's an example showing typical use: creating a completer dictionary,\nadding words to it, then requesting completions from it.\n\n```javascript\nconst completer = require('prefix-completer');\nconst async = require('async');\n\n// make a dictionary\nconst tags = completer.create( { key: 'tags', db: 31 } );\nconsole.log(`zkey: ${tags.rediskey}`); // -\u003e 'tags'\n\n// make a second completion dictionary\nvar usertags = completer.create( { key: 'users', db: 31 });\nconsole.log(`zkey: ${usertags.rediskey}`); // -\u003e 'usertags'\n\nvar wordlist = ['supernumary', ' superb ', 'Super', 'mary poppins', 'bert the sweep', 'codfish', 'sugar'];\n\nasync.series(\n[\n\tfunction(cb) { tags.flush(cb) }, // clear for the example\n\tfunction(cb) { tags.add('supercalifragilisticexpialidocious', cb) }, // add a single word\n\tfunction(cb) { tags.addList(wordlist, cb) }, // add a list of words\n\tfunction(cb) { tags.complete('supe', 15, cb) }, // get completions for a prefix\n\tfunction(cb) { usertags.complete('supe', 15, cb) } // get completions from another dictionary\n],\nfunction(err, results)\n{\n\tconsole.log(`added 1 completion to dict: ${results[1]}`);\n\tconsole.log(`added ${results[2].length} completions to dict`);\n\tconsole.log(`found ${results[3][1].length} completions for ${results[3][0]}:`);\n\tconsole.log(results[3][1]);\n\tconsole.log(`the user tags dictionary has ${results[4][1].length} completions for 'supe'`);\n\tprocess.exit(0);\n});\n```\n\n## API\n\nAll callback-accepting API functions also have a promise-returning `*-Async` version, created by Bluebird's promisify.\n\n### create()\n\n`completer = create([options])`\n\nCreate a completion dictionary. Synchronous. Takes optional hash of parameters. Valid parameters:\n\n__redis__: something to pass to redis's `createClient()`; can be a redis url\n__client__: an existing RedisClient\n__db__: integer specifying the redis database to select (defaults to 0)\n__key__: a short string key for the redis sorted set key; change it to namespace lookup dictionaries (defaults to `'COMP'`)\n\n\n### add()\n\n`completer.add(completion, function(err, added) {})`\n\nAdd a string to the completion dictionary. Leading \u0026 trailing space are removed and the string is converted to lowercase. Responds with the exact string added. If the string was already present, responds with null. Can also accept an array of additions in the first parameter.\n\n### addList()\n\n`completer.add(stringlist, function(err, addedlist) {})`\n\nAdd an array of strings to the completion dictionary. Responds with an array of the exact strings added. Skips strings that were already present.\n\n### complete()\n\n`completer.complete(prefix, maxresults, function(err, prefixcleaned, completions) {})`\n\nSearch for completions for the passed-in search string. Responds with the exact string searched for and an array of completions containing at most `maxresults` strings. The `maxresults` parameter is optional; it defaults to 50 if not passed.\n\n### remove()\n\n`completer.remove(completion, function(err, removed) {})`\n\nRemoves the specified completion from the dictionary. Responds with true if successful.\n\n### flush()\n\n`completer.flush(function(err, count) {})`\n\nDelete the key used to store this dictionary set. Passes the callback straight through to the redis DEL call, which responds with the number of keys deleted.\n\n### statistics()\n\n`completer.statistics(function(err, results))`\n\nGet statistics on the current state of the completion database. Responds with a hash containing the following keys:\n\n__total__: total number of entries in the database\n__leaves__: number of completion phrases stored\n__leaflen__: characters used for completion phrases\n__prefixlen__: characters used for prefix storage\n\n### leaves()\n\n`completer.leaves(function(err, leaves))`\n\nResponds with an array containing all leaf nodes in the dictionary, that is, every valid completion. Does not strip the terminating `*` characters.\n\n### dump()\n\n`completer.dump(function(err, items))`\n\nResponds with an array containing all the items stored in the dictionary. Perhaps useful for debugging.\n\n## LICENSE\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceejbot%2Fprefix-completer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceejbot%2Fprefix-completer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceejbot%2Fprefix-completer/lists"}