initial commit

This commit is contained in:
Mahdi Dibaiee
2015-07-26 11:57:50 +04:30
commit 0340c4bd44
16 changed files with 860 additions and 0 deletions

34
src/index.js Normal file
View File

@ -0,0 +1,34 @@
const trie = new Trie();
const divs = document.querySelectorAll('div');
const colors = {};
for (let div of divs) {
colors[div.className] = div;
trie.add(div.className);
}
const input = document.querySelector('input');
input.addEventListener('keyup', () => {
divs.hide();
const nodes = trie.all(input.value);
if (!nodes) return;
for (let node of nodes) {
const color = colors[node.value];
if (!color) continue;
color.classList.remove('hidden');
}
});
NodeList.prototype.hide = function() {
for (let node of this) {
node.classList.add('hidden');
}
};

5
src/libs/d3.js vendored Normal file

File diff suppressed because one or more lines are too long

54
src/trie.js Normal file
View File

@ -0,0 +1,54 @@
class Node {
constructor(value = '', parent) {
this.name = value;
this.children = [];
this.parent = parent;
}
}
class Trie {
constructor() {
this.root = new Node();
}
add(value, parent = this.root) {
for (let i = 0, len = value.length; i < len; i++) {
if (!parent.children) parent.children = [];
let node = parent.children.find(child => child.name[i] === value[i]);
if (!node) {
node = new Node(value.slice(0, i + 1), parent.name);
parent.children.push(node);
}
parent = node;
}
return parent;
}
find(value, parent = this.root) {
for (let i = 0, len = value.length; i < len; i++) {
parent = parent.children.find(child => child.name[i] === value[i]);
if (!parent) return null;
}
return parent;
}
all(search) {
let node = this.find(search);
if (!node) return null;
let all = [node];
node.children.forEach(function addToAll(child) {
all.push(child);
child.children.forEach(addToAll);
});
return all;
}
}