TEST
by LyndseyB
Babel + JSX
const PERMS_MIN_LEN = 2;
const config = {
END_WORD: '$',
};
const utils = {
objectCopy(obj) {
if(typeof obj === 'undefined') {
return {};
}
return JSON.parse(JSON.stringify(obj));
},
stringify(obj, spacer = 2) {
if(typeof obj === 'undefined') {
return '';
}
return JSON.stringify(obj, null, spacer);
},
};
const permute = (source, dictionary, subAnagramSearch = false) => {
if(typeof source !== 'string') {
throw('Expected string source');
}
const data = run(dictionary);
const words = [];
const permutations = (source, prefix = '') => {
source = source.toLowerCase();
const letters = source.split('');
const word = prefix + source;
const wordType = subAnagramSearch ? prefix : word;
const isValid = data.hasWord(wordType);
const isPrefix = data.isPrefix(prefix);
if(isValid && !words.includes(wordType)) {
words.push(wordType);
}
letters.forEach((letter, index) => {
if(isPrefix) {
const remainder = source.substring(0, index) + source.substring(index + 1);
permutations(remainder, prefix + letter);
}
});
return words.sort();
};
return permutations(source);
};
const recursePrefix = function recursePrefix(node, prefix, prefixes = []) {
let word = prefix;
for(const branch in node) {
if(branch === config.END_WORD) {
prefixes.push(word);
word = '';
}
recursePrefix(node[branch], prefix + branch, prefixes);
}
return prefixes.sort();
};
const checkPrefix = function checkPrefix(node, prefix) {
const input = prefix.toLowerCase().split('');
const found = input.every((letter, index) => {
if(!node[letter]) {
return false;
}
return node = node[letter];
});
return {
found,
node,
};
};
const append = function append(trie, letter, index, array) {
trie[letter] = trie[letter] || {};
trie = trie[letter];
if(index === array.length - 1) {
trie[config.END_WORD] =...