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

Method jsApply

ark/schema/roots/intersection.ts:391–450  ·  view source on GitHub ↗

writes its Apply: a failed basis or prestructural stops it, then its structure and predicates run in turn

(js: NodeCompiler)

Source from the content-addressed store, hash-verified

389
390 /** writes its Apply: a failed basis or prestructural stops it, then its structure and predicates run in turn */
391 private jsApply(js: NodeCompiler): void {
392 const { basis, preStruct, structure } = this
393 const predicates = this.inner.predicate
394 if (basis) {
395 const condition = basis.jsCond(js, "data")
396 if (this.children.length === 1) {
397 js.return(`${condition} || !${js.error(basis)}`)
398 return
399 }
400 js.if(`!(${condition})`, () => js.return(`!${js.error(basis)}`))
401 }
402 if (!structure && !predicates) {
403 for (const node of preStruct) js.check(node)
404 js.return(true)
405 return
406 }
407 const conditions = preStruct.map(node => node.jsCond(js, "data"))
408 if (conditions.length === 1) {
409 js.if(`!(${conditions[0]})`, () =>
410 js.return(`!${js.error(preStruct[0])}`)
411 )
412 } else if (conditions.length) {
413 js.if(`!(${conditions.map(c => `(${c})`).join(" && ")})`, () =>
414 js.return(
415 `!(${conditions.map((c, i) => `(!(${c}) && ${js.error(preStruct[i])})`).join(" || ")})`
416 )
417 )
418 }
419 if (!predicates) {
420 const sequence = this.inlinedSequence
421 if (sequence) sequence.compile(js)
422 else js.return(js.inv(structure!))
423 return
424 }
425 // read only where a later check follows one that can add issues
426 if (structure || predicates.length > 1) js.initErrCount()
427 if (structure) {
428 // transformed only if the predicates allow it
429 if (structure.tfs) {
430 js.if(`!${js.allows(structure)}`, () =>
431 js
432 .if(`!${js.inv(structure)}`, () => js.return(false))
433 .if("ctx.errN > errN", () => js.return(true))
434 )
435 } else {
436 js.if(`!${js.inv(structure)}`, () => js.return(false)).if(
437 "ctx.errN > errN",
438 () => js.return(true)
439 )
440 }
441 }
442 // since predicates can be chained, the first to fail stops this node
443 for (const predicate of predicates.slice(0, -1)) {
444 js.if(`!${js.inv(predicate)}`, () => js.return(false)).if(
445 "ctx.errN > errN",
446 () => js.return(true)
447 )
448 }

Callers 1

compileMethod · 0.95

Calls 9

returnMethod · 0.80
ifMethod · 0.80
allowsMethod · 0.80
mapMethod · 0.65
jsCondMethod · 0.45
errorMethod · 0.45
checkMethod · 0.45
compileMethod · 0.45
invMethod · 0.45

Tested by

no test coverage detected