@metacorp/trie
Advanced tools
Comparing version 0.1.0 to 0.1.1
/** | ||
* Trie v0.1.0 | ||
* Trie v0.1.1 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -124,5 +124,5 @@ * Released under the MIT License | ||
Trie.version = "0.1.0" | ||
Trie.version = "0.1.1" | ||
return Trie; | ||
})); |
/** | ||
* Trie v0.1.0 | ||
* Trie v0.1.1 | ||
* 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.0",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=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}); |
/** | ||
* Trie v0.1.0 | ||
* Trie v0.1.1 | ||
* Copyright 2017 Léopold Szabatura | ||
@@ -141,5 +141,5 @@ * Released under the MIT License | ||
Trie1.version = "0.1.0" | ||
Trie1.version = "0.1.1" | ||
return Trie1; | ||
})); |
/** | ||
* Trie v0.1.0 | ||
* Trie v0.1.1 | ||
* 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.0",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.1",h}); |
{ | ||
"name": "@metacorp/trie", | ||
"version": "0.1.0", | ||
"description": "Blazing fast, 1kb search library", | ||
"version": "0.1.1", | ||
"description": "Blazing fast, <1kb search library", | ||
"main": "dist/trie.min.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,10 +0,7 @@ | ||
# Wade | ||
# Trie | ||
Blazing fast 1kb search | ||
Blazing fast <1kb search | ||
[![Build Status](https://travis-ci.org/kbrsh/wade.svg?branch=master)](https://travis-ci.org/kbrsh/wade) | ||
[![Build Status](https://travis-ci.org/MetaCorp/trie.svg?branch=master)](https://travis-ci.org/MetaCorp/trie) | ||
[https://jsperf.com/metacorp-trie-search](https://jsperf.com/metacorp-trie-search) | ||
[https://jsperf.com/metacorp-trie-init](https://jsperf.com/metacorp-trie-init) | ||
### Installation | ||
@@ -29,3 +26,3 @@ | ||
```js | ||
const trie = new Trie(["Apple", "Lemon", "Orange", "Tomato"]); | ||
const trie = new Trie(['Apple', 'Lemon', 'Orange', 'Tomato']) | ||
``` | ||
@@ -36,8 +33,40 @@ | ||
```js | ||
trie.search("App"); | ||
trie.search('App') | ||
/* | ||
["Apple"] | ||
*/ | ||
``` | ||
Or add word to the data. | ||
```js | ||
trie.addWord('App') | ||
``` | ||
### About | ||
This repo is heavily inspired from [Wade](https://github.com/kbrsh/wade). | ||
It's a simpler version of it without the ability to score the results, but is therefore significantly faster, and allow to add word after initialisation. | ||
Trie comes in two implementations of [prefix tree](https://en.wikipedia.org/wiki/Trie). | ||
Version 1: | ||
- bundle size: 1.55kb(min) - 757b(gzip) | ||
- init speed: 12.24 Ops/sec | ||
- search speed: 79,000 Ops/sec | ||
Version 2: | ||
- bundle size: 1.94kb(min) - 831b(gzip) | ||
- init speed: 3.07 Ops/sec | ||
- search speed: 94,000 Ops/sec | ||
Check jsperf here: | ||
- [init](https://jsperf.com/metacorp-trie-init) | ||
- [searching](https://jsperf.com/metacorp-trie-search) | ||
And bundle size: [here](https://bundlephobia.com/result?p=@metacorp/trie) | ||
### Processors | ||
Wade uses a set of processors to preprocess data and search queries. By default, these will: | ||
Trie uses a set of processors to preprocess data and search queries. By default, these will: | ||
@@ -50,12 +79,12 @@ * Make everything lowercase | ||
You can easily modify the processors as they are available in `Wade.config.processors`, for example: | ||
You can easily modify the processors as they are available in `Trie.config.processors`, for example: | ||
```js | ||
// Don't preprocess at all | ||
Wade.config.processors = []; | ||
Trie.config.processors = [] | ||
// Add custom processor to remove periods | ||
Wade.config.processors.push(function(str) { | ||
return str.replace(/\./g, ""); | ||
}); | ||
Trie.config.processors.push(function(str) { | ||
return str.replace(/\./g, '') | ||
}) | ||
``` | ||
@@ -68,3 +97,3 @@ | ||
```js | ||
Trie.config.stopWords = [/* array of stop words */]; | ||
Trie.config.stopWords = [/* array of stop words */] | ||
``` | ||
@@ -75,11 +104,9 @@ | ||
```js | ||
Trie.config.punctuationRE = /[.!]/g; // should contain punctuation to remove | ||
Trie.config.punctuationRE = /[.!]/g // should contain punctuation to remove | ||
``` | ||
### Support | ||
### License | ||
Support Wade [on Patreon](https://patreon.com/kbrsh) to help sustain the development of the project. The maker of the project works on open source for free. If you or your company depend on this project, then it makes sense to donate to ensure that the project is maintained. | ||
Licensed under the [MIT License](https://github.com/MetaCorp/trie/blob/master/LICENSE) | ||
### License | ||
Licensed under the [MIT License](https://kbrsh.github.io/license) by [Kabir Shah](https://kabir.ml) | ||
Copyright (c) Meta l.szabatura@gmail.com |
Sorry, the diff of this file is not supported yet
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
24811
108