trie-search
Advanced tools
Comparing version 0.1.1 to 1.0.0
@@ -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": "0.1.1", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
@@ -34,4 +34,4 @@ "url": "https://github.com/joshjung/trie-search", | ||
"dependencies": { | ||
"hasharray": "^0.3.0" | ||
"hasharray": "^1.0.0" | ||
} | ||
} |
@@ -73,3 +73,3 @@ ![](https://nodei.co/npm/trie-search.png?downloads=True&stars=True) | ||
objects.forEach(ts.add); | ||
objects.forEach(ts.add.bind(ts)); | ||
@@ -82,2 +82,23 @@ ts.get('a'); // Returns all 4 items above. | ||
Example 2 (deep key lookup) | ||
====================== | ||
var TrieSearch = require('trie-search'); | ||
var objects = [ | ||
{name: 'andrew', details: {age: 21}}, | ||
{name: 'andy', details: {age: 37}}, | ||
{name: 'andrea', details: {age: 25}}, | ||
{name: 'annette', details: {age: 67}} | ||
]; | ||
var ts = new TrieSearch([ | ||
'name', // Searches `object.name` | ||
['details', 'age'] // `Search object.details.age` | ||
]); | ||
objects.forEach(ts.add.bind(ts)); | ||
ts.get('21'); // Returns 'andrew' which has age of 21 | ||
Example 3 (options.min == 3) | ||
@@ -97,3 +118,3 @@ ====================== | ||
objects.forEach(ts.add); | ||
objects.forEach(ts.add.bind(ts)); | ||
@@ -123,3 +144,3 @@ ts.get('a'); // Returns empty array, too short of search | ||
objects.forEach(ts.add); | ||
objects.forEach(ts.add.bind(ts)); | ||
@@ -143,3 +164,3 @@ ts.get('andrew'); // Returns all items | ||
objects.forEach(ts.add); | ||
objects.forEach(ts.add.bind(ts)); | ||
@@ -161,3 +182,3 @@ ts.get('andre'); // Returns only andrew. | ||
37 passing (25ms) | ||
54 passing (25ms) | ||
@@ -164,0 +185,0 @@ License |
@@ -20,2 +20,6 @@ var HashArray = require('hasharray'); | ||
function deepLookup(obj, keys) { | ||
return keys.length === 1 ? obj[keys[0]] : deepLookup(obj[keys[0]], keys.slice(1, keys.length)); | ||
} | ||
TrieSearch.prototype = { | ||
@@ -29,4 +33,5 @@ add: function (obj) { | ||
var key = this.keyFields[k], | ||
val = obj[key]; | ||
isKeyArr = key instanceof Array, | ||
val = isKeyArr ? deepLookup(obj, key) : obj[key]; | ||
if (!val) continue; | ||
@@ -33,0 +38,0 @@ |
@@ -312,2 +312,47 @@ var assert = require('assert'), | ||
}); | ||
describe('TrieSearch::get(...) should work for a deep key combined with a non-deep key', function() { | ||
var ts = new TrieSearch(['key', ['key2', 'key3']], {min: 2, indexField: 'ix'}), | ||
item1 = {key: 'the quick brown fox', key2: {key3: 'jumped'}, ix: 1}, | ||
item2 = {key: 'the quick brown', key2: {key3: 'jumped'},ix: 2}, | ||
item3 = {key: 'the quick fox', key2: {key3: 'brown'}, ix: 3}, | ||
item4 = {key: 'the fox', key2: {key3: 'quick brown'}, ix: 4}; | ||
ts.add(item1); | ||
ts.add(item2); | ||
ts.add(item3); | ||
ts.add(item4); | ||
it('get(\'the quick\') should return all 4 entries', function() { | ||
assert(ts.get('the quick').length == 4); | ||
}); | ||
it('get(\'the brown\') should return all 4 entries', function() { | ||
assert(ts.get('the brown').length == 4); | ||
}); | ||
it('get(\'the fox\') should return 3 entries', function() { | ||
assert(ts.get('the fox').length == 3); | ||
}); | ||
it('get(\'fox brown\') should return 3 entries', function() { | ||
assert(ts.get('fox brown').length == 3); | ||
}); | ||
it('get(\'brown fox\') should return 3 entries', function() { | ||
assert(ts.get('brown fox').length == 3); | ||
}); | ||
it('get(\'brown z\') should return 4 entries', function() { | ||
assert(ts.get('brown z').length == 4); | ||
}); | ||
it('get(\'br f\') should return all entries', function() { | ||
assert(ts.get('br f').length == 4); | ||
}); | ||
it('get(\'jum b c d e f g h\') should return 2 entries, ignoring the shortness of all subsequent words', function() { | ||
assert(ts.get('jum b c d e f g h').length == 2); | ||
}); | ||
}); | ||
@@ -314,0 +359,0 @@ describe('TrieSearch::add(...) and TrieSearch::get(...) should work for a single item with multiple subphrases', 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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8990126
1542
0
203
+ Addedhasharray@1.1.2(transitive)
- Removeddebug@1.0.5(transitive)
- Removedhasharray@0.3.0(transitive)
- Removedms@2.0.0(transitive)
Updatedhasharray@^1.0.0