UNPKG

typia

Version:

Superfast runtime validators with only one line

34 lines (33 loc) 1.26 kB
//#region src/internal/_jsonStringifyString.ts /** * In the past, name of `typia` was `typescript-json`, and supported JSON * serialization by wrapping `fast-json-stringify. `typescript-json`was a helper * library of`fast-json-stringify`, which can skip manual JSON schema definition * just by putting pure TypeScript type. * * This `$string` function is a part of `fast-json-stringify` at that time, and * still being used in `typia` for the string serialization. * * @reference https://github.com/fastify/fast-json-stringify/blob/master/lib/serializer.js * @blog https://dev.to/samchon/good-bye-typescript-is-ancestor-of-typia-20000x-faster-validator-49fi */ const _jsonStringifyString = (str) => { const len = str.length; let result = ""; let last = -1; let point = 255; for (var i = 0; i < len; i++) { point = str.charCodeAt(i); if (point < 32) return JSON.stringify(str); if (point >= 55296 && point <= 57343) return JSON.stringify(str); if (point === 34 || point === 92) { last === -1 && (last = 0); result += str.slice(last, i) + "\\"; last = i; } } return last === -1 && "\"" + str + "\"" || "\"" + result + str.slice(last) + "\""; }; //#endregion export { _jsonStringifyString }; //# sourceMappingURL=_jsonStringifyString.mjs.map