Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -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'),
Expand All @@ -145,19 +145,28 @@ 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;

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]);
Expand Down