Skip to content

Commit

Permalink
trie-ing
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Aug 14, 2015
0 parents commit df55d7a
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2010-2015 Google, Inc. http://angularjs.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

The trie implementation from https://github.com/marccampbell/node-autocomplete packaged in a way I can use it in the browser.

See LICENSE for original copyright holder.
213 changes: 213 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
Trie = function() {
this.words = 0;
this.prefixes = 0;
this.value = "";
this.children = [];
};


/**
* Add a value to the trie
*
* @param {String} value
* @param {Number} index (optional)
*/
Trie.prototype.addValue = function(item, index) {
if (!index) {
index = 0;
}
if(item === null) {
return;
}

var isObject = false;

if(typeof item === 'object') {
isObject = true;
}

if (isObject && item.key.length === 0) {
return;
}
else if(!isObject && item.length === 0) {
return;
}

if ((isObject && index === item.key.length) || (!isObject && index === item.length)) {
this.words++;
this.value = isObject ? item.value : item;
return;
}

this.prefixes++;
var key = isObject ? item.key[index] : item[index];
if (this.children[key] === undefined) {
this.children[key] = new Trie();
}
var child = this.children[key];
child.addValue(item, index + 1);
};

/**
* Remove a value form the trie
*
* @param {String} value
* @param {Number} index (optional)
*/
Trie.prototype.removeValue = function(item, index) {
if (!index) {
index = 0;
}

if (item.length === 0) {
return;
}

if (index === item.length) {
this.words--;
this.value="";
}
else {
this.prefixes--;
var key = item[index];
var child = this.children[key];
if(child) child.removeValue(item, index + 1);
// to remove a node, we need remove it from parent's children array
if(index === (item.length -1)) {
if(Object.keys(child.children).length === 0) {// only remove when there is no children
delete this.children[key];
}
}
}
};

/** Get the count of instances of a word in the entire trie
*
* @param {String} word
* @param {Number} index (optional)
*/
Trie.prototype.wordCount = function(value, index) {
if (!index) {
index = 0;
}

if (value.length === 0) {
return 0;
}

if (index === value.length) {
return this.words;
} else {
var key = value[index];
var child = this.children[key];
if (child) {
return child.wordCount(value, index + 1);
} else {
return 0;
}
}
};

/** Get the count of instances of a prefix in the enture trie
*
* @param {String} prefix
* @param {Number} index
*/
Trie.prototype.prefixCount = function(prefix, index) {
if (!index) {
index = 0;
}

if (prefix.length === 0) {
return 0;
}

if (index === prefix.length) {
return this.prefixes;
} else {
var key = prefix[index];
var child = this.children[key];
if (child) {
return child.prefixCount(prefix, index + 1);
} else {
return 0;
}
}
};

/**
* Check if a word exists in the trie
*
* @param {String} value
*/
Trie.prototype.wordExists = function(value) {
if (value.length === 0) {
return false;
}

return this.wordCount(value) > 0;
};

/**
* Return all words with a prefix
*
* @param {String} prefix
*/
Trie.prototype.allChildWords = function(prefix) {
if (!prefix) {
prefix = '';
}

var words = [];
if (this.words > 0) {
if(this.value.lenth === 0) {
var tmp = new Object();
tmp.key = prefix;
tmp.value = prefix
words.push(tmp);
}
else {
var tmp = new Object();
tmp.key = prefix;
tmp.value = this.value;
words.push(tmp);
}
}

for (key in this.children) {
var child = this.children[key];
words = words.concat(child.allChildWords(prefix + key));
}

return words;
}

/**
* Perform an autocomplete match
*
* @param {String} prefix
* @param {Number} index
*/
Trie.prototype.autoComplete = function(prefix, index) {
if (!index) {
index = 0;
}

if (prefix.length === 0) {
return [];
}

var key = prefix[index];
var child = this.children[key];
if (!child) {
return [];
} else {
if (index === prefix.length - 1) {
return child.allChildWords(prefix);
} else {
return child.autoComplete(prefix, index + 1);
}
}
};

