diff options
Diffstat (limited to 'packages/taler-util/src/codec.ts')
-rw-r--r-- | packages/taler-util/src/codec.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/packages/taler-util/src/codec.ts b/packages/taler-util/src/codec.ts index 8605ff335..2ea64a249 100644 --- a/packages/taler-util/src/codec.ts +++ b/packages/taler-util/src/codec.ts @@ -417,3 +417,26 @@ export function codecOptional<V>(innerCodec: Codec<V>): Codec<V | undefined> { }, }; } + +export type CodecType<T> = T extends Codec<infer X> ? X : any; + +export function codecForEither<T extends Array<Codec<unknown>>>( + ...alts: [...T] +): Codec<CodecType<T[number]>> { + return { + decode(x: any, c?: Context): any { + for (const alt of alts) { + try { + return alt.decode(x, c); + } catch (e) { + continue; + } + } + throw new DecodingError( + `No alternative matched at at ${renderContext(c)}`, + ); + }, + }; +} + +const x = codecForEither(codecForString(), codecForNumber()); |