Skip to content

Commit 0b6c77f

Browse files
committed
feat: 🎸 improve ObjValue printing
1 parent 877b81a commit 0b6c77f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/type/classes/FnType.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import {printTree} from 'tree-dump/lib/printTree';
22
import * as schema from '../../schema';
3-
import type {SchemaOf, Type} from '../types';
43
import {AbsType} from './AbsType';
5-
6-
const fnNotImplemented: schema.FunctionValue<any, any> = async () => {
7-
throw new Error('NOT_IMPLEMENTED');
8-
};
4+
import type {SchemaOf, Type} from '../types';
95

106
const toStringTree = (tab: string = '', type: FnType<Type, Type, any> | FnRxType<Type, Type, any>) => {
117
return printTree(tab, [
@@ -71,6 +67,11 @@ export class FnType<Req extends Type, Res extends Type, Ctx = unknown> extends A
7167
return this;
7268
}
7369

70+
public exec(input: schema.TypeOf<SchemaOf<Req>>) {
71+
const func = this.schema.default as schema.FunctionValue<schema.TypeOf<SchemaOf<Req>>, schema.TypeOf<SchemaOf<Res>>>;
72+
return func(input);
73+
}
74+
7475
public toString(tab: string = ''): string {
7576
return super.toString(tab) + toStringTree(tab, this);
7677
}

src/value/Value.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,33 @@ import {printTree} from 'tree-dump/lib/printTree';
22
import type {Printable} from 'tree-dump';
33
import type {ResolveType, Type} from '../type/types';
44

5+
const copyForPrint = (data: unknown): unknown => {
6+
if (typeof data === 'function') return '__fN---';
7+
if (Array.isArray(data)) return data.map(copyForPrint);
8+
if (data && typeof data === 'object') {
9+
const res: Record<string, unknown> = {};
10+
for (const k in data) res[k] = copyForPrint((data as any)[k]);
11+
return res;
12+
}
13+
return data;
14+
};
15+
516
export class Value<T extends Type = Type> implements Printable {
617
constructor(
718
public data: ResolveType<T>,
819
public type?: T,
920
) {}
1021

22+
public name(): string {
23+
return 'Value';
24+
}
25+
1126
public toString(tab: string = ''): string {
1227
const type = this.type;
13-
return 'Value' + (type ? printTree(tab, [(tab) => type.toString(tab)]) : '');
28+
return this.name() + (type ? printTree(tab, [
29+
(tab) => type.toString(tab),
30+
(tab) => (JSON.stringify(copyForPrint(this.data), null, 2) || 'und').replace(/"__fN---"/g, 'fn()').split('\n').join('\n' + tab),
31+
]) : '');
1432
}
1533
}
1634

0 commit comments

Comments
 (0)