module.exports = Trie;
60 changes: 60 additions & 0 deletions npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
0 info it worked if it ends with ok
1 verbose cli [ '/Users/conrad/.nvm/v0.10.38/bin/node',
1 verbose cli '/Users/conrad/.nvm/v0.10.38/bin/npm',
1 verbose cli 'publish' ]
2 info using npm@1.4.28
3 info using node@v0.10.38
4 verbose node symlink /Users/conrad/.nvm/v0.10.38/bin/node
5 verbose publish [ '.' ]
6 verbose cache add [ '.', null ]
7 verbose cache add name=undefined spec="." args=[".",null]
8 verbose parsed url { protocol: null,
8 verbose parsed url slashes: null,
8 verbose parsed url auth: null,
8 verbose parsed url host: null,
8 verbose parsed url port: null,
8 verbose parsed url hostname: null,
8 verbose parsed url hash: null,
8 verbose parsed url search: null,
8 verbose parsed url query: null,
8 verbose parsed url pathname: '.',
8 verbose parsed url path: '.',
8 verbose parsed url href: '.' }
9 silly lockFile 3a52ce78- .
10 verbose lock . /Users/conrad/.npm/3a52ce78-.lock
11 verbose tar pack [ '/Users/conrad/.npm/trie-ing/0.0.1/package.tgz', '.' ]
12 verbose tarball /Users/conrad/.npm/trie-ing/0.0.1/package.tgz
13 verbose folder .
14 info prepublish trie-ing@0.0.1
15 silly lockFile 1f1177db-tar tar://.
16 verbose lock tar://. /Users/conrad/.npm/1f1177db-tar.lock
17 silly lockFile cc53d201-d-npm-trie-ing-0-0-1-package-tgz tar:///Users/conrad/.npm/trie-ing/0.0.1/package.tgz
18 verbose lock tar:///Users/conrad/.npm/trie-ing/0.0.1/package.tgz /Users/conrad/.npm/cc53d201-d-npm-trie-ing-0-0-1-package-tgz.lock
19 silly lockFile 1f1177db-tar tar://.
20 silly lockFile 1f1177db-tar tar://.
21 silly lockFile cc53d201-d-npm-trie-ing-0-0-1-package-tgz tar:///Users/conrad/.npm/trie-ing/0.0.1/package.tgz
22 silly lockFile cc53d201-d-npm-trie-ing-0-0-1-package-tgz tar:///Users/conrad/.npm/trie-ing/0.0.1/package.tgz
23 silly lockFile bd05b836-onrad-npm-trie-ing-0-0-1-package /Users/conrad/.npm/trie-ing/0.0.1/package
24 verbose lock /Users/conrad/.npm/trie-ing/0.0.1/package /Users/conrad/.npm/bd05b836-onrad-npm-trie-ing-0-0-1-package.lock
25 silly lockFile bd05b836-onrad-npm-trie-ing-0-0-1-package /Users/conrad/.npm/trie-ing/0.0.1/package
26 silly lockFile bd05b836-onrad-npm-trie-ing-0-0-1-package /Users/conrad/.npm/trie-ing/0.0.1/package
27 silly lockFile 3a52ce78- .
28 silly lockFile 3a52ce78- .
29 silly publish { name: 'trie-ing',
29 silly publish version: '0.0.1',
29 silly publish description: 'The trie from node-autocompleter in a form that can be used in browser',
29 silly publish main: 'index.js',
29 silly publish readme: 'ERROR: No README data found!',
29 silly publish _id: 'trie-ing@0.0.1',
29 silly publish scripts: {},
29 silly publish _shasum: 'aadd7bc08f5d26a8b64acf96a40f711e98f5a966',
29 silly publish _from: '.' }
30 error need auth auth and email required for publishing
30 error need auth You need to authorize this machine using `npm adduser`
31 error System Darwin 14.3.0
32 error command "/Users/conrad/.nvm/v0.10.38/bin/node" "/Users/conrad/.nvm/v0.10.38/bin/npm" "publish"
33 error cwd /0/js/trie-ing
34 error node -v v0.10.38
35 error npm -v 1.4.28
36 error code ENEEDAUTH
37 verbose exit [ 1, true ]
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "trie-ing",
"version": "0.0.1",
"description": "The trie from node-autocompleter in a form that can be used in browser",
"main": "index.js"
}

0 comments on commit df55d7a

Please sign in to comment.