typia
Version:
Superfast runtime validators with only one line
1 lines • 1.98 kB
Source Map (JSON)
{"version":3,"file":"_jsonStringifyString.mjs","names":[],"sources":["../../src/internal/_jsonStringifyString.ts"],"sourcesContent":["/**\n * In the past, name of `typia` was `typescript-json`, and supported JSON\n * serialization by wrapping `fast-json-stringify. `typescript-json`was a helper\n * library of`fast-json-stringify`, which can skip manual JSON schema definition\n * just by putting pure TypeScript type.\n *\n * This `$string` function is a part of `fast-json-stringify` at that time, and\n * still being used in `typia` for the string serialization.\n *\n * @reference https://github.com/fastify/fast-json-stringify/blob/master/lib/serializer.js\n * @blog https://dev.to/samchon/good-bye-typescript-is-ancestor-of-typia-20000x-faster-validator-49fi\n */\nexport const _jsonStringifyString = (str: string): string => {\n const len = str.length;\n let result = \"\";\n let last = -1;\n let point = 255;\n\n // eslint-disable-next-line\n for (var i = 0; i < len; i++) {\n point = str.charCodeAt(i);\n if (point < 32) {\n return JSON.stringify(str);\n }\n if (point >= 0xd800 && point <= 0xdfff) {\n // The current character is a surrogate.\n return JSON.stringify(str);\n }\n if (\n point === 0x22 || // '\"'\n point === 0x5c // '\\'\n ) {\n last === -1 && (last = 0);\n result += str.slice(last, i) + \"\\\\\";\n last = i;\n }\n }\n\n return (\n (last === -1 && '\"' + str + '\"') || '\"' + result + str.slice(last) + '\"'\n );\n};\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAa,wBAAwB,QAAwB;CAC3D,MAAM,MAAM,IAAI;CAChB,IAAI,SAAS;CACb,IAAI,OAAO;CACX,IAAI,QAAQ;CAGZ,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,KAAK;EAC5B,QAAQ,IAAI,WAAW,CAAC;EACxB,IAAI,QAAQ,IACV,OAAO,KAAK,UAAU,GAAG;EAE3B,IAAI,SAAS,SAAU,SAAS,OAE9B,OAAO,KAAK,UAAU,GAAG;EAE3B,IACE,UAAU,MACV,UAAU,IACV;GACA,SAAS,OAAO,OAAO;GACvB,UAAU,IAAI,MAAM,MAAM,CAAC,IAAI;GAC/B,OAAO;EACT;CACF;CAEA,OACG,SAAS,MAAM,OAAM,MAAM,QAAQ,OAAM,SAAS,IAAI,MAAM,IAAI,IAAI;AAEzE"}