UNPKG

transliteration

Version:

Unicode to ACSII transliteration / slugify module. Works in node.js, web browser and command line.

101 lines (92 loc) 2.85 kB
import { ucs2decode, fixChineseSpace, escapeRegExp, mergeOptions } from './utils'; let codemap = {}; const defaultOptions = { unknown: '[?]', replace: [], replaceAfter: [], ignore: [], }; let configOptions = {}; /* istanbul ignore next */ export const replaceStr = (str, replace) => { for (const item of replace) { if (item[0] instanceof RegExp) { let flag = item[0].flags; if (!item[0].global) { flag += 'g'; item[0] = new RegExp(item[0].toString().replace(/^\/|\/$/), flag); } } else if (typeof item[0] === 'string') { item[0] = new RegExp(escapeRegExp(item[0]), 'g'); } if (item[0] instanceof RegExp) { str = str.replace(item[0], item[1]); } } return str; }; /** * @param {string} str The string which is being transliterated * @param {object} options options */ /* istanbul ignore next */ const transliterate = (str, options) => { options = options ? mergeOptions(defaultOptions, options) : mergeOptions(defaultOptions, configOptions); str = String(str); if (options.ignore instanceof Array && options.ignore.length > 0) { for (const i in options.ignore) { const splitted = str.split(options.ignore[i]); const result = []; for (const j in splitted) { const ignore = options.ignore.slice(0); ignore.splice(i, 1); result.push(transliterate(splitted[j], mergeOptions(options, { ignore }))); } return result.join(options.ignore[i]); } } str = replaceStr(str, options.replace); const strArr = ucs2decode(fixChineseSpace(str)); const strArrNew = []; for (let ord of strArr) { // These characters are also transliteratable. Will improve it later if needed if (ord > 0xffff) { strArrNew.push(options.unknown); } else { const offset = ord >> 8; if (typeof codemap[offset] === 'undefined') { try { codemap[offset] = require(`../../data/x${offset.toString(16)}.json`); } catch (e) { codemap[offset] = []; } } ord &= 0xff; const t = codemap[offset][ord]; if (typeof t === 'undefined' || t === null) { strArrNew.push(options.unknown); } else { strArrNew.push(codemap[offset][ord]); } } } // trim spaces at the begining and ending of the string if (strArrNew.length > 1) { options.replaceAfter.push([/(^ +?)|( +?$)/g, '']); } let strNew = strArrNew.join(''); strNew = replaceStr(strNew, options.replaceAfter); return strNew; }; transliterate.setCodemap = customCodemap => { codemap = customCodemap || codemap; return codemap; }; transliterate.config = options => { if (options === undefined) { return configOptions; } configOptions = mergeOptions(defaultOptions, options); return configOptions; }; export default transliterate;