|
1 | | -const { Trie, TrieNode } = require('./index'); |
| 1 | +const { Trie, TrieNode } = require("./index"); |
2 | 2 |
|
3 | 3 | let isEndOfWord, children; |
4 | 4 | let _root; |
5 | 5 |
|
6 | | -describe('TrieNode and Trie', () => { |
7 | | - test('TrieNode hold two properties, a boolean value for end of word and another for children', () => { |
8 | | - const node = new TrieNode(); |
9 | | - const keys = Object.keys(node); |
10 | | - const valuesTypeOf = Object.values(node).map((v) => typeof v); |
| 6 | +describe("TrieNode and Trie", () => { |
| 7 | + test("TrieNode hold two properties, a boolean value for end of word and another for children", () => { |
| 8 | + const node = new TrieNode(); |
| 9 | + const keys = Object.keys(node); |
| 10 | + const valuesTypeOf = Object.values(node).map(v => typeof v); |
11 | 11 |
|
12 | | - expect(keys.length).toEqual(2); |
13 | | - expect(valuesTypeOf.includes('boolean')).toEqual(true); |
14 | | - expect(valuesTypeOf.includes('object')).toEqual(true); |
| 12 | + expect(keys.length).toEqual(2); |
| 13 | + expect(valuesTypeOf.includes("boolean")).toEqual(true); |
| 14 | + expect(valuesTypeOf.includes("object")).toEqual(true); |
15 | 15 |
|
16 | | - //Get name of properties on TrieNode |
17 | | - for (key in node) { |
18 | | - if (typeof node[key] === 'boolean') { |
19 | | - isEndOfWord = key; |
20 | | - } |
21 | | - if (typeof node[key] === 'object') { |
22 | | - children = key; |
23 | | - } |
24 | | - } |
25 | | - }); |
26 | | - test('Trie has single property holding root, root must be Trie Node.', () => { |
27 | | - const trie = new Trie(); |
28 | | - const keys = Object.keys(trie); |
29 | | - expect(keys.length).toEqual(1); |
30 | | - _root = keys[0]; |
31 | | - expect(trie[_root] instanceof TrieNode).toEqual(true); |
32 | | - }); |
| 16 | + //Get name of properties on TrieNode |
| 17 | + for (key in node) { |
| 18 | + if (typeof node[key] === "boolean") { |
| 19 | + isEndOfWord = key; |
| 20 | + } |
| 21 | + if (typeof node[key] === "object") { |
| 22 | + children = key; |
| 23 | + } |
| 24 | + } |
| 25 | + }); |
| 26 | + test("Trie has single property holding root, root must be Trie Node.", () => { |
| 27 | + const trie = new Trie(); |
| 28 | + const keys = Object.keys(trie); |
| 29 | + expect(keys.length).toEqual(1); |
| 30 | + _root = keys[0]; |
| 31 | + expect(trie[_root] instanceof TrieNode).toEqual(true); |
| 32 | + }); |
33 | 33 | }); |
34 | 34 |
|
35 | | -describe('insert', () => { |
36 | | - test('inserts nodes and marks correct node as end of word.', () => { |
37 | | - const t = new Trie(); |
38 | | - t.insert('man'); |
39 | | - t.insert('mat'); |
| 35 | +describe.skip("insert", () => { |
| 36 | + test("inserts nodes and marks correct node as end of word.", () => { |
| 37 | + const t = new Trie(); |
| 38 | + t.insert("man"); |
| 39 | + t.insert("mat"); |
40 | 40 |
|
41 | | - const rootNode = t[_root]; |
42 | | - expect(rootNode[isEndOfWord]).toEqual(false); |
43 | | - //Check that root only has one child of "m" |
44 | | - expect(Object.keys(rootNode[children]).length).toEqual(1); |
| 41 | + const rootNode = t[_root]; |
| 42 | + expect(rootNode[isEndOfWord]).toEqual(false); |
| 43 | + //Check that root only has one child of "m" |
| 44 | + expect(Object.keys(rootNode[children]).length).toEqual(1); |
45 | 45 |
|
46 | | - const mNode = rootNode[children]['m']; |
47 | | - expect(Object.keys(mNode[children]).length).toEqual(1); |
| 46 | + const mNode = rootNode[children]["m"]; |
| 47 | + expect(Object.keys(mNode[children]).length).toEqual(1); |
48 | 48 |
|
49 | | - const aNode = mNode[children]['a']; |
50 | | - expect(aNode[isEndOfWord]).toEqual(false); |
51 | | - expect(Object.keys(aNode[children]).length).toEqual(2); |
| 49 | + const aNode = mNode[children]["a"]; |
| 50 | + expect(aNode[isEndOfWord]).toEqual(false); |
| 51 | + expect(Object.keys(aNode[children]).length).toEqual(2); |
52 | 52 |
|
53 | | - const tNode = aNode[children]['t']; |
54 | | - const nNode = aNode[children]['n']; |
55 | | - expect(tNode[isEndOfWord]).toEqual(true); |
56 | | - expect(nNode[isEndOfWord]).toEqual(true); |
57 | | - }); |
| 53 | + const tNode = aNode[children]["t"]; |
| 54 | + const nNode = aNode[children]["n"]; |
| 55 | + expect(tNode[isEndOfWord]).toEqual(true); |
| 56 | + expect(nNode[isEndOfWord]).toEqual(true); |
| 57 | + }); |
58 | 58 | }); |
59 | 59 |
|
60 | | -describe('search', () => { |
61 | | - const t = new Trie(); |
62 | | - t.insert('canada'); |
| 60 | +describe.skip("search", () => { |
| 61 | + const t = new Trie(); |
| 62 | + t.insert("canada"); |
63 | 63 |
|
64 | | - test('returns true if word found', () => { |
65 | | - expect(t.search('canada')).toEqual(true); |
66 | | - }); |
67 | | - test('returns false if word not found', () => { |
68 | | - expect(t.search('a')).toEqual(false); |
69 | | - expect(t.search('canad')).toEqual(false); |
70 | | - }); |
| 64 | + test("returns true if word found", () => { |
| 65 | + expect(t.search("canada")).toEqual(true); |
| 66 | + }); |
| 67 | + test("returns false if word not found", () => { |
| 68 | + expect(t.search("a")).toEqual(false); |
| 69 | + expect(t.search("canad")).toEqual(false); |
| 70 | + }); |
71 | 71 | }); |
72 | 72 |
|
73 | | -describe('Prefix', () => { |
74 | | - test('autocomplete(prefix) works', () => { |
75 | | - const t = new Trie(); |
76 | | - t.insert('carer'); |
77 | | - t.insert('care'); |
78 | | - t.insert('card'); |
79 | | - t.insert('car'); |
| 73 | +describe.skip("Prefix", () => { |
| 74 | + test("autocomplete(prefix) works", () => { |
| 75 | + const t = new Trie(); |
| 76 | + t.insert("carer"); |
| 77 | + t.insert("care"); |
| 78 | + t.insert("card"); |
| 79 | + t.insert("car"); |
80 | 80 |
|
81 | | - expect(t.autocomplete('care').length).toEqual(2); |
82 | | - expect(t.autocomplete('car').length).toEqual(4); |
| 81 | + expect(t.autocomplete("care").length).toEqual(2); |
| 82 | + expect(t.autocomplete("car").length).toEqual(4); |
83 | 83 |
|
84 | | - expect(t.autocomplete('card')).toEqual([ 'card' ]); |
85 | | - expect(t.autocomplete('meow')).toEqual([]); |
86 | | - }); |
| 84 | + expect(t.autocomplete("card")).toEqual(["card"]); |
| 85 | + expect(t.autocomplete("meow")).toEqual([]); |
| 86 | + }); |
87 | 87 | }); |
0 commit comments