trie-prefix-tree
Advanced tools
Comparing version 1.5.0 to 1.5.1
@@ -12,18 +12,23 @@ 'use strict'; | ||
var _utils = require('./utils'); | ||
var _utils2 = _interopRequireDefault(_utils); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function append(trie, letter, index, array) { | ||
trie[letter] = trie[letter] || {}; | ||
trie = trie[letter]; | ||
var isEndWordLetter = letter === _config2.default.END_WORD; | ||
var isLastLetter = index === array.length - 1; | ||
if (index === array.length - 1) { | ||
if (isEndWordLetter && !isLastLetter) { | ||
trie[_config2.default.END_WORD] = 1; | ||
trie[_config2.default.END_WORD_REPLACER] = {}; | ||
trie = trie[_config2.default.END_WORD_REPLACER]; | ||
} else { | ||
trie[letter] = trie[letter] || {}; | ||
trie = trie[letter]; | ||
} | ||
if (isLastLetter) { | ||
trie[_config2.default.END_WORD] = 1; | ||
} | ||
return trie; | ||
}; | ||
} | ||
module.exports = exports['default']; |
@@ -8,4 +8,5 @@ 'use strict'; | ||
END_WORD: '$', | ||
END_WORD_REPLACER: '9a219a89-91cd-42e2-abd5-eb113af08ca8', | ||
PERMS_MIN_LEN: 2 | ||
}; | ||
module.exports = exports['default']; |
@@ -79,3 +79,3 @@ 'use strict'; | ||
isPrefix: function isPrefix(prefix) { | ||
if (typeof prefix !== 'string' || prefix === '') { | ||
if (typeof prefix !== 'string') { | ||
throw 'Expected string prefix, received ' + (typeof prefix === 'undefined' ? 'undefined' : _typeof(prefix)); | ||
@@ -98,3 +98,3 @@ } | ||
if (typeof strPrefix !== 'string' || strPrefix === '') { | ||
if (typeof strPrefix !== 'string') { | ||
throw 'Expected string prefix, received ' + (typeof strPrefix === 'undefined' ? 'undefined' : _typeof(strPrefix)); | ||
@@ -111,6 +111,25 @@ } | ||
var prefixNode = strPrefix.length ? (0, _checkPrefix6.default)(trie, strPrefix).prefixNode : trie; | ||
return (0, _recursePrefix2.default)(prefixNode, strPrefix, sorted); | ||
}, | ||
/** | ||
* Get a random word in the trie with the given prefix | ||
* @returns Array | ||
*/ | ||
getRandomWordWithPrefix: function getRandomWordWithPrefix(strPrefix) { | ||
if (typeof strPrefix !== 'string') { | ||
throw 'Expected string prefix, received ' + (typeof strPrefix === 'undefined' ? 'undefined' : _typeof(strPrefix)); | ||
} | ||
if (!this.isPrefix(strPrefix)) { | ||
return ''; | ||
} | ||
var _checkPrefix3 = (0, _checkPrefix6.default)(trie, strPrefix), | ||
prefixNode = _checkPrefix3.prefixNode; | ||
return (0, _recursePrefix2.default)(prefixNode, strPrefix, sorted); | ||
return (0, _recurseRandomWord2.default)(prefixNode, strPrefix); | ||
}, | ||
@@ -137,6 +156,3 @@ | ||
if (typeof sorted !== 'boolean') { | ||
throw 'Expected sort parameter as boolean, received ' + (typeof sorted === 'undefined' ? 'undefined' : _typeof(sorted)); | ||
} | ||
return (0, _recursePrefix2.default)(trie, '', sorted); | ||
return this.getPrefix('', sorted); | ||
}, | ||
@@ -221,2 +237,6 @@ | ||
var _recurseRandomWord = require('./recurseRandomWord'); | ||
var _recurseRandomWord2 = _interopRequireDefault(_recurseRandomWord); | ||
var _utils = require('./utils'); | ||
@@ -223,0 +243,0 @@ |
@@ -37,3 +37,4 @@ 'use strict'; | ||
for (var branch in node) { | ||
if (branch === _config2.default.END_WORD) { | ||
var currentLetter = branch; | ||
if (branch === _config2.default.END_WORD && typeof node[branch] === 'number') { | ||
if (sorted) { | ||
@@ -45,8 +46,10 @@ pushInOrder(word, prefixes); | ||
word = ''; | ||
} else if (branch === _config2.default.END_WORD_REPLACER) { | ||
currentLetter = _config2.default.END_WORD; | ||
} | ||
recursePrefix(node[branch], prefix + branch, sorted, prefixes); | ||
recursePrefix(node[branch], prefix + currentLetter, sorted, prefixes); | ||
} | ||
return prefixes; | ||
}; | ||
} | ||
module.exports = exports['default']; |
{ | ||
"name": "trie-prefix-tree", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Create and modify trie prefix structures, extract word lists including anagrams and sub-anagrams", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -130,2 +130,8 @@ # Trie Prefix Tree | ||
```javascript | ||
// get a random word at a prefix | ||
myTrie.getRandomWordWithPrefix('c'); // 'cat' | ||
myTrie.getRandomWordWithPrefix('c'); // 'cats' | ||
``` | ||
Other: | ||
@@ -132,0 +138,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21057
11
372
174