UNPKG

@technobuddha/library

Version:
41 lines (40 loc) 1.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.unescapeJS = void 0; /** * Unescape a string encoded in Javascript style * * @param input the string to unescape * @returns the string with escapes resolved */ function unescapeJS(input) { return input.replace( // cspell:disable-next-line /\\(([bfnrtv"'\\])|([0-7]{1,3})|(x[0-9a-fA-F]{2})|(u[0-9a-fA-F]{4})|(u\{[0-9a-fA-F]{1,}\})|.)/ug, function (escape) { var c = escape.charAt(1); if (c === 'b') return '\b'; if (c === 'f') return '\f'; if (c === 'n') return '\n'; if (c === 'r') return '\r'; if (c === 't') return '\t'; if (c === 'v') return '\v'; if (c >= '0' && c <= '7') return String.fromCharCode(Number.parseInt(escape.slice(1), 8)); if (c === 'x') return String.fromCharCode(Number.parseInt(escape.slice(2), 16)); if (c === 'u') { if (escape.charAt(2) === '{') return String.fromCodePoint(Number.parseInt(escape.slice(3, -1), 16)); return String.fromCharCode(Number.parseInt(escape.slice(2), 16)); } return escape.slice(1); }); } exports.unescapeJS = unescapeJS; exports.default = unescapeJS;