( name: string, input: string, steps: readonly TransformStep[], opts?: TransformKeyOptions )
| 574 | } |
| 575 | |
| 576 | tfKey( |
| 577 | name: string, |
| 578 | input: string, |
| 579 | steps: readonly TransformStep[], |
| 580 | opts?: TransformKeyOptions |
| 581 | ): this { |
| 582 | const nodes = steps.map(step => step.node) |
| 583 | const keyExpr = opts?.keyExpr |
| 584 | const pathKeyExpr = opts?.pathKeyExpr ?? keyExpr |
| 585 | const assign = (assignee: string, node: BaseNode, arg: string) => { |
| 586 | const pushesKey = pathKeyExpr !== undefined && node.tfCtx |
| 587 | if (pushesKey) this.line(`${this.ctx}.path.push(${pathKeyExpr})`) |
| 588 | // a step rejecting an earlier step's output transforms the input instead |
| 589 | const transformed = |
| 590 | arg === input ? |
| 591 | this.inv(node, { arg }) |
| 592 | : `${arg} === ${input} || ${this.ref(node)}.allows(${arg}) ? ${this.inv(node, { arg })} : ${this.ref(mergeTransformed)}(${input}, ${arg}, ${this.inv(node, { arg: input })})` |
| 593 | this.line(`${assignee} = ${transformed}`) |
| 594 | return pushesKey ? this.line(`${this.ctx}.path.pop()`) : this |
| 595 | } |
| 596 | const isTransformErrors = () => |
| 597 | `typeof ${name} === "function" && ${name} instanceof ${this.ref(TransformErrors)}` |
| 598 | const assignSteps = () => { |
| 599 | const errN = `${name}ErrorCount` |
| 600 | if (nodes.slice(0, -1).some(node => node.tfCtx)) |
| 601 | this.const(errN, `${this.ctx}.errN`) |
| 602 | for (let i = 0; i < steps.length; i++) { |
| 603 | const { node, signature, indexed } = steps[i] |
| 604 | const previous = nodes.slice(0, i) |
| 605 | const conditions = indexed && opts?.ownKey ? [opts.ownKey] : [] |
| 606 | if (signature) { |
| 607 | conditions.push( |
| 608 | this.inv(signature, { arg: keyExpr!, kind: "Allows" }) |
| 609 | ) |
| 610 | } |
| 611 | if (previous.some(returnsTransformErrors)) |
| 612 | conditions.push(`!(${isTransformErrors()})`) |
| 613 | if (previous.some(node => node.tfCtx)) |
| 614 | conditions.push(`${this.ctx}.errN === ${errN}`) |
| 615 | const assignStep = () => assign(name, node, i === 0 ? input : name) |
| 616 | if (conditions.length) this.if(conditions.join(" && "), assignStep) |
| 617 | else assignStep() |
| 618 | } |
| 619 | return this |
| 620 | } |
| 621 | if ( |
| 622 | steps.length === 1 && |
| 623 | !steps[0].signature && |
| 624 | !(steps[0].indexed && opts?.ownKey) && |
| 625 | !opts?.condition |
| 626 | ) |
| 627 | assign(`const ${name}`, steps[0].node, input) |
| 628 | else { |
| 629 | this.let(name, input) |
| 630 | if (opts?.condition) this.if(opts.condition, assignSteps) |
| 631 | else assignSteps() |
| 632 | } |
| 633 | const onChange = (): this => { |
no test coverage detected