Node Autocomplete
Node Autocomplete is an autocomplete library for node.js.
Installation
npm install -S autocomplete
Features
- in memory, in process, not redis dependent
- internal trie data structure to store the strings
- super fast for adding, removing and lookups
- performance tested for string lists of 500,000 words
- high level of tests
Usage
var data = 'fruit' 'app' 'apple' 'banana'// instatiate a new autocomplete objectvar Autocomplete = var auto = auto// results will be an array with 0 or more elementsvar results = auto // here results will be an array of key-value pairsconsoledirresults
Output
The output of the search is an array of objects with key and value properties. In the example above, results
looks like
key: 'app' value: 'app' key: 'apple' value: 'apple, }]
If you are just searching for strings you can get an array of matching strings by mapping the results and returning just the key
var data = 'fruit' 'app' 'apple' 'banana'// instatiate a new autocomplete objectvar Autocomplete = var auto = auto// results will be an array with 0 or more elementsvar results = auto // here results will be an array of key-value pairsconsoledirresultsvar stringMatches = results// stringMatches is ['app', 'apple']consoledirstringMatches
Adding
You can add to the list of candidate elements after the autocomplete object has been initialized
var Autocomplete = var auto = var data = autoauto
Removing
You can remove from the list of candidate elements after the autocomplete object has been initialized
var Autocomplete = var auto = var data = 'app' 'apple' 'apples'autoauto
Object Elements
You can also add key value pairs as an array of 2 elements
var Autocomplete = var auto = var data = 'app' 'apple' 'apples' 'yummy' 'banana' autovar results = app// in the results, there will be an element with the key *apples* and the value *yummy*consoledirresults
In the example above, results
looks like
key: 'app' value: 'app' key: 'apple' value: 'apple' key: 'apples' value: 'yummy'
Running Tests
Install development dependencies:
npm install
Then:
npm test
Actively tested with node:
- 0.8.22
Authors
- Noah Isaacson
- Marc Campbell
Original source code based on https://github.com/marccampbell/node-autocomplete
License
(The MIT License)
Copyright (c) 2011 Marc Campbell <marc.e.campbell@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.