MCPcopy Create free account
hub / github.com/arktypeio/arktype / _serialize

Function _serialize

ark/util/serialize.ts:187–231  ·  view source on GitHub ↗
(
	data: unknown,
	opts: SerializationOptions,
	seen: unknown[]
)

Source from the content-addressed store, hash-verified

185const quotedMarkedValue = /"\uE000(-?\d+n|undefined)"/g
186
187const _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

Callers 2

snapshotFunction · 0.85
printableFunction · 0.85

Calls 9

domainOfFunction · 0.90
isDateFunction · 0.90
toJsonMethodOfFunction · 0.90
ownEntriesOfFunction · 0.90
defineValueFunction · 0.90
replaceMethod · 0.80
mapMethod · 0.65
toStringMethod · 0.65
applyMethod · 0.45

Tested by

no test coverage detected