typia
Version:
Superfast runtime validators with only one line
1 lines • 2.98 kB
Source Map (JSON)
{"version":3,"file":"_jsonStringifyArray.mjs","names":[],"sources":["../../src/internal/_jsonStringifyArray.ts"],"sourcesContent":["/**\n * Serializes the elements of an array the way ECMAScript `JSON.stringify` does.\n *\n * `SerializeJSONArray` walks index `0` to `LengthOfArrayLike(value) - 1` and\n * writes `null` wherever the element serializes to `undefined`. Neither\n * `Array.prototype.map` nor `Array.prototype.join` reproduces that:\n *\n * - `map` never visits a hole and leaves one behind, and `join` renders a hole as\n * empty text, so a sparse array joined into malformed text such as `[,1]`. A\n * hole exists at runtime whatever the element type declares, so this is not\n * an `any` concern.\n * - `join` renders a mapped `undefined` as empty text too, which is what an `any`\n * or `unknown` element holding a function, a symbol, or a `toJSON` that\n * returns nothing serializes to.\n *\n * The length is converted with `ToLength` and read once, which is both what\n * `JSON.stringify` does and what `Array.prototype.every` - the traversal\n * typia's own array checkers emit - does, so the checker and the serializer\n * walk one index range rather than two that merely usually coincide.\n *\n * @param elements Array being serialized.\n * @param mapper Serializer of one element, emitted by the transform.\n * @returns Comma separated element text, without the enclosing brackets.\n * @internal\n */\nexport const _jsonStringifyArray = <T>(\n elements: ArrayLike<T>,\n mapper: (elem: T, index: number) => string,\n): string => {\n const length: number = Math.min(\n Math.max(Math.trunc(elements.length) || 0, 0),\n Number.MAX_SAFE_INTEGER,\n );\n let output: string = \"\";\n for (let i: number = 0; i < length; ++i) {\n const elem: T | undefined = elements[i];\n // A hole and an explicit `undefined` are answered without calling the\n // mapper, because the mapper is the element type's own serializer and a\n // typed one has no answer for a missing value. Everything else is answered\n // from the mapper's *result*: an element that is present but serializes to\n // nothing — a function, a symbol, or a `toJSON` returning nothing in an\n // `any` slot — is `null` in an array position, exactly as\n // SerializeJSONArray specifies, and testing the input instead would let its\n // text become the literal `undefined`.\n const text: string | undefined =\n elem === undefined ? undefined : mapper(elem, i);\n output += (i === 0 ? \"\" : \",\") + (text === undefined ? \"null\" : text);\n }\n return output;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,MAAa,uBACX,UACA,WACW;CACX,MAAM,SAAiB,KAAK,IAC1B,KAAK,IAAI,KAAK,MAAM,SAAS,MAAM,KAAK,GAAG,CAAC,GAC5C,OAAO,gBACT;CACA,IAAI,SAAiB;CACrB,KAAK,IAAI,IAAY,GAAG,IAAI,QAAQ,EAAE,GAAG;EACvC,MAAM,OAAsB,SAAS;EASrC,MAAM,OACJ,SAAS,KAAA,IAAY,KAAA,IAAY,OAAO,MAAM,CAAC;EACjD,WAAW,MAAM,IAAI,KAAK,QAAQ,SAAS,KAAA,IAAY,SAAS;CAClE;CACA,OAAO;AACT"}