Socket
Socket
Sign inDemoInstall

trie-search

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trie-search - npm Package Compare versions

Comparing version 0.1.1 to 1.0.0

4

package.json

@@ -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() {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc