-
-
Save paulvstheworld/9560591 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // lol global | |
| var index = {}; | |
| function add(content, page) { | |
| words = content.trim().split(" "); | |
| index[page] = {}; | |
| for (var i = 0, len = words.length; i<len; i++) { | |
| index[page][words[i]] = true; | |
| } | |
| } | |
| function search(query) { | |
| var result = []; | |
| words = query.trim().split(" "); | |
| for (var key in index) { | |
| if (containsAll(words, index[key])) { | |
| result.push(key); | |
| } | |
| } | |
| return result; | |
| } | |
| function containsAll(words, page) { | |
| for (var i = 0, len = words.length; i<len; i++) { | |
| if (!!page[words[i]]) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| // TESTS | |
| add("puppies kittens", 9); | |
| add("kittens", 10); | |
| add("puppies", 8); | |
| add("dog cat", 6); | |
| console.log("printing index"); | |
| console.log(index); | |
| console.log("just puppies:") | |
| console.log(search(" puppies")); | |
| console.log("just kittens"); | |
| console.log(search("kittens ")); | |
| console.log("puppies and kittens:"); | |
| console.log(search("puppies kittens")); | |
| console.log(search("hot dog")); | |
| console.log(search("cat dog")); | |
| console.log(search("puppies")); | |
| console.log("OLD TESTS better return true or we're really in trouble :c"); | |
| console.log(containsAll(["my","milkshake"],{my: true, milkshake: true, brings: true, all: true})); | |
| console.log(containsAll(["milkshake"],{milkshake: "milkshake"})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment