Skip to content

Commit ed7ac6e

Browse files
committed
[dsch] drafts
1 parent 46b930f commit ed7ac6e

3 files changed

Lines changed: 34 additions & 68 deletions

File tree

.eslintrc.json

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/deserialize.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Caster, OkType } from './types.js';
2+
3+
type CasterFactory<Args extends any[], R> = (...args: Args) => Caster<R>;
4+
5+
type WrappedFactory<F extends CasterFactory<any, any>> = (
6+
...args: Parameters<F>
7+
) => Caster<OkType<ReturnType<F>>>;
8+
9+
export interface Deserializer<P> extends Caster<P> {
10+
to<T>(caster: Caster<T>): Caster<T>;
11+
to<F extends CasterFactory<any, any>>(factory: F): WrappedFactory<F>;
12+
}
13+
14+
export const deserialize = <P>(parser: Caster<P>): Deserializer<P> => {
15+
const result = parser as any;
16+
17+
result.to = (target: any) => {
18+
if (typeof target === 'function' && 'chain' in target) {
19+
const caster = target as Caster<any>;
20+
const name = `${parser.name} |> ${caster.name}`;
21+
return parser.chain(caster, name);
22+
}
23+
24+
return (...args: any[]) => {
25+
const caster = target(...args);
26+
const name = `${parser.name} |> ${caster.name}`;
27+
return parser.chain(caster, name);
28+
};
29+
};
30+
31+
return result as Deserializer<P>;
32+
};

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ export * from './text.js';
1313
export * from './types.js';
1414
export * from './casting-error.js';
1515
export * from './date.js';
16+
export * from './deserialize.js';
17+

0 commit comments

Comments
 (0)