trie-search
Advanced tools
Comparing version 1.2.9 to 1.2.11
@@ -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.2.9", | ||
"version": "1.2.11", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "url": "https://github.com/joshjung/trie-search", |
@@ -252,7 +252,14 @@ var HashArray = require('hasharray'); | ||
}, | ||
_get: function (phrase) { | ||
_getCacheKey: function(phrase, limit){ | ||
var cacheKey = phrase | ||
if(limit) { | ||
cacheKey = phrase + "_" + limit | ||
} | ||
return cacheKey | ||
}, | ||
_get: function (phrase, limit) { | ||
phrase = this.options.ignoreCase ? phrase.toLowerCase() : phrase; | ||
var c, node; | ||
if (this.options.cache && (c = this.getCache.get(phrase))) | ||
if (this.options.cache && (c = this.getCache.get(this._getCacheKey(phrase, limit)))) | ||
return c.value; | ||
@@ -281,3 +288,4 @@ | ||
{ | ||
this.getCache.add({key: phrase, value: v}); | ||
var cacheKey = this._getCacheKey(phrase, limit) | ||
this.getCache.add({key: cacheKey, value: v}); | ||
this.cleanCache(); | ||
@@ -289,11 +297,27 @@ } | ||
function aggregate(node, ha) { | ||
if (node.value && node.value.length) | ||
ha.addAll(node.value); | ||
if(limit && ha.all.length === limit) { | ||
return | ||
} | ||
for (var k in node) | ||
if (k != 'value') | ||
if (node.value && node.value.length) { | ||
if(!limit || (ha.all.length + node.value.length) < limit) { | ||
ha.addAll(node.value); | ||
} else { | ||
// Limit is less than the number of entries in the node.value + ha combined | ||
ha.addAll(node.value.slice(0, limit - ha.all.length)) | ||
return | ||
} | ||
} | ||
for (var k in node) { | ||
if (limit && ha.all.length === limit){ | ||
return | ||
} | ||
if (k != 'value') { | ||
aggregate(node[k], ha); | ||
} | ||
} | ||
} | ||
}, | ||
get: function (phrases, reducer) { | ||
get: function (phrases, reducer, limit) { | ||
var self = this, | ||
@@ -312,3 +336,3 @@ haKeyFields = this.options.indexField ? [this.options.indexField] : this.keyFields, | ||
{ | ||
var matches = this._get(phrases[i]); | ||
var matches = this._get(phrases[i], limit); | ||
@@ -315,0 +339,0 @@ if (reducer) { |
@@ -740,2 +740,34 @@ var assert = require('assert'), | ||
}); | ||
describe('TrieSearch:TrieSearch::get(...) should work with limits', function() { | ||
// NOTE: Cache is set to true since caching also needs to be tested | ||
var ts = new TrieSearch(null, {cache: true}); | ||
var obj = { | ||
"a": ["data"], | ||
"ab": ["data"], | ||
"abc": ["data"], | ||
"abcd": ["data"], | ||
"abcde": ["data"], | ||
"abcdef": ["data"], | ||
} | ||
ts.addFromObject(obj); | ||
it('Get with limits and get without limits should work properly', function() { | ||
var getWithoutLimit = ts.get("a") | ||
assert(getWithoutLimit.length === 6, "Expected 6 in without-limit get") | ||
var getWithLimitResp= ts.get("a", null, 4) | ||
assert(getWithLimitResp.length === 4, "Expected 4 in with-limit get") | ||
}); | ||
it('Failure case with limits should work properly', function() { | ||
var getWithLimit = ts.get("b", null, 4) | ||
assert(getWithLimit.length === 0, "Expected 0 in with-limit get") | ||
}); | ||
it('A bigger limit value than the actual amount of data must work properly', function() { | ||
var getWithLimit = ts.get("a", null, 100) | ||
assert(getWithLimit.length === 6, "Expected 6 in with-limit get") | ||
}); | ||
}); | ||
}); |
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
9012421
1965