Skip to content

Add caseSensitive parameter #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:
18 changes: 17 additions & 1 deletion __tests__/create.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
31 changes: 30 additions & 1 deletion __tests__/hasWord.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
6 changes: 4 additions & 2 deletions src/checkPrefix.js
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 7 additions & 1 deletion src/create.js
Original file line number Diff line number Diff line change
@@ -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('')
22 changes: 13 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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;