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

Function writeSitedWrite

ark/type/standalone/combinators.ts:727–760  ·  view source on GitHub ↗
(
	js: CombinatorCompiler,
	structure: StructureNode,
	failing: boolean
)

Source from the content-addressed store, hash-verified

725// the keys data holds in arktype's order; a default written by a function holds
726// its key's place until each transform has run
727const writeSitedWrite = (
728 js: CombinatorCompiler,
729 structure: StructureNode,
730 failing: boolean
731): string => {
732 // keys written before any that may be absent are a literal's
733 const literal: string[] = []
734 const writes: string[] = []
735 const defaults: string[] = []
736 for (const [i, prop] of structure.inOrder.entries()) {
737 const key = prop.key as string
738 const access = writeAccess(key)
739 const held = writeHeld(prop, i, failing)
740 if (!prop.is("optional")) {
741 if (writes.length || access !== `.${key}`)
742 writes.push(`result${access} = ${held};`)
743 else literal.push(`${key}: ${held}`)
744 continue
745 }
746 const holds = writeHolds(prop)
747 if (!prop.hasDef()) writes.push(`if (${holds}) result${access} = ${held};`)
748 else if (isLiteralDefault(prop)) {
749 writes.push(
750 `result${access} = ${holds} ? ${held} : ${writeLiteral(js, prop.default)};`
751 )
752 } else {
753 writes.push(`result${access} = ${holds} ? ${held} : void 0;`)
754 defaults.push(
755 `if (!(${holds})) values.defaulted(checkers[${i}].w, result);`
756 )
757 }
758 }
759 return `(data, checkers, values) => { const result = { ${literal.join(", ")} }; ${writes.join(" ")} ${defaults.length ? `if (!values.failed()) { ${defaults.join(" ")} }` : ""} return result; }`
760}
761
762// a shape keeping the keys it doesn't declare copies data where one it declares
763// changes: in order where nothing can fail, else each transform, then each default

Callers 1

writeStructureFunction · 0.85

Calls 7

writeAccessFunction · 0.85
writeHeldFunction · 0.85
writeHoldsFunction · 0.85
isLiteralDefaultFunction · 0.85
writeLiteralFunction · 0.85
isMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected