diff --git a/dist/src/Utils.js b/dist/src/Utils.js index 604a7b6e..af88dafe 100644 --- a/dist/src/Utils.js +++ b/dist/src/Utils.js @@ -41,13 +41,15 @@ class Utils { return obj; } static stringToUnicode(string) { - const radix = 16, unicodeSliceValue = -4; - let regexp = new RegExp('[\x00-\x7F]'); + const radix = 16; + let regexp = new RegExp('[\x00-\x7F]'), template; return `'${string.replace(/[\s\S]/g, (escape) => { if (regexp.test(escape)) { - return '\\x' + escape.charCodeAt(0).toString(radix); + template = '0'.repeat(2); + return `\\x${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`; } - return `\\u${('0000' + escape.charCodeAt(0).toString(radix)).slice(unicodeSliceValue)}`; + template = '0'.repeat(4); + return `\\u${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`; })}'`; } } diff --git a/src/Utils.ts b/src/Utils.ts index 8044e0e2..fcf9e46e 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -97,17 +97,21 @@ export class Utils { * @returns {string} */ public static stringToUnicode (string: string): string { - const radix: number = 16, - unicodeSliceValue: number = -4; + const radix: number = 16; - let regexp: RegExp = new RegExp('[\x00-\x7F]'); + let regexp: RegExp = new RegExp('[\x00-\x7F]'), + template: string; return `'${string.replace(/[\s\S]/g, (escape: string): string => { if (regexp.test(escape)) { - return '\\x' + escape.charCodeAt(0).toString(radix); + template = '0'.repeat(2); + + return `\\x${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`; } - - return `\\u${('0000' + escape.charCodeAt(0).toString(radix)).slice(unicodeSliceValue)}`; + + template = '0'.repeat(4); + + return `\\u${(template + escape.charCodeAt(0).toString(radix)).slice(-template.length)}`; })}'`; } }