MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / escapeJsonString

Function escapeJsonString

src/common/json.ts:45–64  ·  view source on GitHub ↗
(s: string)

Source from the content-addressed store, hash-verified

43 *
44 */
45export 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
66export function unescapeJsonString(s: string | any): string {
67 if (typeof s !== 'string') return s;

Callers 1

parse-cortex.tsFile · 0.90

Calls 3

toStringMethod · 0.65
fromMethod · 0.45
floorMethod · 0.45

Tested by

no test coverage detected