@metacorp/trie
Advanced tools
Comparing version 0.1.1 to 0.1.2
/** | ||
* Trie v0.1.1 | ||
* Trie v0.1.2 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -60,3 +60,3 @@ * Released under the MIT License | ||
var curr = root | ||
for(var i = 0, c = null; c = word.charAt(i); i++, prev = curr, curr = curr[c]) { | ||
for (var i = 0, c = null; c = word.charAt(i); i++, prev = curr, curr = curr[c]) { | ||
curr[c] = {} | ||
@@ -72,8 +72,4 @@ } | ||
function run(node, cb) { | ||
var keys = Object.keys(node) | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
cb(node) | ||
keys[i] !== '$' && run(node[keys[i]], cb) | ||
} | ||
for (var i = 0, keys = Object.keys(node); key = keys[i]; i++) | ||
{ key !== '$' ? run(node[key], cb) : cb(node.$) } | ||
} | ||
@@ -86,3 +82,3 @@ | ||
this.root = {} | ||
for(var i = 0, l = words.length; i < l; i++) { | ||
for (var i = 0, l = words.length; i < l; i++) { | ||
this$1.addWord(words[i]) | ||
@@ -101,3 +97,3 @@ } | ||
var j = 0 | ||
for(curr = prev; curr = curr[word$1.charAt(j)]; j++, prev = curr) {} | ||
for (curr = prev; curr = curr[word$1.charAt(j)]; j++, prev = curr) {} | ||
bNode(prev, word$1.substr(j), this$1.words.length) | ||
@@ -110,3 +106,36 @@ } | ||
var this$1 = this; | ||
// Missing first word | ||
var strArray = processEntry(str) | ||
var l = strArray.length | ||
if (!l) { return [] } | ||
var res = new Set() | ||
var addSet = function ($) { | ||
for (var i = 0, l = $.length; i < l; i++) { | ||
res.add(this$1.words[$[i]]) | ||
} | ||
} | ||
for (var i = 0; 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, addSet) | ||
} | ||
return Array.from(res) | ||
} | ||
function run2(node) { | ||
if (!node.$s) { | ||
var set = new Set()// DOUBLON ? => SET | ||
for (var i = 0, keys = Object.keys(node); key = keys[i]; i++) { | ||
(key !== '$' ? run2(node[key]) : node.$).forEach(function (e) { return set.add(e); }) | ||
} | ||
node.$s = Array.from(set) | ||
} | ||
return node.$s | ||
} | ||
bTree.prototype.search2 = function (str) { | ||
var this$1 = this; | ||
var strArray = processEntry(str) | ||
@@ -119,4 +148,4 @@ if (!strArray.length) { return [] } | ||
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]); }); }) | ||
for (curr = prev; curr = curr[str$1.charAt(j)]; j++ , prev = curr) { } | ||
j === str$1.length && run2(prev).forEach(function (i) { return res.add(this$1.words[i]); }) | ||
} | ||
@@ -126,2 +155,5 @@ return Array.from(res) | ||
bTree.prototype.save = function () { | ||
} | ||
var Trie = bTree | ||
@@ -131,5 +163,5 @@ | ||
Trie.version = "0.1.1" | ||
Trie.version = "0.1.2" | ||
return Trie; | ||
})); |
/** | ||
* Trie v0.1.1 | ||
* Trie v0.1.2 | ||
* 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(r,t,n){for(var o=r,e=0,u=null;u=t.charAt(e);e++,prev=o,o=o[u])o[u]={};return o.$?o.$.push(n):o.$=[n],r}function t(r,n){for(var o=Object.keys(r),e=0,u=o.length;e<u;e++)n(r),"$"!==o[e]&&t(r[o[e]],n)}function n(r){this.words=[],this.root={};for(var t=0,n=r.length;t<n;t++)this.addWord(r[t])}var o=/\s+/g,e={stopWords:[],punctuationRE:/[!"',.:;?]/g,processors:[function(r){return r.toLowerCase()},function(r){return r.replace(e.punctuationRE,"")},function(r){for(var t=e.stopWords,n=u(r),o=n.length;0!=o--;)-1!==t.indexOf(n[o])&&n.splice(o,1);return n.join(" ")}]},u=function(r){if(!r||!r.length)return[];var t=r.split(o);return 0===t[0].length&&t.shift(),0===t[t.length-1].length&&t.pop(),t},s=function(r){if(r.length)for(var t=0,n=e.processors.length;t<n;t++)r=e.processors[t](r);return u(r)};n.prototype.addWord=function(t){var n=s(t);if(n.length){for(var o=0,e=n.length;o<e;o++){var u=n[o],i=this.root,c=0;for(curr=i;curr=curr[u.charAt(c)];c++,i=curr);r(i,u.substr(c),this.words.length)}this.words.push(t)}},n.prototype.search=function(r){var n=this,o=s(r);if(!o.length)return[];for(var e=new Set,u=0,i=o.length;u<i;u++){var c=o[u],f=n.root,h=0;for(curr=f;curr=curr[c.charAt(h)];h++,f=curr);h===c.length&&t(f,function(r){return r.$&&r.$.forEach(function(r){return e.add(n.words[r])})})}return Array.from(e)};var i=n;return i.config=e,i.version="0.1.1",i}); | ||
!function(r,t){"undefined"==typeof module?r.Trie=t():module.exports=t()}(this,function(){function r(r,t,n){for(var o=r,e=0,u=null;u=t.charAt(e);e++,prev=o,o=o[u])o[u]={};return o.$?o.$.push(n):o.$=[n],r}function t(r,n){for(var o=0,e=Object.keys(r);key=e[o];o++)"$"!==key?t(r[key],n):n(r.$)}function n(r){this.words=[],this.root={};for(var t=0,n=r.length;t<n;t++)this.addWord(r[t])}function o(r){if(!r.$s){for(var t=new Set,n=0,e=Object.keys(r);key=e[n];n++)("$"!==key?o(r[key]):r.$).forEach(function(r){return t.add(r)});r.$s=Array.from(t)}return r.$s}var e=/\s+/g,u={stopWords:[],punctuationRE:/[!"',.:;?]/g,processors:[function(r){return r.toLowerCase()},function(r){return r.replace(u.punctuationRE,"")},function(r){for(var t=u.stopWords,n=c(r),o=n.length;0!=o--;)-1!==t.indexOf(n[o])&&n.splice(o,1);return n.join(" ")}]},c=function(r){if(!r||!r.length)return[];var t=r.split(e);return 0===t[0].length&&t.shift(),0===t[t.length-1].length&&t.pop(),t},f=function(r){if(r.length)for(var t=0,n=u.processors.length;t<n;t++)r=u.processors[t](r);return c(r)};n.prototype.addWord=function(t){var n=f(t);if(n.length){for(var o=0,e=n.length;o<e;o++){var u=n[o],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)}},n.prototype.search=function(r){var n=this,o=f(r),e=o.length;if(!e)return[];for(var u=new Set,c=function(r){for(var t=0,o=r.length;t<o;t++)u.add(n.words[r[t]])},s=0;s<e;s++){var i=o[s],a=n.root,h=0;for(curr=a;curr=curr[i.charAt(h)];h++,a=curr);h===i.length&&t(a,c)}return Array.from(u)},n.prototype.search2=function(r){var t=this,n=f(r);if(!n.length)return[];for(var e=new Set,u=0,c=n.length;u<c;u++){var s=n[u],i=t.root,a=0;for(curr=i;curr=curr[s.charAt(a)];a++,i=curr);a===s.length&&o(i).forEach(function(r){return e.add(t.words[r])})}return Array.from(e)},n.prototype.save=function(){};var s=n;return s.config=u,s.version="0.1.2",s}); |
/** | ||
* Trie v0.1.1 | ||
* Trie v0.1.2 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -141,5 +141,5 @@ * Released under the MIT License | ||
Trie1.version = "0.1.1" | ||
Trie1.version = "0.1.2" | ||
return Trie1; | ||
})); |
/** | ||
* Trie v0.1.1 | ||
* Trie v0.1.2 | ||
* 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)})}var r=/\s+/g,e={stopWords:[],punctuationRE:/[!"',.:;?]/g,processors:[function(t){return t.toLowerCase()},function(t){return t.replace(e.punctuationRE,"")},function(t){for(var n=e.stopWords,r=o(t),i=r.length;0!=i--;)-1!==n.indexOf(r[i])&&r.splice(i,1);return r.join(" ")}]},o=function(t){if(!t||!t.length)return[];var n=t.split(r);return 0===n[0].length&&n.shift(),0===n[n.length-1].length&&n.pop(),n},i=function(t){if(t.length)for(var n=0,r=e.processors.length;n<r;n++)t=e.processors[n](t);return o(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){var r=i(n);if(r.length){for(var e=this.words.length,o=this.root,h=0,s=(h=0,r.length);h<s;h++){for(var u=r[h],d=o;d=d.getChild(u.charAt(h));h++,o=d);h>=u.length?o.addIndex(e):o.addChild(new t(u.substr(h),e))}this.words.push(n)}},n.prototype.search=function(t){var n=this,r=i(t);if(!r.length)return[];for(var e=new Set,o=0,h=r.length;o<h;o++){for(var s=r[o],u=n.root,d=(o=0,u);d=d.getChild(s.charAt(o));o++,u=d);o===s.length&&u.run(function(t){return t.indexes&&t.indexes.forEach(function(t){return e.add(n.words[t])})})}return Array.from(e)};var h=n;return h.config=e,h.version="0.1.1",h}); | ||
!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)})}var r=/\s+/g,e={stopWords:[],punctuationRE:/[!"',.:;?]/g,processors:[function(t){return t.toLowerCase()},function(t){return t.replace(e.punctuationRE,"")},function(t){for(var n=e.stopWords,r=o(t),i=r.length;0!=i--;)-1!==n.indexOf(r[i])&&r.splice(i,1);return r.join(" ")}]},o=function(t){if(!t||!t.length)return[];var n=t.split(r);return 0===n[0].length&&n.shift(),0===n[n.length-1].length&&n.pop(),n},i=function(t){if(t.length)for(var n=0,r=e.processors.length;n<r;n++)t=e.processors[n](t);return o(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){var r=i(n);if(r.length){for(var e=this.words.length,o=this.root,h=0,s=(h=0,r.length);h<s;h++){for(var u=r[h],d=o;d=d.getChild(u.charAt(h));h++,o=d);h>=u.length?o.addIndex(e):o.addChild(new t(u.substr(h),e))}this.words.push(n)}},n.prototype.search=function(t){var n=this,r=i(t);if(!r.length)return[];for(var e=new Set,o=0,h=r.length;o<h;o++){for(var s=r[o],u=n.root,d=(o=0,u);d=d.getChild(s.charAt(o));o++,u=d);o===s.length&&u.run(function(t){return t.indexes&&t.indexes.forEach(function(t){return e.add(n.words[t])})})}return Array.from(e)};var h=n;return h.config=e,h.version="0.1.2",h}); |
{ | ||
"name": "@metacorp/trie", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Blazing fast, <1kb search library", | ||
@@ -5,0 +5,0 @@ "main": "dist/trie.min.js", |
@@ -34,3 +34,3 @@ # Trie | ||
/* | ||
["Apple"] | ||
['Apple'] | ||
*/ | ||
@@ -42,3 +42,3 @@ ``` | ||
```js | ||
trie.addWord('App') | ||
trie.addWord('Kiwi') | ||
``` | ||
@@ -65,3 +65,3 @@ | ||
- [init](https://jsperf.com/metacorp-trie-init) | ||
- [searching](https://jsperf.com/metacorp-trie-search) | ||
- [search](https://jsperf.com/metacorp-trie-search) | ||
@@ -68,0 +68,0 @@ And bundle size: [here](https://bundlephobia.com/result?p=@metacorp/trie) |
@@ -45,3 +45,3 @@ const whitespaceRE = /\s+/g | ||
var curr = root | ||
for(var i = 0, c = null; c = word.charAt(i); i++, prev = curr, curr = curr[c]) { | ||
for (var i = 0, c = null; c = word.charAt(i); i++, prev = curr, curr = curr[c]) { | ||
curr[c] = {} | ||
@@ -57,8 +57,4 @@ } | ||
function run(node, cb) { | ||
const keys = Object.keys(node) | ||
for (var i = 0, l = keys.length; i < l; i++) { | ||
cb(node) | ||
keys[i] !== '$' && run(node[keys[i]], cb) | ||
} | ||
for (var i = 0, keys = Object.keys(node); key = keys[i]; i++) | ||
key !== '$' ? run(node[key], cb) : cb(node.$) | ||
} | ||
@@ -69,3 +65,3 @@ | ||
this.root = {} | ||
for(var i = 0, l = words.length; i < l; i++) { | ||
for (var i = 0, l = words.length; i < l; i++) { | ||
this.addWord(words[i]) | ||
@@ -82,3 +78,3 @@ } | ||
var j = 0 | ||
for(curr = prev; curr = curr[word.charAt(j)]; j++, prev = curr) {} | ||
for (curr = prev; curr = curr[word.charAt(j)]; j++, prev = curr) {} | ||
bNode(prev, word.substr(j), this.words.length) | ||
@@ -89,4 +85,35 @@ } | ||
bTree.prototype.search = function (str) { | ||
bTree.prototype.search = function (str) {// Missing first word | ||
const strArray = processEntry(str) | ||
const l = strArray.length | ||
if (!l) return [] | ||
const res = new Set() | ||
const addSet = $ => { | ||
for (var i = 0, l = $.length; i < l; i++) { | ||
res.add(this.words[$[i]]) | ||
} | ||
} | ||
for (var i = 0; i < l; i++) { | ||
const str = strArray[i] | ||
var prev = this.root | ||
var j = 0 | ||
for (curr = prev; curr = curr[str.charAt(j)]; j++, prev = curr) {} | ||
j === str.length && run(prev, addSet) | ||
} | ||
return Array.from(res) | ||
} | ||
function run2(node) { | ||
if (!node.$s) { | ||
var set = new Set()// DOUBLON ? => SET | ||
for (var i = 0, keys = Object.keys(node); key = keys[i]; i++) { | ||
(key !== '$' ? run2(node[key]) : node.$).forEach(e => set.add(e)) | ||
} | ||
node.$s = Array.from(set) | ||
} | ||
return node.$s | ||
} | ||
bTree.prototype.search2 = function (str) { | ||
const strArray = processEntry(str) | ||
if (!strArray.length) return [] | ||
@@ -98,4 +125,4 @@ const res = new Set() | ||
var j = 0 | ||
for(curr = prev; curr = curr[str.charAt(j)]; j++, prev = curr) {} | ||
j === str.length && run(prev, node => node.$ && node.$.forEach(i => res.add(this.words[i]))) | ||
for (curr = prev; curr = curr[str.charAt(j)]; j++ , prev = curr) { } | ||
j === str.length && run2(prev).forEach(i => res.add(this.words[i])) | ||
} | ||
@@ -105,2 +132,5 @@ return Array.from(res) | ||
bTree.prototype.save = function () { | ||
} | ||
const Trie = bTree | ||
@@ -107,0 +137,0 @@ |
27035
644