( data: unknown, opts: SerializationOptions, seen: unknown[] )
| 185 | const quotedMarkedValue = /"\uE000(-?\d+n|undefined)"/g |
| 186 | |
| 187 | const _serialize = ( |
| 188 | data: unknown, |
| 189 | opts: SerializationOptions, |
| 190 | seen: unknown[] |
| 191 | ): unknown => { |
| 192 | switch (domainOf(data)) { |
| 193 | case "object": { |
| 194 | const o = data as object |
| 195 | const toJson = |
| 196 | !opts.onAccessor || isDate(o) ? toJsonMethodOf(o) : undefined |
| 197 | if (toJson) return Reflect.apply(toJson, o, []) |
| 198 | if (typeof o === "function") |
| 199 | return opts.onFunction?.(o) ?? printableOpts.onFunction(o) |
| 200 | |
| 201 | if (seen.includes(o)) return opts.onCycle?.(o) ?? "(cycle)" |
| 202 | |
| 203 | const nextSeen = [...seen, o] |
| 204 | if (isArray(o)) return o.map(item => _serialize(item, opts, nextSeen)) |
| 205 | |
| 206 | if (isDate(o)) return o.toDateString() |
| 207 | |
| 208 | const result: Record<string, unknown> = {} |
| 209 | for (const [k, v] of ownEntriesOf(o, opts)) { |
| 210 | const key = |
| 211 | typeof k === "symbol" ? (opts.onSymbol?.(k) ?? k.toString()) : k |
| 212 | defineValue(result, key, _serialize(v, opts, nextSeen)) |
| 213 | } |
| 214 | |
| 215 | return result |
| 216 | } |
| 217 | case "symbol": |
| 218 | return ( |
| 219 | opts.onSymbol?.(data as symbol) ?? |
| 220 | printableOpts.onSymbol(data as symbol) |
| 221 | ) |
| 222 | case "bigint": |
| 223 | return opts.onBigInt?.(data as bigint) ?? `${data}n` |
| 224 | case "undefined": |
| 225 | return opts.onUndefined ?? "undefined" |
| 226 | case "string": |
| 227 | return (data as string).replace(/\\/g, "\\\\") |
| 228 | default: |
| 229 | return data |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Converts a Date instance to a human-readable description relative to its precision |
no test coverage detected