(string: string)
| 25 | } |
| 26 | |
| 27 | export function decodeUnicodeEscapeSequence(string: string): string { |
| 28 | let result = ''; |
| 29 | let state: 'string' | 'escape' | 'digit' = 'string'; |
| 30 | let digits = ''; |
| 31 | |
| 32 | for (let i = 0; i < string.length; i++) { |
| 33 | const char = string[i]!; |
| 34 | |
| 35 | switch (state) { |
| 36 | case 'string': { |
| 37 | if (char === '\\') { |
| 38 | state = 'escape'; |
| 39 | } else { |
| 40 | result += char; |
| 41 | } |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | case 'escape': { |
| 46 | if (char !== 'u') { |
| 47 | throw new SyntaxError('invalid escape sequence'); |
| 48 | } |
| 49 | state = 'digit'; |
| 50 | break; |
| 51 | } |
| 52 | |
| 53 | case 'digit': { |
| 54 | if (HEX_DIGIT.test(char)) { |
| 55 | digits += char; |
| 56 | } else { |
| 57 | throw new SyntaxError('invalid escape sequence'); |
| 58 | } |
| 59 | if (digits.length === 4) { |
| 60 | result += String.fromCharCode(Number.parseInt(digits, 16)); |
| 61 | state = 'string'; |
| 62 | digits = ''; |
| 63 | } |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (state !== 'string') { |
| 70 | throw new SyntaxError('invalid escape sequence'); |
| 71 | } |
| 72 | |
| 73 | return result; |
| 74 | } |
no outgoing calls
no test coverage detected