Node Autocomplete
Modernized Trie-search based on Node Autocomplete is an autocomplete library for node.js.
TODO: Cleanup test suite, use chai. Add all them missing semi-colons...
Installation
npm install triesearch
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']
var Autocomplete = require('autocomplete')
var auto = new Autocomplete()
auto.initialize(data)
var results = auto.search('ap')
console.dir(results)
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']
var Autocomplete = require('autocomplete')
var auto = new Autocomplete()
auto.initialize(data)
var results = auto.search('ap')
console.dir(results)
var stringMatches = results.map(function (result) {
return result.key
})
console.dir(stringMatches)
Adding
You can add to the list of candidate elements after the autocomplete object has been initialized
var Autocomplete = require('autocomplete')
var auto = new Autocomplete()
var data = []
auto.initialize(data)
auto.addElement('cheeseburger')
Removing
You can remove from the list of candidate elements after the autocomplete object has been initialized
var Autocomplete = require('autocomplete')
var auto = new Autocomplete()
var data = ['app', 'apple', 'apples']
auto.initialize(data)
auto.removeElement('apple')
Object Elements
You can also add key value pairs as an array of 2 elements
var Autocomplete = require('autocomplete')
var auto = new Autocomplete()
var data = ['app', 'apple', ['apples', 'yummy'], 'banana' ]
auto.initialize(data)
var results = app.search('ap')
console.dir(results)
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:
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.