diff --git a/lib/hypher.js b/lib/hypher.js index 2278d7b..72c12a3 100644 --- a/lib/hypher.js +++ b/lib/hypher.js @@ -50,11 +50,6 @@ Hypher.TrieNode; Hypher.prototype.createTrie = function (patternObject) { var size = 0, i = 0, - c = 0, - p = 0, - chars = null, - points = null, - codePoint = null, t = null, tree = { _points: [] @@ -63,27 +58,35 @@ Hypher.prototype.createTrie = function (patternObject) { for (size in patternObject) { if (patternObject.hasOwnProperty(size)) { - patterns = patternObject[size].match(new RegExp('.{1,' + (+size) + '}', 'g')); - - for (i = 0; i < patterns.length; i += 1) { - chars = patterns[i].replace(/[0-9]/g, '').split(''); - points = patterns[i].split(/\D/); + size = +size; + for (i = 0; i < patternObject[size].length; i += size) { t = tree; - for (c = 0; c < chars.length; c += 1) { - codePoint = chars[c].charCodeAt(0); + const points = []; + let prev = 0; - if (!t[codePoint]) { - t[codePoint] = {}; + for (let j = 0; j < size; j++) { + const char = patternObject[size][i + j]; + if (!char) { + break; + } + const codePoint = char.charCodeAt(0); + if (codePoint >= 48 && codePoint <= 57) { + points.push(+char); + } else { + if (prev < 48 || prev > 57) { + points.push(0); + } + + if (!t[codePoint]) { + t[codePoint] = {}; + } + t = t[codePoint]; } - t = t[codePoint]; + prev = codePoint; } - t._points = []; - - for (p = 0; p < points.length; p += 1) { - t._points[p] = points[p] || 0; - } + t._points = points; } } } @@ -129,7 +132,6 @@ Hypher.prototype.hyphenateText = function (str, minLength) { Hypher.prototype.hyphenate = function (word) { var characters, characterPoints = [], - originalCharacters, i, j, k, @@ -153,8 +155,7 @@ Hypher.prototype.hyphenate = function (word) { word = '_' + word + '_'; - characters = word.toLowerCase().split(''); - originalCharacters = word.split(''); + characters = word.toLowerCase(); wordLength = characters.length; for (i = 0; i < wordLength; i += 1) { @@ -182,9 +183,9 @@ Hypher.prototype.hyphenate = function (word) { for (i = 1; i < wordLength - 1; i += 1) { if (i > this.leftMin && i < (wordLength - this.rightMin) && points[i] % 2) { - result.push(originalCharacters[i]); + result.push(word[i]); } else { - result[result.length - 1] += originalCharacters[i]; + result[result.length - 1] += word[i]; } } diff --git a/package.json b/package.json index 561b594..cf4eb71 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "author": "Bram Stein (http://www.bramstein.com)", "devDependencies": { "benchmark": "=0.1.347", - "microtime": "^2.1.3", "vows": ">=0.5.6" }, "directories": { diff --git a/test/benchmark.js b/test/benchmark.js index 6c4e788..bbbc7de 100644 --- a/test/benchmark.js +++ b/test/benchmark.js @@ -55,6 +55,8 @@ suite.add('Hypher', hypherDictionary, { setup: hypherSetup }); +suite.add('Hypher (createTrie)', hypherSetup); + suite.add('Hyphenator', hyphenatorDictionary, { setup: hyphenatorSetup }); diff --git a/test/hypher-test.js b/test/hypher-test.js index 3235a08..1aea7b0 100644 --- a/test/hypher-test.js +++ b/test/hypher-test.js @@ -70,13 +70,13 @@ vows.describe('Hypher').addBatch({ 97: { _points: [0, 1], 98: { - _points: [0, 2, 0] + _points: [0, 2] } }, 98: { _points: [0, 2], 99: { - _points: [0, 3, 0] + _points: [0, 3] } }, _points: []