Comparing version 1.3.0 to 1.3.1
354
hyphen.js
@@ -8,3 +8,3 @@ /** Franklin M. Liang's hyphenation algorithm, implemented in Javascript. | ||
*/ | ||
(function(root, factory) { | ||
(function (root, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
@@ -22,4 +22,4 @@ // AMD. Register as an anonymous module. | ||
} | ||
})(this, function() { | ||
var // settings | ||
})(this, function () { | ||
var // | ||
SETTING_ASYNC_MODE = false, | ||
@@ -29,74 +29,46 @@ SETTING_DEBUG = false, | ||
function hyphenateWord(text, patterns, debug, hyphenChar) { | ||
var levels = new Array(text.length + 1), | ||
loweredText = text.toLocaleLowerCase(), | ||
p = [], | ||
pattern, | ||
patternData, | ||
patternIndex = 0; | ||
var _global = | ||
typeof global === "object" | ||
? global | ||
: typeof window === "object" | ||
? /* eslint-disable-next-line no-undef */ | ||
window | ||
: typeof this === "object" | ||
? this | ||
: {}; | ||
for (var i = levels.length; i--; ) levels[i] = 0; | ||
while ((patternData = patterns[patternIndex++])) { | ||
var fromChar = 0; | ||
var endPattern = false; | ||
while (!endPattern) { | ||
var patternEntityIndex = loweredText.indexOf( | ||
patternData.text, | ||
fromChar | ||
function createHyphenator(patternsDefinition, settings) { | ||
var // Settings | ||
debug = | ||
(settings && settings.debug !== undefined && settings.debug) || | ||
SETTING_DEBUG, | ||
hyphenChar = | ||
(settings && | ||
settings.hyphenChar !== undefined && | ||
settings.hyphenChar) || | ||
SETTING_HYPHEN_CHAR, | ||
asyncMode = | ||
(settings && settings.async !== undefined && settings.async) || | ||
SETTING_ASYNC_MODE, | ||
// Prepare cache | ||
cache = patternsDefinition.exceptions.reduce(function (cache, exception) { | ||
cache[exception.replace(/\-/g, "")] = exception.replace( | ||
/\-/g, | ||
hyphenChar | ||
); | ||
return cache; | ||
}, {}), | ||
// Preprocess patterns | ||
patterns = patternsDefinition.patterns.map(preprocessPattern); | ||
var patternFits = | ||
patternEntityIndex > -1 && | ||
(patternData.stickToLeft ? patternEntityIndex === 0 : true) && | ||
(patternData.stickToRight | ||
? patternEntityIndex + patternData.text.length === text.length | ||
: true); | ||
if (patternFits) { | ||
p.push(patternData.pattern + ">" + patternData.levels.join("")); | ||
for (var i = 0; i < patternData.levels.length; i++) | ||
levels[patternEntityIndex + i] = Math.max( | ||
patternData.levels[i], | ||
levels[patternEntityIndex + i] | ||
); | ||
} | ||
if (patternEntityIndex > -1 && patternData.text.length > 0) { | ||
fromChar = patternEntityIndex + patternData.text.length + 1; | ||
} else { | ||
endPattern = true; | ||
} | ||
} | ||
if (asyncMode && !("Promise" in _global)) { | ||
throw new Error( | ||
"Failed to create hyphenator: Could not find global Promise object, needed for hyphenator to work in async mode" | ||
); | ||
} | ||
levels[0] = levels[1] = levels[levels.length - 1] = levels[ | ||
levels.length - 2 | ||
] = 0; | ||
var hyphenatedText = "", | ||
leveledText = "", | ||
debugHyphenatedText = ""; | ||
for (var i = 0; i < levels.length; i++) { | ||
hyphenatedText += | ||
(levels[i] % 2 === 1 ? hyphenChar : "") + text.charAt(i); | ||
debugHyphenatedText += (levels[i] % 2 === 1 ? "-" : "") + text.charAt(i); | ||
leveledText += (levels[i] > 0 ? levels[i] : "") + text.charAt(i); | ||
} | ||
if (debug) | ||
console.log.apply( | ||
console, | ||
[text, "->"] | ||
.concat(p) | ||
.concat(["->"]) | ||
.concat(levels) | ||
.concat(["->", leveledText]) | ||
.concat(["->", debugHyphenatedText]) | ||
); | ||
return hyphenatedText; | ||
return function (text) { | ||
return start(text, patterns, cache, debug, hyphenChar, asyncMode); | ||
}; | ||
} | ||
function createTextChunkReader(text, hyphenChar) { | ||
@@ -109,3 +81,4 @@ function readNextTextChunk() { | ||
chunkReader: while (nextCharIndex <= text.length) { | ||
var nextChar = text.charAt(nextCharIndex++), | ||
var // | ||
nextChar = text.charAt(nextCharIndex++), | ||
charIsLetter = | ||
@@ -181,3 +154,4 @@ !!nextChar && !/\s|[\!-\@\[-\`\{-\~\u2013-\u203C]/.test(nextChar), | ||
var nextCharIndex = 0, | ||
var // | ||
nextCharIndex = 0, | ||
SHOULD_HYPHENATE = 1, | ||
@@ -195,3 +169,121 @@ SHOULD_SKIP = 2, | ||
} | ||
function hyphenateWord(text, patterns, debug, hyphenChar) { | ||
var // | ||
levels = new Array(text.length + 1), | ||
loweredText = text.toLocaleLowerCase(), | ||
p = [], | ||
patternData, | ||
patternIndex = 0; | ||
for (var i = levels.length; i--; ) levels[i] = 0; | ||
while ((patternData = patterns[patternIndex++])) { | ||
var // | ||
fromChar = 0, | ||
endPattern = false; | ||
while (!endPattern) { | ||
var // | ||
patternEntityIndex = loweredText.indexOf(patternData.text, fromChar), | ||
patternFits = | ||
patternEntityIndex > -1 && | ||
(patternData.stickToLeft ? patternEntityIndex === 0 : true) && | ||
(patternData.stickToRight | ||
? patternEntityIndex + patternData.text.length === text.length | ||
: true); | ||
if (patternFits) { | ||
p.push(patternData.pattern + ">" + patternData.levels.join("")); | ||
for (var i = 0; i < patternData.levels.length; i++) | ||
levels[patternEntityIndex + i] = Math.max( | ||
patternData.levels[i], | ||
levels[patternEntityIndex + i] | ||
); | ||
} | ||
if (patternEntityIndex > -1 && patternData.text.length > 0) { | ||
fromChar = patternEntityIndex + patternData.text.length + 1; | ||
} else { | ||
endPattern = true; | ||
} | ||
} | ||
} | ||
levels[0] = levels[1] = levels[levels.length - 1] = levels[ | ||
levels.length - 2 | ||
] = 0; | ||
var // | ||
hyphenatedText = "", | ||
leveledText = "", | ||
debugHyphenatedText = ""; | ||
for (var i = 0; i < levels.length; i++) { | ||
hyphenatedText += | ||
(levels[i] % 2 === 1 ? hyphenChar : "") + text.charAt(i); | ||
debugHyphenatedText += (levels[i] % 2 === 1 ? "-" : "") + text.charAt(i); | ||
leveledText += (levels[i] > 0 ? levels[i] : "") + text.charAt(i); | ||
} | ||
if (debug) | ||
console.log.apply( | ||
console, | ||
[text, "->"] | ||
.concat(p) | ||
.concat(["->"]) | ||
.concat(levels) | ||
.concat(["->", leveledText]) | ||
.concat(["->", debugHyphenatedText]) | ||
); | ||
return hyphenatedText; | ||
} | ||
function preprocessPattern(pattern) { | ||
var // | ||
patternCharIndex = 0, | ||
patternChar, | ||
patternData = { | ||
pattern: pattern, | ||
text: "", | ||
levels: [], | ||
stickToLeft: 0, | ||
stickToRight: 0 | ||
}, | ||
states = { alphabet: 1, level: 2, stickToLeft: 3, stickToRight: 4 }; | ||
while ((patternChar = pattern.charAt(patternCharIndex++))) { | ||
var // | ||
charIsDot = patternChar === ".", | ||
charIsNumber = !charIsDot && /\d/.test(patternChar), | ||
state = charIsDot | ||
? patternCharIndex - 1 === 0 | ||
? states.stickToLeft | ||
: states.stickToRight | ||
: charIsNumber | ||
? states.level | ||
: states.alphabet; | ||
switch (state) { | ||
case states.alphabet: | ||
!prevCharIsNumber && patternData.levels.push(0); | ||
patternData.text += patternChar; | ||
break; | ||
case states.level: | ||
patternData.levels.push(parseInt(patternChar)); | ||
break; | ||
case states.stickToLeft: | ||
patternData.stickToLeft = true; | ||
break; | ||
case states.stickToRight: | ||
patternData.stickToRight = true; | ||
break; | ||
} | ||
var prevCharIsNumber = charIsNumber; | ||
} | ||
return patternData; | ||
} | ||
function start(text, patterns, cache, debug, hyphenChar, isAsync) { | ||
@@ -210,9 +302,10 @@ function done() { | ||
); | ||
console.log(`Work time: ${workTime / 1000}`); | ||
console.log(`Wait time: ${(allTime - workTime) / 1000}`); | ||
console.log(`All time: ${allTime / 1000}`); | ||
console.log("Work time: " + workTime / 1000); | ||
console.log("Wait time: " + (allTime - workTime) / 1000); | ||
console.log("All time: " + allTime / 1000); | ||
} | ||
} | ||
var newText = "", | ||
var // | ||
newText = "", | ||
nextTextChunk, | ||
@@ -226,6 +319,7 @@ reader = createTextChunkReader(text, hyphenChar), | ||
var allTime = new Date(), | ||
var // | ||
allTime = new Date(), | ||
workTime = 0; | ||
var resolveNewText = function() {}; | ||
var resolveNewText = function () {}; | ||
@@ -273,3 +367,4 @@ (function nextTick() { | ||
if (isAsync) { | ||
return new Promise(function(resolve) { | ||
/* eslint-disable-next-line no-undef */ | ||
return new Promise(function (resolve) { | ||
resolveNewText = resolve; | ||
@@ -281,104 +376,3 @@ }); | ||
} | ||
// extract useful data from pattern | ||
function preprocessPattern(pattern) { | ||
var patternCharIndex = 0, | ||
patternChar, | ||
patternData = { | ||
pattern: pattern, | ||
text: "", | ||
levels: [], | ||
stickToLeft: 0, | ||
stickToRight: 0 | ||
}, | ||
states = { alphabet: 1, level: 2, stickToLeft: 3, stickToRight: 4 }; | ||
while ((patternChar = pattern.charAt(patternCharIndex++))) { | ||
var charIsDot = patternChar === ".", | ||
charIsNumber = !charIsDot && /\d/.test(patternChar), | ||
state = charIsDot | ||
? patternCharIndex - 1 === 0 | ||
? states.stickToLeft | ||
: states.stickToRight | ||
: charIsNumber | ||
? states.level | ||
: states.alphabet; | ||
switch (state) { | ||
case states.alphabet: | ||
!prevCharIsNumber && patternData.levels.push(0); | ||
patternData.text += patternChar; | ||
break; | ||
case states.level: | ||
patternData.levels.push(parseInt(patternChar)); | ||
break; | ||
case states.stickToLeft: | ||
patternData.stickToLeft = true; | ||
break; | ||
case states.stickToRight: | ||
patternData.stickToRight = true; | ||
break; | ||
} | ||
var prevCharIsNumber = charIsNumber; | ||
} | ||
return patternData; | ||
} | ||
function purifyPatterns(text) { | ||
return ( | ||
text | ||
// Remove comments | ||
.replace(/%.*/g, "") | ||
// Remove repeating spaces | ||
.replace(/\s+/g, " ") | ||
// Trim spaces | ||
.replace(/^\s|\s$/g, "") | ||
// Split to Array | ||
.split(" ") | ||
); | ||
} | ||
// Hyphenator factory | ||
return function(patternsDefinition, settings) { | ||
var // Settings | ||
debug = | ||
(settings && settings.debug !== undefined && settings.debug) || | ||
SETTING_DEBUG, | ||
hyphenChar = | ||
(settings && | ||
settings.hyphenChar !== undefined && | ||
settings.hyphenChar) || | ||
SETTING_HYPHEN_CHAR, | ||
asyncMode = | ||
(settings && settings.async !== undefined && settings.async) || | ||
SETTING_ASYNC_MODE, | ||
cache = {}, | ||
// Preprocess patterns | ||
patterns = (patternsDefinition.patterns.splice | ||
? patternsDefinition.patterns | ||
: purifyPatterns(patternsDefinition.patterns) | ||
).map(function(pattern) { | ||
return preprocessPattern(pattern); | ||
}); | ||
// Prepare cache | ||
(patternsDefinition.exceptions.splice | ||
? patternsDefinition.exceptions | ||
: purifyPatterns(patternsDefinition.exceptions) | ||
).forEach(function(exception) { | ||
cache[exception.replace(/\-/g, "")] = exception.replace( | ||
/\-/g, | ||
hyphenChar | ||
); | ||
}); | ||
// Hyphenator function | ||
return function(text) { | ||
return start(text, patterns, cache, debug, hyphenChar, asyncMode); | ||
}; | ||
}; | ||
return createHyphenator; | ||
}); |
{ | ||
"name": "hyphen", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Franklin M. Liang's hyphenation algorithm, implemented in Javascript.", | ||
"main": "hyphen.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "node build.js", | ||
"lint": "npx eslint hyphen.js", | ||
"start": "npm run watch", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"watch": "npx nodemon -w ./src build.js" | ||
}, | ||
@@ -27,3 +31,8 @@ "repository": { | ||
"patterns" | ||
] | ||
], | ||
"devDependencies": { | ||
"eslint": "^6.8.0", | ||
"nodemon": "^2.0.2", | ||
"prettier": "^2.0.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
3727320
3
305888