Skip to content

Commit

Permalink
fix: Fixes #8.
Browse files Browse the repository at this point in the history
Allow words containing the end word character to be added to the trie.

#8
  • Loading branch information
lyndseybrowning committed Feb 9, 2019
1 parent df2fc30 commit 9fd037d
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 124 deletions.
27 changes: 20 additions & 7 deletions __tests__/addWord.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import trie from '../src/index';
import config from '../src/config';

test('Adding a word to the trie', () => {
const input = ['dog'];
Expand All @@ -7,19 +8,31 @@ test('Adding a word to the trie', () => {
d: {
o: {
g: {
$: 1
}
}
$: 1,
},
},
},
c: {
a: {
t: {
$: 1
}
}
}
$: 1,
},
},
},
});

expect(() => trie(input).addWord()).toThrow();
expect(actual.dump()).toEqual(expected);
});

test('Words that contain the END_WORD character can be added to the trie', () => {
const dictionary = trie([]);
const testWord = `test${config.END_WORD}word`;
const expectedWords = ['test', testWord];

dictionary.addWord('test');
dictionary.addWord(testWord);

expect(dictionary.getWords()).toEqual(expectedWords);
expect(dictionary.getPrefix('te')).toEqual(expectedWords);
});
139 changes: 70 additions & 69 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,74 +1,75 @@
{
"name": "trie-prefix-tree",
"version": "0.0.0-development",
"description": "Create and modify trie prefix structures, extract word lists including anagrams and sub-anagrams",
"main": "dist/index.js",
"scripts": {
"build": "babel --out-dir dist src",
"prebuild": "rimraf dist",
"lint": "eslint src",
"report-coverage": "codecov",
"test": "jest",
"test-coverage": "jest --coverage",
"test-watch": "jest --watch",
"commit": "git-cz",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
"url": "https://github.com/lyndseybrowning/trie-prefix-tree"
},
"keywords": [
"javascript",
"trie",
"words"
],
"files": [
"dist",
"README.md"
],
"author": "Lyndsey Browning <lbrowning86@gmail.com> (http://lyndseyb.co.uk/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/lyndseybrowning/trie-prefix/issues"
},
"homepage": "https://github.com/lyndseybrowning/trie-prefix#readme",
"devDependencies": {
"babel-cli": "^6.22.2",
"babel-core": "^6.22.1",
"babel-jest": "18.0.0",
"babel-plugin-add-module-exports": "0.2.1",
"babel-plugin-array-includes": "2.0.3",
"babel-plugin-transform-object-rest-spread": "^6.22.0",
"babel-preset-env": "1.1.8",
"babel-preset-es2015": "^6.22.0",
"codecov": "1.0.1",
"commitizen": "2.9.6",
"cz-conventional-changelog": "1.2.0",
"eslint": "^3.15.0",
"ghooks": "2.0.0",
"jest": "^18.1.0",
"rimraf": "^2.5.4",
"semantic-release": "^6.3.2"
},
"jest": {
"coverageDirectory": "./coverage",
"collectCoverage": true,
"transform": {
".*": "<rootDir>/node_modules/babel-jest"
"name": "trie-prefix-tree",
"version": "0.0.0-development",
"description": "Create and modify trie prefix structures, extract word lists including anagrams and sub-anagrams",
"main": "dist/index.js",
"scripts": {
"build": "babel --out-dir dist src",
"prebuild": "rimraf dist",
"lint": "eslint src",
"report-coverage": "codecov",
"test": "jest",
"test-coverage": "jest --coverage",
"test-watch": "jest --watch",
"commit": "git-cz",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"moduleFileExtensions": [
"js",
"json",
"es6"
]
},
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
"repository": {
"type": "git",
"url": "https://github.com/lyndseybrowning/trie-prefix-tree"
},
"ghooks": {
"pre-commit": "npm run lint && npm run test"
"keywords": [
"javascript",
"trie",
"words"
],
"files": [
"dist",
"README.md"
],
"author": "Lyndsey Browning <lbrowning86@gmail.com> (http://lyndseyb.co.uk/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/lyndseybrowning/trie-prefix/issues"
},
"homepage": "https://github.com/lyndseybrowning/trie-prefix#readme",
"devDependencies": {
"babel-cli": "^6.22.2",
"babel-core": "^6.22.1",
"babel-jest": "18.0.0",
"babel-plugin-add-module-exports": "0.2.1",
"babel-plugin-array-includes": "2.0.3",
"babel-plugin-transform-object-rest-spread": "^6.22.0",
"babel-preset-env": "1.1.8",
"babel-preset-es2015": "^6.22.0",
"codecov": "1.0.1",
"commitizen": "2.9.6",
"cz-conventional-changelog": "1.2.0",
"eslint": "^3.15.0",
"ghooks": "2.0.0",
"jest": "^18.1.0",
"rimraf": "^2.5.4",
"semantic-release": "^6.3.2"
},
"jest": {
"verbose": false,
"coverageDirectory": "./coverage",
"collectCoverage": false,
"transform": {
".*": "<rootDir>/node_modules/babel-jest"
},
"moduleFileExtensions": [
"js",
"json",
"es6"
]
},
"config": {
"commitizen": {
"path": "node_modules/cz-conventional-changelog"
},
"ghooks": {
"pre-commit": "npm run lint && npm run test"
}
}
}
}
18 changes: 13 additions & 5 deletions src/append.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import config from './config';
import utils from './utils';

export default function append(trie, letter, index, array) {
trie[letter] = trie[letter] || {};
trie = trie[letter];
const isEndWordLetter = letter === config.END_WORD;
const isLastLetter = index === array.length - 1;

if(index === array.length - 1) {
if(isEndWordLetter && !isLastLetter) {
trie[config.END_WORD] = 1;
trie[config.END_WORD_REPLACER] = {};
trie = trie[config.END_WORD_REPLACER];
} else {
trie[letter] = trie[letter] || {};
trie = trie[letter];
}

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

return trie;
};
}
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
END_WORD: '$',
END_WORD_REPLACER: '9a219a89-91cd-42e2-abd5-eb113af08ca8',
PERMS_MIN_LEN: 2,
};
Loading

0 comments on commit 9fd037d

Please sign in to comment.