MCPcopy Create free account
hub / github.com/Effect-TS/effect / dropUndefinedOf

Function dropUndefinedOf

packages/effect/src/ui/Query.ts:2478–2506  ·  view source on GitHub ↗
(
  ast: SchemaAST.AST
)

Source from the content-addressed store, hash-verified

2476 * Args with nothing to drop are returned as they are, not as a copy.
2477 */
2478const dropUndefinedOf = (
2479 ast: SchemaAST.AST
2480): ((args: unknown) => unknown) | null => {
2481 if (ast._tag !== "Objects") return null
2482 const optional = ast.propertySignatures.flatMap((property) => {
2483 const type = property.type
2484 // Asked on first use: the type may be behind a `suspend` that can't be followed yet.
2485 return SchemaAST.isOptional(type)
2486 ? [{ name: property.name, allowed: lazy(() => namesUndefined(type)) }]
2487 : []
2488 })
2489 if (optional.length === 0) return null
2490 return (args) => {
2491 if (typeof args !== "object" || args === null || Array.isArray(args)) {
2492 return args
2493 }
2494 let names: Array<PropertyKey> | undefined
2495 for (const { allowed, name } of optional) {
2496 const descriptor = Object.getOwnPropertyDescriptor(args, name)
2497 if (
2498 descriptor !== undefined && "value" in descriptor &&
2499 descriptor.value === undefined && allowed()
2500 ) {
2501 ;(names ??= []).push(name)
2502 }
2503 }
2504 return names === undefined ? args : copyWithout(args, names)
2505 }
2506}
2507
2508/**
2509 * The JSON encoder of an args Schema, run on the args once the optional

Callers 2

argsEncoderFunction · 0.85
makeDefFunction · 0.85

Calls 5

namesUndefinedFunction · 0.85
copyWithoutFunction · 0.85
lazyFunction · 0.70
pushMethod · 0.65
allowedFunction · 0.50

Tested by

no test coverage detected