initial commit
This commit is contained in:
34
src/index.js
Normal file
34
src/index.js
Normal 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
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
54
src/trie.js
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user