ES6 Plato on Github
Report Home
Summary Display
dictionary-escaper.mjs
Maintainability
90.31
Lines of code
39
Difficulty
15.45
Estimated Errors
0.47
Function weight
By Complexity
By SLOC
const specialMap = { '(': String.fromCharCode(0x2001), ')': String.fromCharCode(0x2002), '|': String.fromCharCode(0x2003), '{': String.fromCharCode(0x2004), '}': String.fromCharCode(0x2005), ',': String.fromCharCode(0x2006), '[': String.fromCharCode(0x2007), ']': String.fromCharCode(0x2008), '-': String.fromCharCode(0x2009), '<': String.fromCharCode(0x200a), '>': String.fromCharCode(0x200b), ':': String.fromCharCode(0x200c), '=': String.fromCharCode(0x200d), '@': String.fromCharCode(0x200e), '\\': String.fromCharCode(0x200f), }; const revertSpecial = swapKeyValue(specialMap); function swapKeyValue(object) { const result = {}; for (const key in object) { result[object[key]] = key; } return result; } export function escape2utfSpecial(str) { return str.replace(/\\(.)/g, (a, chr) => specialMap[chr] ? specialMap[chr] : chr); } export function utfSpecial2unEscaped(str) { return str.split('').map(chr => revertSpecial[chr] ? revertSpecial[chr] : chr).join(''); } export function utfSpecial2escaped(str) { return str.split('').map(chr => revertSpecial[chr] ? `\\${revertSpecial[chr]}` : chr).join(''); } export function escapeAll(str){ return str.replace(/./g, chr => specialMap[chr] ? specialMap[chr] : chr); }