diff --git a/README.md b/README.md index 8f2fe1c..4fad226 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,11 @@ import trie from 'trie-prefix-tree'; Instantiate the Trie: ```javascript +// create Trie var myTrie = trie(['cat', 'cats', 'dogs', 'elephant', 'tiger']); + +// create Trie with case-sensitive +var myTrieWithCaseSensitive = trie(['cat', 'cats', 'dogs', 'elephant', 'tiger'], true); ``` Trie functionality: diff --git a/__tests__/create.js b/__tests__/create.js index 66a5ae4..e9a1e48 100644 --- a/__tests__/create.js +++ b/__tests__/create.js @@ -4,7 +4,7 @@ describe('Creating the Trie', () => { it('throws when the first argument is not an array', () => { const input = ''; const expected = `Expected parameter Array, received ${typeof input}`; - + try { create(input); } catch(error) { @@ -27,4 +27,20 @@ describe('Creating the Trie', () => { expect(data).toEqual(expected); }); + + it('returns a Trie object structure with case-sensitive', () => { + const input = ['Dog']; + const data = create(input, true); + const expected = { + D: { + o: { + g: { + $: 1 + } + } + } + }; + + expect(data).toEqual(expected); + }); }); diff --git a/__tests__/hasWord.js b/__tests__/hasWord.js index e8cc1be..1544ba4 100644 --- a/__tests__/hasWord.js +++ b/__tests__/hasWord.js @@ -1,7 +1,7 @@ import trie from '../src/index'; describe('validating a word exists', () => { - const input = ['dog', 'cat', 'lion', 'tiger', 'carse', 'car', 'scar']; + const input = ['dog', 'cat', 'lion', 'tiger', 'carse', 'car', 'scar', 'cute DOG']; const data = trie(input); it('throws an error when a word is not passed', () => { @@ -12,6 +12,10 @@ describe('validating a word exists', () => { expect(data.hasWord('dog')).toEqual(true); }); + it('returns true for a valid word found', () => { + expect(data.hasWord('cute dog')).toEqual(true); + }); + it('converts the word to lowercase', () => { expect(data.hasWord('DOG')).toEqual(true); }); @@ -20,3 +24,28 @@ describe('validating a word exists', () => { expect(data.hasWord('elephant')).toEqual(false); }); }); + +describe('validating a word exists with case-sensitive', () => { + const input = ['dog', 'cat', 'lion', 'tiger', 'carse', 'car', 'scar', 'cute DOG']; + const data = trie(input, true); + + it('throws an error when a word is not passed', () => { + expect(() => data.hasWord()).toThrow(); + }); + + it('returns true for a valid word found', () => { + expect(data.hasWord('dog')).toEqual(true); + }); + + it('returns true for a valid word found', () => { + expect(data.hasWord('cute DOG')).toEqual(true); + }); + + it('returns false for a invalid word', () => { + expect(data.hasWord('cute dog')).toEqual(false); + }); + + it('returns false for an invalid word', () => { + expect(data.hasWord('elephant')).toEqual(false); + }); +}); diff --git a/src/checkPrefix.js b/src/checkPrefix.js index b0cadf7..fae4d02 100644 --- a/src/checkPrefix.js +++ b/src/checkPrefix.js @@ -1,7 +1,9 @@ import utils from './utils'; -export default function checkPrefix(prefixNode, prefix) { - const input = prefix.toLowerCase().split(''); +export default function checkPrefix(prefixNode, prefix, caseSensitive) { + const input = caseSensitive + ? prefix.split('') + : prefix.toLowerCase().split(''); const prefixFound = input.every((letter, index) => { if(!prefixNode[letter]) { return false; diff --git a/src/create.js b/src/create.js index ff86b54..fc945c8 100644 --- a/src/create.js +++ b/src/create.js @@ -1,11 +1,17 @@ import append from './append'; -export default function create(input) { +export default function create(input, caseSensitive) { if(!Array.isArray(input)) { throw(`Expected parameter Array, received ${typeof input}`); } const trie = input.reduce((accumulator, item) => { + caseSensitive + ? + item + .split('') + .reduce(append, accumulator) + : item .toLowerCase() .split('') diff --git a/src/index.js b/src/index.js index 3a11190..00d6b28 100644 --- a/src/index.js +++ b/src/index.js @@ -9,12 +9,12 @@ import permutations from './permutations'; const PERMS_MIN_LEN = config.PERMS_MIN_LEN; -export default function(input) { +export default function(input, caseSensitive = false) { if(!Array.isArray(input)) { throw(`Expected parameter Array, received ${typeof input}`); } - const trie = create([...input]); + const trie = create([...input], caseSensitive); return { /** @@ -23,7 +23,7 @@ export default function(input) { tree() { return trie; }, - + /** * Get a string representation of the trie */ @@ -43,7 +43,9 @@ export default function(input) { return append(...params); }; - const input = word.toLowerCase().split(''); + const input = caseSensitive + ? word.split('') + : word.toLowerCase().split(''); input.reduce(reducer, trie); return this; @@ -57,7 +59,8 @@ export default function(input) { throw(`Expected parameter string, received ${typeof word}`); } - const { prefixFound, prefixNode } = checkPrefix(trie, word); + const { prefixFound, prefixNode } = + checkPrefix(trie, word, caseSensitive); if(prefixFound) { delete prefixNode[config.END_WORD]; @@ -75,7 +78,7 @@ export default function(input) { throw(`Expected string prefix, received ${typeof prefix}`); } - const { prefixFound } = checkPrefix(trie, prefix); + const { prefixFound } = checkPrefix(trie, prefix, caseSensitive); return prefixFound; }, @@ -98,7 +101,7 @@ export default function(input) { } const prefixNode = strPrefix.length ? - checkPrefix(trie, strPrefix).prefixNode + checkPrefix(trie, strPrefix, caseSensitive).prefixNode : trie; return recursePrefix(prefixNode, strPrefix, sorted); @@ -117,7 +120,7 @@ export default function(input) { return ''; } - const { prefixNode } = checkPrefix(trie, strPrefix); + const { prefixNode } = checkPrefix(trie, strPrefix, caseSensitive); return recurseRandomWord(prefixNode, strPrefix); }, @@ -149,7 +152,8 @@ export default function(input) { throw(`Expected string word, received ${typeof word}`); } - const { prefixFound, prefixNode } = checkPrefix(trie, word); + const { prefixFound, prefixNode } = + checkPrefix(trie, word, caseSensitive); if(prefixFound) { return prefixNode[config.END_WORD] === 1;