( ...params: Params )
| 921 | * @since 3.10.0 |
| 922 | */ |
| 923 | export const TemplateLiteralParser = <Params extends array_.NonEmptyReadonlyArray<TemplateLiteralParserParameters>>( |
| 924 | ...params: Params |
| 925 | ): TemplateLiteralParser<Params> => { |
| 926 | const encodedSchemas: Array<Schema.Any> = [] |
| 927 | const elements: Array<Schema.Any> = [] |
| 928 | const schemas: Array<Schema.Any> = [] |
| 929 | let coerced = false |
| 930 | for (let i = 0; i < params.length; i++) { |
| 931 | const param = params[i] |
| 932 | const schema = isSchema(param) ? param : Literal(param) |
| 933 | schemas.push(schema) |
| 934 | const encoded = encodedSchema(schema) |
| 935 | encodedSchemas.push(encoded) |
| 936 | const element = getTemplateLiteralParserCoercedElement(encoded, schema) |
| 937 | if (element) { |
| 938 | elements.push(element) |
| 939 | coerced = true |
| 940 | } else { |
| 941 | elements.push(schema) |
| 942 | } |
| 943 | } |
| 944 | const from = TemplateLiteral(...encodedSchemas as any) |
| 945 | const re = AST.getTemplateLiteralCapturingRegExp(from.ast as AST.TemplateLiteral) |
| 946 | let to = Tuple(...elements) |
| 947 | if (coerced) { |
| 948 | to = to.annotations({ [AST.AutoTitleAnnotationId]: format(Tuple(...schemas)) }) |
| 949 | } |
| 950 | return class TemplateLiteralParserClass extends transformOrFail(from, to, { |
| 951 | strict: false, |
| 952 | decode: (i, _, ast) => { |
| 953 | const match = re.exec(i) |
| 954 | return match |
| 955 | ? ParseResult.succeed(match.slice(1, params.length + 1)) |
| 956 | : ParseResult.fail(new ParseResult.Type(ast, i, `${re.source}: no match for ${JSON.stringify(i)}`)) |
| 957 | }, |
| 958 | encode: (tuple) => ParseResult.succeed(tuple.join("")) |
| 959 | }) { |
| 960 | static params = params.slice() |
| 961 | } as any |
| 962 | } |
| 963 | |
| 964 | const declareConstructor = < |
| 965 | const TypeParameters extends ReadonlyArray<Schema.Any>, |
nothing calls this directly
no test coverage detected
searching dependent graphs…