diff options
Diffstat (limited to 'packages/taler-wallet-core/src/util')
-rw-r--r-- | packages/taler-wallet-core/src/util/amounts.ts | 12 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/util/codec-test.ts | 24 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/util/codec.ts | 76 |
3 files changed, 58 insertions, 54 deletions
diff --git a/packages/taler-wallet-core/src/util/amounts.ts b/packages/taler-wallet-core/src/util/amounts.ts index 33faa868e..2a8c47905 100644 --- a/packages/taler-wallet-core/src/util/amounts.ts +++ b/packages/taler-wallet-core/src/util/amounts.ts @@ -22,7 +22,7 @@ * Imports. */ import { - makeCodecForObject, + buildCodecForObject, codecForString, codecForNumber, Codec, @@ -68,13 +68,13 @@ export interface AmountJson { } export const codecForAmountJson = (): Codec<AmountJson> => - makeCodecForObject<AmountJson>() - .property("currency", codecForString) - .property("value", codecForNumber) - .property("fraction", codecForNumber) + buildCodecForObject<AmountJson>() + .property("currency", codecForString()) + .property("value", codecForNumber()) + .property("fraction", codecForNumber()) .build("AmountJson"); -export const codecForAmountString = (): Codec<AmountString> => codecForString; +export const codecForAmountString = (): Codec<AmountString> => codecForString(); /** * Result of a possibly overflowing operation. diff --git a/packages/taler-wallet-core/src/util/codec-test.ts b/packages/taler-wallet-core/src/util/codec-test.ts index b429c318c..f8f4c797c 100644 --- a/packages/taler-wallet-core/src/util/codec-test.ts +++ b/packages/taler-wallet-core/src/util/codec-test.ts @@ -21,10 +21,10 @@ import test from "ava"; import { Codec, - makeCodecForObject, - makeCodecForConstString, + buildCodecForObject, + codecForConstString, codecForString, - makeCodecForUnion, + buildCodecForUnion, } from "./codec"; interface MyObj { @@ -44,8 +44,8 @@ interface AltTwo { type MyUnion = AltOne | AltTwo; test("basic codec", (t) => { - const myObjCodec = makeCodecForObject<MyObj>() - .property("foo", codecForString) + const myObjCodec = buildCodecForObject<MyObj>() + .property("foo", codecForString()) .build("MyObj"); const res = myObjCodec.decode({ foo: "hello" }); t.assert(res.foo === "hello"); @@ -56,15 +56,15 @@ test("basic codec", (t) => { }); test("union", (t) => { - const altOneCodec: Codec<AltOne> = makeCodecForObject<AltOne>() - .property("type", makeCodecForConstString("one")) - .property("foo", codecForString) + const altOneCodec: Codec<AltOne> = buildCodecForObject<AltOne>() + .property("type", codecForConstString("one")) + .property("foo", codecForString()) .build("AltOne"); - const altTwoCodec: Codec<AltTwo> = makeCodecForObject<AltTwo>() - .property("type", makeCodecForConstString("two")) - .property("bar", codecForString) + const altTwoCodec: Codec<AltTwo> = buildCodecForObject<AltTwo>() + .property("type", codecForConstString("two")) + .property("bar", codecForString()) .build("AltTwo"); - const myUnionCodec: Codec<MyUnion> = makeCodecForUnion<MyUnion>() + const myUnionCodec: Codec<MyUnion> = buildCodecForUnion<MyUnion>() .discriminateOn("type") .alternative("one", altOneCodec) .alternative("two", altTwoCodec) diff --git a/packages/taler-wallet-core/src/util/codec.ts b/packages/taler-wallet-core/src/util/codec.ts index 2ce3c2cba..111abc38c 100644 --- a/packages/taler-wallet-core/src/util/codec.ts +++ b/packages/taler-wallet-core/src/util/codec.ts @@ -221,18 +221,18 @@ export class UnionCodecPreBuilder<T> { /** * Return a builder for a codec that decodes an object with properties. */ -export function makeCodecForObject<T>(): ObjectCodecBuilder<T, {}> { +export function buildCodecForObject<T>(): ObjectCodecBuilder<T, {}> { return new ObjectCodecBuilder<T, {}>(); } -export function makeCodecForUnion<T>(): UnionCodecPreBuilder<T> { +export function buildCodecForUnion<T>(): UnionCodecPreBuilder<T> { return new UnionCodecPreBuilder<T>(); } /** * Return a codec for a mapping from a string to values described by the inner codec. */ -export function makeCodecForMap<T>( +export function codecForMap<T>( innerCodec: Codec<T>, ): Codec<{ [x: string]: T }> { if (!innerCodec) { @@ -255,7 +255,7 @@ export function makeCodecForMap<T>( /** * Return a codec for a list, containing values described by the inner codec. */ -export function makeCodecForList<T>(innerCodec: Codec<T>): Codec<T[]> { +export function codecForList<T>(innerCodec: Codec<T>): Codec<T[]> { if (!innerCodec) { throw Error("inner codec must be defined"); } @@ -276,16 +276,18 @@ export function makeCodecForList<T>(innerCodec: Codec<T>): Codec<T[]> { /** * Return a codec for a value that must be a number. */ -export const codecForNumber: Codec<number> = { - decode(x: any, c?: Context): number { - if (typeof x === "number") { - return x; - } - throw new DecodingError( - `expected number at ${renderContext(c)} but got ${typeof x}`, - ); - }, -}; +export function codecForNumber(): Codec<number> { + return { + decode(x: any, c?: Context): number { + if (typeof x === "number") { + return x; + } + throw new DecodingError( + `expected number at ${renderContext(c)} but got ${typeof x}`, + ); + }, + }; +} /** * Return a codec for a value that must be a number. @@ -304,30 +306,34 @@ export const codecForBoolean: Codec<boolean> = { /** * Return a codec for a value that must be a string. */ -export const codecForString: Codec<string> = { - decode(x: any, c?: Context): string { - if (typeof x === "string") { - return x; - } - throw new DecodingError( - `expected string at ${renderContext(c)} but got ${typeof x}`, - ); - }, -}; +export function codecForString(): Codec<string> { + return { + decode(x: any, c?: Context): string { + if (typeof x === "string") { + return x; + } + throw new DecodingError( + `expected string at ${renderContext(c)} but got ${typeof x}`, + ); + }, + }; +} /** * Codec that allows any value. */ -export const codecForAny: Codec<any> = { - decode(x: any, c?: Context): any { - return x; - }, -}; +export function codecForAny(): Codec<any> { + return { + decode(x: any, c?: Context): any { + return x; + }, + }; +} /** * Return a codec for a value that must be a string. */ -export function makeCodecForConstString<V extends string>(s: V): Codec<V> { +export function codecForConstString<V extends string>(s: V): Codec<V> { return { decode(x: any, c?: Context): V { if (x === s) { @@ -345,7 +351,7 @@ export function makeCodecForConstString<V extends string>(s: V): Codec<V> { /** * Return a codec for a boolean true constant. */ -export function makeCodecForConstTrue(): Codec<true> { +export function codecForConstTrue(): Codec<true> { return { decode(x: any, c?: Context): true { if (x === true) { @@ -361,7 +367,7 @@ export function makeCodecForConstTrue(): Codec<true> { /** * Return a codec for a boolean true constant. */ -export function makeCodecForConstFalse(): Codec<false> { +export function codecForConstFalse(): Codec<false> { return { decode(x: any, c?: Context): false { if (x === false) { @@ -377,7 +383,7 @@ export function makeCodecForConstFalse(): Codec<false> { /** * Return a codec for a value that must be a constant number. */ -export function makeCodecForConstNumber<V extends number>(n: V): Codec<V> { +export function codecForConstNumber<V extends number>(n: V): Codec<V> { return { decode(x: any, c?: Context): V { if (x === n) { @@ -392,9 +398,7 @@ export function makeCodecForConstNumber<V extends number>(n: V): Codec<V> { }; } -export function makeCodecOptional<V>( - innerCodec: Codec<V>, -): Codec<V | undefined> { +export function codecOptional<V>(innerCodec: Codec<V>): Codec<V | undefined> { return { decode(x: any, c?: Context): V | undefined { if (x === undefined || x === null) { |