Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"env": {
"node": 1
"node": 1,
"es6": true,
},
"parserOptions": {
"ecmaVersion": 2017,
Expand Down
7 changes: 4 additions & 3 deletions __tests__/create.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import create from '../src/create';
import utils from '../src/utils';

describe('Creating the Trie', () => {
it('throws when the first argument is not an array', () => {
Expand All @@ -14,16 +15,16 @@ describe('Creating the Trie', () => {

it('returns a Trie object structure converted to lowercase', () => {
const input = ['Dog'];
const data = create(input);
const expected = {
const data = utils.stringify(create(input), 0);
const expected = JSON.stringify({
d: {
o: {
g: {
$: 1
}
}
}
};
});

expect(data).toEqual(expected);
});
Expand Down
3 changes: 2 additions & 1 deletion __tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import trie from '../src/index';
import utils from '../src/utils';

describe('Trie', () => {
it('throws an error when the first argument specified is not an array', () => {
Expand Down Expand Up @@ -59,7 +60,7 @@ describe('Retrieving the Trie', () => {
describe('Retrieving the RAW Trie tree', () => {
it('returns the raw trie object structure', () => {
const input = ['dog', 'dogs', 'donut'];
const actual = JSON.stringify(trie(input).tree());
const actual = utils.stringify(trie(input).tree(), 0);
const expected = JSON.stringify({
d: {
o: {
Expand Down
12 changes: 6 additions & 6 deletions src/append.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ export default function append(trie, letter, index, array) {
const isLastLetter = index === array.length - 1;

if(isEndWordLetter && !isLastLetter) {
trie[config.END_WORD] = 1;
trie[config.END_WORD_REPLACER] = {};
trie = trie[config.END_WORD_REPLACER];
trie.set(config.END_WORD, 1);
trie.set(config.END_WORD_REPLACER, new Map());
trie = trie.get(config.END_WORD_REPLACER);
} else {
trie[letter] = trie[letter] || {};
trie = trie[letter];
trie.set(letter, trie.get(letter) || new Map());
trie = trie.get(letter);
}

if(isLastLetter) {
trie[config.END_WORD] = 1;
trie.set(config.END_WORD, 1);
}

return trie;
Expand Down
4 changes: 2 additions & 2 deletions src/checkPrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import utils from './utils';
export default function checkPrefix(prefixNode, prefix) {
const input = prefix.toLowerCase().split('');
const prefixFound = input.every((letter, index) => {
if(!prefixNode[letter]) {
if(!prefixNode.get(letter)) {
return false;
}
return prefixNode = prefixNode[letter];
return prefixNode = prefixNode.get(letter);
});

return {
Expand Down
2 changes: 1 addition & 1 deletion src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function create(input) {
.reduce(append, accumulator);

return accumulator;
}, {});
}, new Map());

return trie;
};
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default function(input) {
const { prefixFound, prefixNode } = checkPrefix(trie, word);

if(prefixFound) {
delete prefixNode[config.END_WORD];
prefixNode.delete(config.END_WORD);
}

return this;
Expand Down Expand Up @@ -152,7 +152,7 @@ export default function(input) {
const { prefixFound, prefixNode } = checkPrefix(trie, word);

if(prefixFound) {
return prefixNode[config.END_WORD] === 1;
return prefixNode.get(config.END_WORD) === 1;
}

return false;
Expand Down
7 changes: 4 additions & 3 deletions src/permutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export default function permutations(letters, trie, opts = {
const words = [];

const permute = (word, node, prefix = '') => {
if(!(node instanceof Map)) return words.sort();
const wordIsEmpty = word.length === 0;
const wordFound = words.includes(prefix);
const endWordFound = node[config.END_WORD] === 1;
const endWordFound = node.get(config.END_WORD) === 1;

if(wordIsEmpty && endWordFound && !wordFound) {
words.push(prefix);
Expand All @@ -27,9 +28,9 @@ export default function permutations(letters, trie, opts = {
}
}

if(node[letter]) {
if(node.get(letter)) {
const remaining = word.substring(0, i) + word.substring(i + 1, len);
permute(remaining, node[letter], prefix + letter, words);
permute(remaining, node.get(letter), prefix + letter, words);
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/recursePrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const pushInOrder = function(word, prefixes) {
export default function recursePrefix(node, prefix, sorted, prefixes = []) {
let word = prefix;

for(const branch in node) {
for(const branch of node.keys()) {
let currentLetter = branch;
if(branch === config.END_WORD && typeof node[branch] === 'number') {
if(branch === config.END_WORD && typeof node.get(branch) === 'number') {
if(sorted) {
pushInOrder(word, prefixes);
} else {
Expand All @@ -32,7 +32,9 @@ export default function recursePrefix(node, prefix, sorted, prefixes = []) {
} else if(branch === config.END_WORD_REPLACER) {
currentLetter = config.END_WORD;
}
recursePrefix(node[branch], prefix + currentLetter, sorted, prefixes);
if(node.get(branch) instanceof Map) {
recursePrefix(node.get(branch), prefix + currentLetter, sorted, prefixes);
}
}

return prefixes;
Expand Down
6 changes: 3 additions & 3 deletions src/recurseRandomWord.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import config from './config';

export default function recurseRandomWord(node, prefix) {
const word = prefix;
const branches = Object.keys(node);
const branches = [...node.keys()];
const branch = branches[Math.floor(Math.random() * branches.length)];

if(branch === config.END_WORD) {
if(branch === config.END_WORD || !node.get(branch)) {
return word;
}
return recurseRandomWord(node[branch], prefix + branch);
return recurseRandomWord(node.get(branch), prefix + branch);
};
12 changes: 11 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ export default {
return JSON.parse(JSON.stringify(obj));
},

mapToObj(map) {
const obj = {};
for(const [k, v] of map) {
obj[k] = (v instanceof Map) ? this.mapToObj(v) : v;
}
return obj;
},

stringify(obj, spacer = 2) {
if(typeof obj === 'undefined') {
return '';
}
return JSON.stringify(obj, null, spacer);
return (obj instanceof Map)
? JSON.stringify(this.mapToObj(obj), null, spacer)
: JSON.stringify(obj, null, spacer);
},
};