mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
33 lines
956 B
JavaScript
33 lines
956 B
JavaScript
module.exports.wordTrans = (text, words) => {
|
|
text = text.split(' ');
|
|
let translation = [];
|
|
for (let i = 0; i < text.length; i++) {
|
|
const word = text[i].toLowerCase();
|
|
const wordPuncStrip = word.replace(/[\[\\^$.|?*+()\]]/g, '');
|
|
if (words[wordPuncStrip]) {
|
|
const reg = new RegExp(wordPuncStrip, 'gi');
|
|
translation.push(word.replace(reg, words[wordPuncStrip]));
|
|
}
|
|
else {
|
|
translation.push(word);
|
|
}
|
|
}
|
|
return translation.join(' ');
|
|
};
|
|
|
|
module.exports.letterTrans = (text, letters, joinWith) => {
|
|
text = text.split('');
|
|
let translation = [];
|
|
for (let i = 0; i < text.length; i++) {
|
|
const letter = text[i];
|
|
if (letters[letter]) {
|
|
translation.push(letters[letter]);
|
|
}
|
|
else {
|
|
translation.push(letter);
|
|
}
|
|
}
|
|
joinWith = joinWith || '';
|
|
return translation.join(joinWith);
|
|
};
|