@internal
(
compile: SchemaParser.Compiler,
compileConstructorDefault: SchemaParser.Compiler = compile
)
| 1718 | } |
| 1719 | /** @internal */ |
| 1720 | getParser( |
| 1721 | compile: SchemaParser.Compiler, |
| 1722 | compileConstructorDefault: SchemaParser.Compiler = compile |
| 1723 | ): SchemaParser.Parser { |
| 1724 | // oxlint-disable-next-line @typescript-eslint/no-this-alias |
| 1725 | const ast = this |
| 1726 | type ElementParser = { readonly ast: AST; readonly parser: SchemaParser.Parser } |
| 1727 | let elements: Array<ElementParser> | undefined |
| 1728 | let rest: Array<ElementParser> | undefined |
| 1729 | const elementLen = ast.elements.length |
| 1730 | const tailLen = Math.max(0, ast.rest.length - 1) |
| 1731 | |
| 1732 | function getParser( |
| 1733 | tailThreshold: number, |
| 1734 | index: number |
| 1735 | ): { readonly ast: AST; readonly parser: SchemaParser.Parser } { |
| 1736 | if (index < elementLen) { |
| 1737 | return elements![index] |
| 1738 | } else if (index >= tailThreshold) { |
| 1739 | return rest![index - tailThreshold + 1] |
| 1740 | } |
| 1741 | return rest![0] |
| 1742 | } |
| 1743 | |
| 1744 | return Effect.fnUntracedEager(function*(input, options) { |
| 1745 | if (input === InternalParser.missing) { |
| 1746 | return InternalParser.missing |
| 1747 | } |
| 1748 | |
| 1749 | // If the input is not an array, return early with an error |
| 1750 | if (!Array.isArray(input)) { |
| 1751 | return yield* Effect.fail(new SchemaIssue.InvalidType(ast, input, options)) |
| 1752 | } |
| 1753 | if (!elements) { |
| 1754 | elements = ast.elements.map((ast) => ({ ast, parser: compileConstructorDefault(ast) })) |
| 1755 | rest = ast.rest.map((ast) => ({ ast, parser: compileConstructorDefault(ast) })) |
| 1756 | } |
| 1757 | |
| 1758 | const len = input.length |
| 1759 | const state = { |
| 1760 | ast, |
| 1761 | getParser, |
| 1762 | input, |
| 1763 | len, |
| 1764 | tailThreshold: Math.max(elementLen, len - tailLen), |
| 1765 | output: new globalThis.Array(len), |
| 1766 | issues: undefined as Arr.NonEmptyArray<SchemaIssue.Issue> | undefined, |
| 1767 | options |
| 1768 | } |
| 1769 | const concurrency = resolveConcurrency(options?.concurrency) |
| 1770 | const eff = parseArray(state, input, { |
| 1771 | concurrency: concurrency?.concurrency, |
| 1772 | end: ast.rest.length === 0 ? elementLen : Math.max(len, elementLen + tailLen) |
| 1773 | }) |
| 1774 | if (eff) yield* eff |
| 1775 | |
| 1776 | // --------------------------------------------- |
| 1777 | // handle excess indexes |
nothing calls this directly
no test coverage detected