trie-search
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -6,3 +6,3 @@ { | ||
"description": "A trie implementation that maps keys to objects for rapid retrieval by phrases. Most common use will be for typeahead searches.", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "url": "https://github.com/joshjung/trie-search", |
@@ -64,3 +64,3 @@ ![](https://nodei.co/npm/trie-search.png?downloads=True&stars=True) | ||
var objects = [ | ||
var arr = [ | ||
{name: 'andrew', age: 21}, | ||
@@ -74,3 +74,3 @@ {name: 'andy', age: 37}, | ||
objects.forEach(ts.add.bind(ts)); | ||
ts.addAll(arr); | ||
@@ -88,3 +88,3 @@ ts.get('a'); // Returns all 4 items above. | ||
var objects = [ | ||
var arr = [ | ||
{name: 'andrew', details: {age: 21}}, | ||
@@ -101,3 +101,3 @@ {name: 'andy', details: {age: 37}}, | ||
objects.forEach(ts.add.bind(ts)); | ||
ts.addAll(arr); | ||
@@ -111,3 +111,3 @@ ts.get('21'); // Returns 'andrew' which has age of 21 | ||
var objects = [ | ||
var arr = [ | ||
{name: 'andrew', age: 21}, | ||
@@ -121,3 +121,3 @@ {name: 'andy', age: 37}, | ||
objects.forEach(ts.add.bind(ts)); | ||
ts.addAll(arr); | ||
@@ -139,3 +139,3 @@ ts.get('a'); // Returns empty array, too short of search | ||
var objects = [ | ||
var arr = [ | ||
{ix: 1, name: 'andrew', location: 'sweden', age: 21}, | ||
@@ -148,3 +148,3 @@ {ix: 2, name: 'andrew', location: 'brussels', age: 37}, | ||
objects.forEach(ts.add.bind(ts)); | ||
ts.addAll(arr); | ||
@@ -159,3 +159,3 @@ ts.get('andrew'); // Returns all items | ||
var objects = [ | ||
var arr = [ | ||
{name: 'andrew', age: 21, zip: 60600}, | ||
@@ -169,3 +169,3 @@ {name: 'andy', age: 37, zip: 60601}, | ||
objects.forEach(ts.add.bind(ts)); | ||
ts.addAll(arr); | ||
@@ -187,3 +187,3 @@ ts.get('andre'); // Returns only andrew. | ||
54 passing (25ms) | ||
59 passing (25ms) | ||
@@ -190,0 +190,0 @@ License |
@@ -13,2 +13,3 @@ var HashArray = require('hasharray'); | ||
this.options.splitOnRegEx = this.options.hasOwnProperty('splitOnRegEx') ? this.options.splitOnRegEx : /\s/g; | ||
this.options.min = this.options.min || 1; | ||
@@ -18,3 +19,6 @@ this.keyFields = keyFields ? (keyFields instanceof Array ? keyFields : [keyFields]) : []; | ||
this.size = 0; | ||
this.getCache = new HashArray('key'); | ||
if (this.options.cache) { | ||
this.getCache = new HashArray('key'); | ||
} | ||
}; | ||
@@ -31,2 +35,7 @@ | ||
// Someone might have called add via an array forEach where the second param is a number | ||
if (typeof customKeys === 'number') { | ||
customKeys = undefined; | ||
} | ||
var keyFields = customKeys || this.keyFields; | ||
@@ -56,2 +65,6 @@ | ||
}, | ||
addAll: function (arr, customKeys) { | ||
for (var i = 0; i < arr.length; i++) | ||
this.add(arr[i], customKeys); | ||
}, | ||
reset: function () { | ||
@@ -62,2 +75,5 @@ this.root = {}; | ||
clearCache: function () { | ||
// if (this.getCache && !this.getCache._list.length) { | ||
// return; | ||
// } | ||
this.getCache = new HashArray('key'); | ||
@@ -64,0 +80,0 @@ }, |
@@ -73,2 +73,27 @@ var assert = require('assert'), | ||
}); | ||
describe('TrieSearch::addAll(...)should work for an array', function() { | ||
var ts = new TrieSearch('key'), | ||
items = [{key: 'addendum'}, {key: 'banana'}, {key: 'cat'}]; | ||
ts.addAll(items); | ||
it('get(\'blah\') for each subkey should work', function() { | ||
assert.equal(ts.get('b')[0], items[1]); | ||
assert.equal(ts.get('ba')[0], items[1]); | ||
assert.equal(ts.get('ban')[0], items[1]); | ||
assert.equal(ts.get('bana')[0], items[1]); | ||
assert.equal(ts.get('banana')[0], items[1]); | ||
assert.equal(ts.get('a')[0], items[0]); | ||
assert.equal(ts.get('ad')[0], items[0]); | ||
assert.equal(ts.get('add')[0], items[0]); | ||
assert.equal(ts.get('adde')[0], items[0]); | ||
assert.equal(ts.get('addendum')[0], items[0]); | ||
assert.equal(ts.get('c')[0], items[2]); | ||
assert.equal(ts.get('ca')[0], items[2]); | ||
assert.equal(ts.get('cat')[0], items[2]); | ||
}); | ||
}); | ||
@@ -75,0 +100,0 @@ describe('TrieSearch::add(...) and TrieSearch::get(...) should work for a single item with a numeric key', function() { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8993105
1602