New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@metacorp/trie

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metacorp/trie - npm Package Compare versions

Comparing version 0.0.6 to 0.0.7

dist/trie1.js

86

dist/trie.js
/**
* Trie v0.0.6
* Trie v0.0.7
* Copyright 2017 Léopold Szabatura

@@ -21,36 +21,19 @@ * Released under the MIT License

function bNode(word, index) {
if (!word || !word.length) {
this.value = null
this.children = []
} else {
this.value = word[0]
if (word.length === 1) {
this.addIndex(index)
this.children = []
} else
{ this.children = [new bNode(word.substr(1), index)] }
function bNode(root, word, index) {
var curr = root
for(var i = 0, c = null; c = word.charAt(i); i++, prev = curr, curr = curr[c]) {
curr[c] = {}
}
}
bNode.prototype.getChild = function (val) {
if (val === undefined) { return }
var ret = this.children.filter(function (n) { return n.value === val; })
return ret.length ? ret[0] : undefined
}
bNode.prototype.addChild = function (node) {
this.children.push(node)
}
bNode.prototype.addIndex = function (index) {
if (this.indexes)
{ this.indexes.push(index) }
if (!curr.$)
{ curr.$ = [index] }
else
{ this.indexes = [index] }
{ curr.$.push(index) }
return root
}
bNode.prototype.run = function (cb) {
cb(this)
this.children.forEach(function (node) { return node.run(cb); })
function run(node, cb) {
for(var k in node) {
cb(node)
k !== '$' && run(node[k], cb)
}
}

@@ -62,20 +45,14 @@

this.words = []
this.root = new bNode()
words && words.forEach(function (word) { return this$1.addWord(word); })
this.root = {}
for(var i = 0; i < words.length; i++) {
this$1.addWord(words[i])
}
}
bTree.prototype.addWord = function (word) {
if (!word || !word.length) { return }
var index = this.words.length
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)
this.words.push(word)
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)) }
})
}

@@ -86,13 +63,8 @@

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)
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]); }); })
return Array.from(res)
}

@@ -104,5 +76,5 @@

Trie.version = "0.0.6"
Trie.version = "0.0.7"
return Trie;
}));
/**
* Trie v0.0.6
* Trie v0.0.7
* 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,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});
!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});

@@ -48,31 +48,33 @@ const gulp = require("gulp");

const bubleConfig = {
namedFunctionExpressions: false,
transforms: {
arrow: true,
classes: false,
collections: false,
computedProperty: false,
conciseMethodProperty: false,
constLoop: false,
dangerousForOf: false,
dangerousTaggedTemplateString: false,
defaultParameter: false,
destructuring: false,
forOf: false,
generator: false,
letConst: true,
modules: false,
numericLiteral: false,
parameterDestructuring: false,
reservedProperties: false,
spreadRest: false,
stickyRegExp: false,
templateString: true,
unicodeRegExp: false
}
}
gulp.task("transpile", function() {
return gulp.src(["./src/index.js"])
.pipe(gulpBuble({
namedFunctionExpressions: false,
transforms: {
arrow: true,
classes: false,
collections: false,
computedProperty: false,
conciseMethodProperty: false,
constLoop: false,
dangerousForOf: false,
dangerousTaggedTemplateString: false,
defaultParameter: false,
destructuring: false,
forOf: false,
generator: false,
letConst: true,
modules: false,
numericLiteral: false,
parameterDestructuring: false,
reservedProperties: false,
spreadRest: false,
stickyRegExp: false,
templateString: true,
unicodeRegExp: false
}
}))
.pipe(concat("trie.js"))
return gulp.src(["./src/trie.js"])
.pipe(gulpBuble(bubleConfig))
// .pipe(concat("trie.js"))
.pipe(gulp.dest("./dist/"));

@@ -103,3 +105,32 @@ });

gulp.task("transpile1", function () {
return gulp.src(["./src/trie1.js"])
.pipe(gulpBuble(bubleConfig))
// .pipe(concat("trie.js"))
.pipe(gulp.dest("./dist/"));
});
gulp.task("build1", ["transpile1"], function() {
return gulp.src(["./src/wrapper1.js"])
.pipe(include())
.pipe(concat("trie1.js"))
.pipe(header(comment + '\n'))
.pipe(replace("__VERSION__", pkg.version))
.pipe(size())
.pipe(gulp.dest("./dist/"));
});
gulp.task("minify1", ["build1"], function() {
return gulp.src(["./dist/trie1.js"])
.pipe(uglify())
.pipe(header(comment))
.pipe(size())
.pipe(size({
gzip: true
}))
.pipe(concat("trie1.min.js"))
.pipe(gulp.dest("./dist/"));
});
gulp.task("test", function() {

@@ -111,2 +142,2 @@ gulp.src("./test/**/*.js")

