diff --git a/lib/index.js b/lib/index.js index ead0cf1..09e1c67 100644 --- a/lib/index.js +++ b/lib/index.js @@ -20,17 +20,17 @@ function getDictionary(name, options = {}) { }, {})); } -function convertChain(input, chains) { +function convertChain(input, chains, options) { return chains.reduce((input, chain) => { const dictionaries = chain.slice(); dictionaries.splice(0, 0, {}); - return translate(input, Object.assign.apply(null, dictionaries)); + return translate(input, Object.assign.apply(null, dictionaries), options); }, input); } -exports.hongKongToSimplified = function (text) { +exports.hongKongToSimplified = function (text, options) { return convertChain(text, [ [ getDictionary('HKVariantsRevPhrases'), @@ -40,10 +40,10 @@ exports.hongKongToSimplified = function (text) { getDictionary('TSPhrases'), getDictionary('TSCharacters') ], - ]); + ], options); }; -exports.simplifiedToHongKong = function (text) { +exports.simplifiedToHongKong = function (text, options) { return convertChain(text, [ [ getDictionary('STPhrases'), @@ -53,19 +53,19 @@ exports.simplifiedToHongKong = function (text) { getDictionary('HKVariantsPhrases'), getDictionary('HKVariants') ] - ]); + ], options); }; -exports.simplifiedToTraditional = function (text) { +exports.simplifiedToTraditional = function (text, options) { return convertChain(text, [ [ getDictionary('STPhrases'), getDictionary('STCharacters') ] - ]); + ], options); }; -exports.simplifiedToTaiwan = function (text) { +exports.simplifiedToTaiwan = function (text, options) { return convertChain(text, [ [ getDictionary('STPhrases'), @@ -74,10 +74,10 @@ exports.simplifiedToTaiwan = function (text) { [ getDictionary('TWVariants') ] - ]); + ], options); }; -exports.simplifiedToTaiwanWithPhrases = function (text) { +exports.simplifiedToTaiwanWithPhrases = function (text, options) { return convertChain(text, [ [ getDictionary('STPhrases'), @@ -89,35 +89,35 @@ exports.simplifiedToTaiwanWithPhrases = function (text) { getDictionary('TWPhrasesOther'), getDictionary('TWVariants') ] - ]); + ], options); }; -exports.traditionalToHongKong = function (text) { +exports.traditionalToHongKong = function (text, options) { return convertChain(text, [ [ getDictionary('HKVariants') ] - ]); + ], options); }; -exports.traditionalToSimplified = function (text) { +exports.traditionalToSimplified = function (text, options) { return convertChain(text, [ [ getDictionary('TSPhrases'), getDictionary('TSCharacters') ] - ]); + ], options); }; -exports.traditionalToTaiwan = function (text) { +exports.traditionalToTaiwan = function (text, options) { return convertChain(text, [ [ getDictionary('TWVariants') ] - ]); + ], options); }; -exports.taiwanToSimplified = function (text) { +exports.taiwanToSimplified = function (text, options) { return convertChain(text, [ [ getDictionary('TWVariantsRevPhrases'), @@ -127,10 +127,10 @@ exports.taiwanToSimplified = function (text) { getDictionary('TSPhrases'), getDictionary('TSCharacters') ] - ]); + ], options); }; -exports.taiwanToSimplifiedWithPhrases = function (text) { +exports.taiwanToSimplifiedWithPhrases = function (text, options) { return convertChain(text, [ [ getDictionary('TWVariantsRevPhrases'), @@ -145,12 +145,19 @@ exports.taiwanToSimplifiedWithPhrases = function (text) { getDictionary('TSPhrases'), getDictionary('TSCharacters') ] - ]); + ], options); }; -function translate(text, dictionary) { +function translate(text, dictionary, options) { const maxLength = Object.keys(dictionary).reduce((maxLength, word) => Math.max(maxLength, word.length), 0); const translated = []; + options = options || {}; + + if (options.skip) { + if (typeof options.skip == 'string') { + options.skip = options.skip.split(''); + } + } for (let i = 0, { length } = text; i < length; i++) { let found; @@ -158,6 +165,8 @@ function translate(text, dictionary) { for (let j = maxLength; j > 0; j--) { const target = text.substr(i, j); + if (options.skip && options.skip.includes(target)) continue; + if (Object.hasOwnProperty.call(dictionary, target)) { i += j - 1; translated.push(dictionary[target]);