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

Function serialize

ark/schema/units.ts:1081–1118  ·  view source on GitHub ↗
(value: unknown)

Source from the content-addressed store, hash-verified

1079 const serializing = new Set<object>()
1080 let holdsIdentity = false
1081 const serialize = (value: unknown): string => {
1082 if (isNode(value)) {
1083 link.byId[value.id] ??= value
1084 return positionOf(value.id)
1085 }
1086 if (typeof value === "string")
1087 return s.opts.keyOf?.(value) ?? serializePrimitive(value)
1088 if (!hasDomain(value, "object") && typeof value !== "symbol")
1089 return Object.is(value, -0) ? "-0" : serializePrimitive(value as never)
1090 const key =
1091 s.opts.keyOf?.(value) ??
1092 // arktype's own pattern instance is keyed by its source
1093 (value instanceof RegExp && isPatternInstanceIn(node, value) ?
1094 `${value}`
1095 : undefined)
1096 if (key !== undefined) return key
1097 const prototype = typeof value === "object" && Object.getPrototypeOf(value)
1098 // a value containing itself is keyed by identity
1099 if (
1100 serializing.has(value as object) ||
1101 (!isArray(value) && prototype !== Object.prototype && prototype !== null)
1102 ) {
1103 let i = s.valueIndices.get(value)
1104 if (i === undefined) s.valueIndices.set(value, (i = s.valueIndices.size))
1105 holdsIdentity = true
1106 return `v${i}`
1107 }
1108 serializing.add(value as object)
1109 const isFailSite = failSites.has(value as object)
1110 const content =
1111 isArray(value) ?
1112 `[${value.map(serialize)}]`
1113 : `{${Object.entries(value)
1114 .filter(([k]) => !isFailSite || (k !== "vb" && k !== "mb"))
1115 .map(([k, v]) => `${JSON.stringify(k)}: ${serialize(v)}`)}}`
1116 serializing.delete(value as object)
1117 return content
1118 }
1119 const content = [
1120 node.kind,
1121 // a program's nodes stay apart from another scope's

Callers 2

compileContentFunction · 0.70
writeInnerJsonFunction · 0.70

Calls 14

isNodeFunction · 0.90
serializePrimitiveFunction · 0.90
hasDomainFunction · 0.90
positionOfFunction · 0.85
isPatternInstanceInFunction · 0.85
keyOfMethod · 0.80
isMethod · 0.80
filterMethod · 0.80
getMethod · 0.65
mapMethod · 0.65
hasMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected