@metacorp/trie
Advanced tools
Comparing version 0.0.8 to 0.0.9
/** | ||
* Trie v0.0.7 | ||
* Trie v0.0.8 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -34,5 +34,7 @@ * Released under the MIT License | ||
function run(node, cb) { | ||
for(var k in node) { | ||
var keys = Object.keys(node) | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
cb(node) | ||
k !== '$' && run(node[k], cb) | ||
keys[i] !== '$' && run(node[keys[i]], cb) | ||
} | ||
@@ -46,3 +48,3 @@ } | ||
this.root = {} | ||
for(var i = 0; i < words.length; i++) { | ||
for(var i = 0, l = words.length; i < l; i++) { | ||
this$1.addWord(words[i]) | ||
@@ -53,6 +55,12 @@ } | ||
bTree.prototype.addWord = function (word) { | ||
var prev = this.root | ||
var j = 0 | ||
for(curr = prev; curr = curr[word.charAt(j)]; j++, prev = curr) {} | ||
bNode(prev, word.substr(j), this.words.length) | ||
var this$1 = this; | ||
var wordArray = word.toLowerCase().split(' ') | ||
for (var i = 0, l = wordArray.length; i < l; i++) { | ||
var word$1 = wordArray[i] | ||
var prev = this$1.root | ||
var j = 0 | ||
for(curr = prev; curr = curr[word$1.charAt(j)]; j++, prev = curr) {} | ||
bNode(prev, word$1.substr(j), this$1.words.length) | ||
} | ||
this.words.push(word) | ||
@@ -65,6 +73,10 @@ } | ||
var res = new Set() | ||
var prev = this.root | ||
var j = 0 | ||
for(curr = prev; curr = curr[str.charAt(j)]; j++, prev = curr) {} | ||
j === str.length && run(prev, function (node) { return node.$ && node.$.forEach(function (i) { return res.add(this$1.words[i]); }); }) | ||
var strArray = str.toLowerCase().split(' ') | ||
for (var i = 0, l = strArray.length; i < l; i++) { | ||
var str$1 = strArray[i] | ||
var prev = this$1.root | ||
var j = 0 | ||
for(curr = prev; curr = curr[str$1.charAt(j)]; j++, prev = curr) {} | ||
j === str$1.length && run(prev, function (node) { return node.$ && node.$.forEach(function (i) { return res.add(this$1.words[i]); }); }) | ||
} | ||
return Array.from(res) | ||
@@ -77,5 +89,5 @@ } | ||
Trie.version = "0.0.7" | ||
Trie.version = "0.0.8" | ||
return Trie; | ||
})); |
/** | ||
* Trie v0.0.7 | ||
* Trie v0.0.8 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -7,2 +7,2 @@ * Released under the MIT License | ||
*/ | ||
!function(r,t){"undefined"==typeof module?r.Trie=t():module.exports=t()}(this,function(){function r(t,o){for(var n in t)o(t),"$"!==n&&r(t[n],o)}function t(r){this.words=[],this.root={};for(var t=0;t<r.length;t++)this.addWord(r[t])}t.prototype.addWord=function(r){var t=this.root,o=0;for(curr=t;curr=curr[r.charAt(o)];o++,t=curr);!function(r,t,o){for(var n=r,u=0,c=null;c=t.charAt(u);u++,prev=n,n=n[c])n[c]={};n.$?n.$.push(o):n.$=[o]}(t,r.substr(o),this.words.length),this.words.push(r)},t.prototype.search=function(t){var o=this,n=new Set,u=this.root,c=0;for(curr=u;curr=curr[t.charAt(c)];c++,u=curr);return c===t.length&&r(u,function(r){return r.$&&r.$.forEach(function(r){return n.add(o.words[r])})}),Array.from(n)};var o=t;return o.config={},o.version="0.0.7",o}); | ||
!function(r,t){"undefined"==typeof module?r.Trie=t():module.exports=t()}(this,function(){function r(r,t,o){for(var n=r,e=0,u=null;u=t.charAt(e);e++,prev=n,n=n[u])n[u]={};return n.$?n.$.push(o):n.$=[o],r}function t(r,o){for(var n=Object.keys(r),e=0,u=n.length;e<u;e++)o(r),"$"!==n[e]&&t(r[n[e]],o)}function o(r){this.words=[],this.root={};for(var t=0,o=r.length;t<o;t++)this.addWord(r[t])}o.prototype.addWord=function(t){for(var o=t.toLowerCase().split(" "),n=0,e=o.length;n<e;n++){var u=o[n],c=this.root,s=0;for(curr=c;curr=curr[u.charAt(s)];s++,c=curr);r(c,u.substr(s),this.words.length)}this.words.push(t)},o.prototype.search=function(r){for(var o=this,n=new Set,e=r.toLowerCase().split(" "),u=0,c=e.length;u<c;u++){var s=e[u],i=o.root,f=0;for(curr=i;curr=curr[s.charAt(f)];f++,i=curr);f===s.length&&t(i,function(r){return r.$&&r.$.forEach(function(r){return n.add(o.words[r])})})}return Array.from(n)};var n=o;return n.config={},n.version="0.0.8",n}); |
/** | ||
* Trie v0.0.7 | ||
* Trie v0.0.8 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -101,5 +101,5 @@ * Released under the MIT License | ||
Trie1.version = "0.0.7" | ||
Trie1.version = "0.0.8" | ||
return Trie1; | ||
})); |
/** | ||
* Trie v0.0.7 | ||
* Trie v0.0.8 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -7,2 +7,2 @@ * Released under the MIT License | ||
*/ | ||
!function(t,n){"undefined"==typeof module?t.Trie1=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.7",r}); | ||
!function(t,n){"undefined"==typeof module?t.Trie1=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.8",r}); |
{ | ||
"name": "@metacorp/trie", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Blazing fast, 1kb search library", | ||
@@ -16,7 +16,7 @@ "main": "dist/trie.min.js", | ||
"search", | ||
"wade", | ||
"trie", | ||
"fast", | ||
"light" | ||
], | ||
"author": "Kabir Shah", | ||
"author": "Léopold Szabatura", | ||
"license": "MIT", | ||
@@ -23,0 +23,0 @@ "bugs": { |
@@ -19,5 +19,7 @@ const whitespaceRE = /\s+/g; | ||
function run(node, cb) { | ||
for(var k in node) { | ||
const keys = Object.keys(node) | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
cb(node) | ||
k !== '$' && run(node[k], cb) | ||
keys[i] !== '$' && run(node[keys[i]], cb) | ||
} | ||
@@ -29,3 +31,3 @@ } | ||
this.root = {} | ||
for(var i = 0; i < words.length; i++) { | ||
for(var i = 0, l = words.length; i < l; i++) { | ||
this.addWord(words[i]) | ||
@@ -37,3 +39,4 @@ } | ||
const wordArray = word.toLowerCase().split(' ') | ||
wordArray.forEach(word => { | ||
for (var i = 0, l = wordArray.length; i < l; i++) { | ||
const word = wordArray[i] | ||
var prev = this.root | ||
@@ -43,3 +46,3 @@ var j = 0 | ||
bNode(prev, word.substr(j), this.words.length) | ||
}) | ||
} | ||
this.words.push(word) | ||
@@ -51,3 +54,4 @@ } | ||
const strArray = str.toLowerCase().split(' ') | ||
strArray.forEach(str => { | ||
for (var i = 0, l = strArray.length; i < l; i++) { | ||
const str = strArray[i] | ||
var prev = this.root | ||
@@ -57,3 +61,3 @@ var j = 0 | ||
j === str.length && run(prev, node => node.$ && node.$.forEach(i => res.add(this.words[i]))) | ||
}) | ||
} | ||
return Array.from(res) | ||
@@ -60,0 +64,0 @@ } |
19060
444