Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Should we remove the separator before a digit where previous word does not end in a digit?
let mergeDigitSuffixes = false;
// Language-specific behaviour
const lang = options.lang || '';
if (lang.toLowerCase().startsWith('ja') || hepburn.containsKana(text)) {
// Convert from Japanese Kana using Hepburn romanisation
text = hepburn.fromKana(text);
// Remove any remaining non-Kana, e.g. Kanji
text = text.replace(/([^A-Za-z0-9\- ]+)/g, '');
} else if (lang.toLowerCase().startsWith('zh') || /[\u4e00-\u9fa5]+/.test(text)) {
// Should we use tone numbers? (default is true)
const tone = (typeof options.tone === 'boolean') ? options.tone : true;
mergeDigitSuffixes = tone;
text = pinyin(text, {
style: tone ? pinyin.STYLE_TONE2 : pinyin.STYLE_NORMAL
}).join(' ');
// Remove punctuation symbols
const customNonPunctuation = customCharsAsArray(options.custom).map(function (c) { return `\\${c}`; }).join('');
const nonPunctuationMatcher = new RegExp(`([^0-9A-Za-z ${customNonPunctuation}]+)`, 'g');
text = text.replace(nonPunctuationMatcher, '');
// Remove space around single character words, caused by non-Mandarin symbols in otherwise Mandarin text
text = text.replace(/([^1-4]) ([A-Za-z]) /g, '$1$2');
}
// Convert to slug using speakingurl
const separator = options.replacement || options.separator;
const slug = speakingurl(text, {
lang: lang || 'en',
separator: typeof separator === 'string' ? separator : '-',
maintainCase: options.maintainCase || false,
custom: options.custom || {}
});
function getPinyin(Chinese_words){
const py = pinyin(Chinese_words, {
style: pinyin.STYLE_TONE2
});
return py.join('_');
}