@metacorp/trie
Advanced tools
Comparing version 0.0.5 to 0.0.6
/** | ||
* Trie v0.0.1 | ||
* Trie v0.0.6 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -25,10 +25,8 @@ * Released under the MIT License | ||
this.children = [] | ||
} | ||
else { | ||
} else { | ||
this.value = word[0] | ||
if (word.length === 1) { | ||
this.index = index | ||
this.addIndex(index) | ||
this.children = [] | ||
} | ||
else | ||
} else | ||
{ this.children = [new bNode(word.substr(1), index)] } | ||
@@ -48,2 +46,9 @@ } | ||
bNode.prototype.addIndex = function (index) { | ||
if (this.indexes) | ||
{ this.indexes.push(index) } | ||
else | ||
{ this.indexes = [index] } | ||
} | ||
bNode.prototype.run = function (cb) { | ||
@@ -66,8 +71,12 @@ cb(this) | ||
this.words.push(word) | ||
var prev = this.root, i = 0; | ||
for (var node = prev; node = node.getChild(word.charAt(i)); i++ , prev = node) { } | ||
if (i >= word.length) | ||
{ prev.index = index } | ||
else | ||
{ prev.addChild(new bNode(word.substr(i), index)) } | ||
var prev = this.root, | ||
i = 0; | ||
var wordArray = word.toLowerCase().split(' ') | ||
wordArray.forEach(function (word) { | ||
for (var node = prev; node = node.getChild(word.charAt(i)); i++, prev = node) {} | ||
if (i >= word.length) | ||
{ prev.addIndex(index) } | ||
else | ||
{ prev.addChild(new bNode(word.substr(i), index)) } | ||
}) | ||
} | ||
@@ -78,7 +87,13 @@ | ||
var ret = [] | ||
var prev = this.root, i = 0; | ||
for (var node = prev; node = node.getChild(str.charAt(i)); i++ , prev = node) { } | ||
i === str.length && prev.run(function (node) { return node.index !== undefined && ret.push(this$1.words[node.index]); }) | ||
return ret | ||
var ret = new Set() | ||
var strArray = str.toLowerCase().split(' ') | ||
strArray.forEach(function (str) { | ||
var prev = this$1.root, | ||
i = 0; | ||
for (var node = prev; node = node.getChild(str.charAt(i)); i++, prev = node) {} | ||
i === str.length && | ||
prev.run(function (node) { return node.indexes && | ||
node.indexes.forEach(function (index) { return ret.add(this$1.words[index]); }); }) | ||
}) | ||
return Array.from(ret) | ||
} | ||
@@ -90,5 +105,5 @@ | ||
Trie.version = "0.0.1" | ||
Trie.version = "0.0.6" | ||
return Trie; | ||
})); |
/** | ||
* Trie v0.0.1 | ||
* Trie v0.0.6 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -7,2 +7,2 @@ * Released under the MIT License | ||
*/ | ||
!function(t,n){"undefined"==typeof module?t.Trie=n():module.exports=n()}(this,function(){function t(n,i){n&&n.length?(this.value=n[0],1===n.length?(this.index=i,this.children=[]):this.children=[new t(n.substr(1),i)]):(this.value=null,this.children=[])}function n(n){var i=this;this.words=[],this.root=new t,n&&n.forEach(function(t){return i.addWord(t)})}t.prototype.getChild=function(t){if(void 0!==t){var n=this.children.filter(function(n){return n.value===t});return n.length?n[0]:void 0}},t.prototype.addChild=function(t){this.children.push(t)},t.prototype.run=function(t){t(this),this.children.forEach(function(n){return n.run(t)})},n.prototype.addWord=function(n){if(n&&n.length){var i=this.words.length;this.words.push(n);for(var r=this.root,e=0,o=r;o=o.getChild(n.charAt(e));e++,r=o);e>=n.length?r.index=i:r.addChild(new t(n.substr(e),i))}},n.prototype.search=function(t){for(var n=this,i=[],r=this.root,e=0,o=r;o=o.getChild(t.charAt(e));e++,r=o);return e===t.length&&r.run(function(t){return void 0!==t.index&&i.push(n.words[t.index])}),i};var i=n;return i.config={},i.version="0.0.1",i}); | ||
!function(t,n){"undefined"==typeof module?t.Trie=n():module.exports=n()}(this,function(){function t(n,r){n&&n.length?(this.value=n[0],1===n.length?(this.addIndex(r),this.children=[]):this.children=[new t(n.substr(1),r)]):(this.value=null,this.children=[])}function n(n){var r=this;this.words=[],this.root=new t,n&&n.forEach(function(t){return r.addWord(t)})}t.prototype.getChild=function(t){if(void 0!==t){var n=this.children.filter(function(n){return n.value===t});return n.length?n[0]:void 0}},t.prototype.addChild=function(t){this.children.push(t)},t.prototype.addIndex=function(t){this.indexes?this.indexes.push(t):this.indexes=[t]},t.prototype.run=function(t){t(this),this.children.forEach(function(n){return n.run(t)})},n.prototype.addWord=function(n){if(n&&n.length){var r=this.words.length;this.words.push(n);var e=this.root,i=0;n.toLowerCase().split(" ").forEach(function(n){for(var o=e;o=o.getChild(n.charAt(i));i++,e=o);i>=n.length?e.addIndex(r):e.addChild(new t(n.substr(i),r))})}},n.prototype.search=function(t){var n=this,r=new Set;return t.toLowerCase().split(" ").forEach(function(t){for(var e=n.root,i=0,o=e;o=o.getChild(t.charAt(i));i++,e=o);i===t.length&&e.run(function(t){return t.indexes&&t.indexes.forEach(function(t){return r.add(n.words[t])})})}),Array.from(r)};var r=n;return r.config={},r.version="0.0.6",r}); |
{ | ||
"name": "@metacorp/trie", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Blazing fast, 1kb search library", | ||
@@ -5,0 +5,0 @@ "main": "dist/trie.min.js", |
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
14962
273