Permalink
Browse files

Updated Hypher.

  • Loading branch information...
bramstein committed May 6, 2012
1 parent 35af221 commit be92f6907850a742aa52a728ffad347ea8c03d2d
Showing with 26 additions and 68 deletions.
  1. +26 −68 lib/hypher.js
View
@@ -3,7 +3,7 @@
* @param {!{patterns: !Object, leftmin: !number, rightmin: !number}} language The language pattern file. Compatible with Hyphenator.js.
* @param {?Object=} options Options to alter Hypher's hyphenation behaviour.
*/
function Hypher(language, options) {
function Hypher(language) {
/**
* @type {!Hypher.TrieNode}
@@ -22,28 +22,6 @@ function Hypher(language, options) {
*/
this.rightMin = language['rightmin'];
/**
* @type {!number}
* @const
*/
this.minLength = (options && options['minLength']) || 4;
/**
* @type {!string}
* @const
*/
this.anyChar = (options && options['anyChar']) || '_';
/**
* @type {!Array.<!string>}
*/
this.compoundSeperators = (options && options['compoundSeperators']) || ['\\u002D', '\\u2010', '\\u2013'];
/**
* @type {!RegExp}
*/
this.compoundSeperatorsRegex = new RegExp('([' + this.compoundSeperators.join('') + '])', 'g');
/**
* @type {!Object.<string, !Array.<string>>}
*/
@@ -108,12 +86,27 @@ Hypher.prototype.createTrie = function (patternObject) {
* @param {!string} str The text to hyphenate.
* @return {!string} The same text with soft hyphens inserted in the right positions.
*/
Hypher.prototype.hyphenateText = function (str) {
// TODO: add support for compound words, ZWNJ, etc here
// instead of at the word level.
Hypher.prototype.hyphenateText = function (str, minLength) {
minLength = minLength || 4;
// Regexp("\b", "g") splits on word boundaries,
// compound separators and ZWNJ so we don't need
// any special cases for those characters.
var words = str.split(/\b/g);
return words.map(function (word) {
return this.hyphenate(word).join('\u00AD');
return words.map(function (word, i) {
if (word.indexOf('/') !== -1) {
// Don't insert a zero width space if the slash is at the beginning or end
// of the text, or right after or before a space.
if (i === 0 || i === words.length -1 || /\s+\/|\/\s+/.test(word)) {
return word;
} else {
return word + '\u200B';
}
} else if (word.length <= minLength) {
return word;
} else {
return this.hyphenate(word).join('\u00AD');
}
}, this).join('');
};
@@ -137,43 +130,17 @@ Hypher.prototype.hyphenate = function (word) {
nodePointsLength,
m = Math.max,
trie = this.trie,
result = [''],
parts,
part,
hyphenatedParts,
partsLength;
result = [''];
if (this.exceptions[word]) {
if (this.exceptions.hasOwnProperty(word)) {
return this.exceptions[word];
}
if (word.length <= this.minLength || word.indexOf('\u00AD') !== -1) {
if (word.indexOf('\u00AD') !== -1) {
return [word];
}
if (this.compoundSeperatorsRegex.test(word)) {
parts = word.split(this.compoundSeperatorsRegex);
partsLength = parts.length;
for (i = 0; i < partsLength; i += 1) {
part = parts[i];
if (this.compoundSeperators.indexOf(part) === -1) {
hyphenatedParts = this.hyphenate(part);
for (j = 0; j < hyphenatedParts.length; j += 1) {
if (j === 0) {
result[result.length - 1] += hyphenatedParts[j];
} else {
result.push(hyphenatedParts[j]);
}
}
} else {
result[result.length - 1] += part;
}
}
return result;
}
word = this.anyChar + word + this.anyChar;
word = '_' + word + '_';
characters = word.toLowerCase().split('');
originalCharacters = word.split('');
@@ -211,13 +178,4 @@ Hypher.prototype.hyphenate = function (word) {
}
return result;
};
// Export Hypher for Node.js
//module.exports = {
// Hypher: Hypher
//};
// Uncomment these lines if you run Hypher on a Web browser or through Google Closure Compiler.
window['Hypher'] = Hypher;
window['Hypher'].prototype['hyphenate'] = Hypher.prototype.hyphenate;
};

0 comments on commit be92f69

Please sign in to comment.