trie-search
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -28,2 +28,3 @@ // Type definitions for trie-search 2.0+ | ||
add(item : T, customKeys? : KeyFields) : void | ||
remove(phrase : string, keyFields? : KeyFields) : void | ||
addAll(items : T[], customKeys? : KeyFields) : void | ||
@@ -30,0 +31,0 @@ reset() : void |
@@ -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": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "index.js", | ||
@@ -14,3 +14,3 @@ "types": "index.d.ts", | ||
"scripts": { | ||
"test": "jest test/trie-search.test.js", | ||
"test": "jest test/trie-search.test.ts", | ||
"release:major": "npm version major && git push --follow-tags && npm publish", | ||
@@ -17,0 +17,0 @@ "release:minor": "npm version minor && git push --follow-tags && npm publish", |
@@ -18,2 +18,4 @@ ![](https://nodei.co/npm/trie-search.png?downloads=True&stars=True) | ||
There is also a `remove(str: string)` method that will remove every node that has the exact `str` string passed as an argument, as well as the corresponding diacritic variants. This method also works for strings that have multiple words separated by spaces. | ||
# Install | ||
@@ -48,2 +50,4 @@ | ||
trie.search('hello trains'); // [item2] | ||
trie.remove('hello world'); | ||
trie.search('hel'); // [item2] | ||
``` | ||
@@ -383,2 +387,19 @@ | ||
## `remove()` | ||
``` | ||
import TrieSearch from 'trie-search'; | ||
const trie : TrieSearch<any> = new TrieSearch(['keyfield1', 'keyfield2']); | ||
const keyValue = 'value'; | ||
const item1 = {keyfield1: keyValue}; | ||
const item2 = {keyfield2: keyValue}; | ||
trie.add(item1); | ||
trie.add(item2); | ||
trie.search(keyValue); // [item1, item2] | ||
trie.remove(keyValue, 'keyfield1'); | ||
trie.search(keyValue); // [item2] | ||
``` | ||
# Testing | ||
@@ -391,5 +412,5 @@ | ||
Test Suites: 1 passed, 1 total | ||
Tests: 81 passed, 81 total | ||
Tests: 87 passed, 87 total | ||
Snapshots: 0 total | ||
Time: 1.754 s | ||
Time: 0.338 s, estimated 1 s | ||
``` | ||
@@ -396,0 +417,0 @@ |
@@ -69,7 +69,6 @@ var HashArray = require('hasharray'); | ||
add: function (obj, customKeys) { | ||
if (this.options.cache) | ||
this.clearCache(); | ||
if (this.options.cache) this.clearCache(); | ||
// Someone might have called add via an array forEach where the second param is a number | ||
if (typeof customKeys === 'number') { | ||
if (typeof customKeys === "number") { | ||
customKeys = undefined; | ||
@@ -80,4 +79,3 @@ } | ||
for (var k in keyFields) | ||
{ | ||
for (var k in keyFields) { | ||
var key = keyFields[k], | ||
@@ -100,2 +98,45 @@ isKeyArr = key instanceof Array, | ||
}, | ||
remove: function (phrase, keyFields) { | ||
if (!phrase) return; | ||
phrase = phrase.toString(); | ||
keyFields = keyFields || this.keyFields; | ||
keyFields = keyFields instanceof Array ? keyFields : [keyFields]; | ||
if (this.options.cache) this.clearCache(); | ||
var diacriticalVariants = this.expandString(phrase); | ||
for (var variant of diacriticalVariants) { | ||
var words = variant.split(" "); | ||
for (var word of words) { | ||
this.removeNode(this.root, keyFields, phrase, word); | ||
} | ||
} | ||
}, | ||
removeNode: function (node, keyFields, phrase, word) { | ||
if (!node) { | ||
return null; | ||
} | ||
if (!word.length) { | ||
node.value = node.value.filter( | ||
(item) => !keyFields.some((key) => item[key] === phrase), | ||
); | ||
if (!node.value.length) { | ||
delete node.value; | ||
} | ||
return; | ||
} | ||
var char = word[0]; | ||
if (node[char]) { | ||
this.removeNode(node[char], keyFields, phrase, word.slice(1)); | ||
this.deleteNodeIfEmpty(node, char); | ||
} | ||
}, | ||
deleteNodeIfEmpty: function (parentNode, key) { | ||
if (Object.keys(parentNode[key]).length === 0) { | ||
delete parentNode[key]; | ||
this.size--; | ||
} | ||
}, | ||
/** | ||
@@ -115,3 +156,3 @@ * By default using the options.expandRegexes, given a string like 'ö är bra', this will expand it to: | ||
*/ | ||
expandString: function(value) { | ||
expandString: function (value) { | ||
var values = [value]; | ||
@@ -118,0 +159,0 @@ |
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
31562
393
442