(s: string)
| 43 | * |
| 44 | */ |
| 45 | export function escapeJsonString(s: string): string { |
| 46 | const result: string[] = []; |
| 47 | for (const codePoint of Array.from(s)) { |
| 48 | if (codePoint === undefined) break; |
| 49 | const code = codePoint.codePointAt(0)!; |
| 50 | if (JSON_ESCAPE_CHARS[code]) { |
| 51 | result.push(JSON_ESCAPE_CHARS[code]); |
| 52 | } else if (code > 0xffff) { |
| 53 | const high = Math.floor((code - 0x10000) / 0x400) + 0xd800; |
| 54 | const low = ((code - 0x10000) % 0x400) + 0xdc00; |
| 55 | result.push(`\\u${high.toString(16).padStart(4, '0')}`); |
| 56 | result.push(`\\u${low.toString(16).padStart(4, '0')}`); |
| 57 | } else if (code < 0x20) { |
| 58 | result.push(`\\u${code.toString(16).padStart(4, '0')}`); |
| 59 | } else { |
| 60 | result.push(codePoint); |
| 61 | } |
| 62 | } |
| 63 | return result.join(''); |
| 64 | } |
| 65 | |
| 66 | export function unescapeJsonString(s: string | any): string { |
| 67 | if (typeof s !== 'string') return s; |
no test coverage detected