Skip to content

Commit

Permalink
feat: preprocess type
Browse files Browse the repository at this point in the history
commit 12f3253892db42e7ce5b9c1ec3da962df0651dc1
Author: skarab42 <skarab@bluewin.ch>
Date:   Sun Mar 13 07:27:10 2022 +0100

    docs: add preprocess type

commit 4f81fbab67620358540fad689c96f1c28f87ed6d
Author: skarab42 <skarab@bluewin.ch>
Date:   Sun Mar 13 07:15:26 2022 +0100

    test: preprocess type

commit 9bc691945f60e60cf642db5fec4335ccbd6f6959
Author: skarab42 <skarab@bluewin.ch>
Date:   Sun Mar 13 07:15:09 2022 +0100

    feat: preprocess type
  • Loading branch information
skarab42 committed Mar 13, 2022
1 parent 5353c23 commit 45fdc3d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -119,6 +119,7 @@ type User = t.infer<typeof user>;
- [function(args)](#functionargs)
- [function(args, returns)](#functionargs-returns)
- [function(args, returns, implement)](#functionargs-returns-implement)
- [preprocess(filter, type)](#preprocessfilter-type)
- [Contributing 💜](#contributing-)

# API
Expand Down Expand Up @@ -494,6 +495,17 @@ const func = t.function(args, returns, (input, toInt) => {
type Func = t.infer<typeof func>; // (arg_0: string, arg_1: boolean) => string | number
```

## preprocess(filter, type)

If you want to modify the input before it is parsed you can use the `preprocess` type as follows.

```ts
const toString = t.preprocess((input) => String(input), t.string());

toString.parse("42"); // => "42"
toString.parse(42); // => "42"
```

# Contributing 💜

See [CONTRIBUTING.md](https://github.com/skarab42/tson/blob/main/CONTRIBUTING.md)
1 change: 1 addition & 0 deletions src/type/index.ts
Expand Up @@ -18,6 +18,7 @@ export { nullableType as nullable } from "./nullable";
export { numberType as number } from "./number";
export { objectType as object } from "./object";
export { optionalType as optional } from "./optional";
export { preprocessType as preprocess } from "./preprocess";
export { promiseType as promise } from "./promise";
export { recordType as record } from "./record";
export { setType as set } from "./set";
Expand Down
13 changes: 13 additions & 0 deletions src/type/preprocess.ts
@@ -0,0 +1,13 @@
import { InferType, Type } from "../types";

export function preprocessType<
TFunc extends (input: unknown) => unknown,
TType extends Type<InferType<TType>>,
>(filter: TFunc, type: TType): TType {
return {
...type,
parse(input: unknown) {
return type.parse(filter(input));
},
};
}
18 changes: 18 additions & 0 deletions test/preprocess.test.ts
@@ -0,0 +1,18 @@
import { expect, test } from "vitest";
import { t } from "../src";

test("preprocess() string", () => {
const preprocess = t.preprocess((input) => String(input), t.string());
expect(preprocess.parse("42")).toBe("42");
expect(preprocess.parse(42)).toBe("42");
});

test("preprocess() number", () => {
const preprocess = t.preprocess((input) => Number(input), t.number());
expect(preprocess.parse("42")).toBe(42);
expect(preprocess.parse(42)).toBe(42);

expect(() => preprocess.parse(undefined)).toThrow(
"expected 'number' got 'NaN'",
);
});

0 comments on commit 45fdc3d

Please sign in to comment.