// Default task
gulp.task("default", ["build", "minify"]);
gulp.task("default", ["build", "minify", "build1", "minify1"]);
{
"name": "@metacorp/trie",
"version": "0.0.6",
"version": "0.0.7",
"description": "Blazing fast, 1kb search library",

@@ -5,0 +5,0 @@ "main": "dist/trie.min.js",

@@ -15,3 +15,3 @@ # Wade

```sh
npm install wade
npm install @metacorp/trie
```

@@ -22,3 +22,3 @@

```html
<script src="https://unpkg.com/wade"></script>
<script src="https://unpkg.com/@metacorp/trie"></script>
```

@@ -28,45 +28,14 @@

Initialize Wade with an array of data.
Initialize Trie with an array of data.
```js
const search = Wade(["Apple", "Lemon", "Orange", "Tomato"]);
const trie = new Trie(["Apple", "Lemon", "Orange", "Tomato"]);
```
Now you can search for a query within the data, and Wade will return results. Each result will include the index of the item in the data it corresponds to along with a score depending on the relevance of the query to the result.
Now you can search for a query within the data, and Trie will return results.
```js
search("App");
/*
[{
index: 0,
score: 1.25
}]
*/
trie.search("App");
```
Combined with libraries like [Moon](http://moonjs.ga), you can create a [real-time search](http://moonjs.ga/examples/search/index.html).
### Loading/Saving Data
To save data as an object, use `Wade.save` on your search function, and then use these later when initializing Wade.
For example:
```js
// Create the initial search function
const search = Wade(["Apple", "Lemon", "Orange", "Tomato"]);
const instance = Wade.save(search);
// Save `instance`
```
Later, you can get the same search function without having Wade recreate an index every time by doing:
```js
// Retrieve `instance`, then
const search = Wade(instance);
```
`instance` can be saved to a file using using `JSON.stringify()` and loaded with `JSON.parse()`.
### Processors

@@ -99,3 +68,3 @@

```js
Wade.config.stopWords = [/* array of stop words */];
Trie.config.stopWords = [/* array of stop words */];
```

@@ -106,16 +75,5 @@

```js
Wade.config.punctuationRE = /[.!]/g; // should contain punctuation to remove
Trie.config.punctuationRE = /[.!]/g; // should contain punctuation to remove
```
### Algorithm
First, an index is generated from the data. When performing a search, the following happens:
* The search query is processed.
* The search query is tokenized into terms.
* Each term except the last is searched for exactly and scores for each item in the data are updated according to the relevance of the term to the data.
* The last keyword is treated as a prefix, and Wade performs a depth-first search and updates the score for all data prefixed with this term using the relevance weight for the term. This allows for searching as a user types.
In-depth explanations of the algorithm are available on the [blog post](https://blog.kabir.ml/posts/inside-wade.html) and [pdf](https://github.com/kbrsh/wade/blob/master/Wade.pdf).
### Support

@@ -122,0 +80,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc