-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjType.ts
More file actions
168 lines (145 loc) · 5.09 KB
/
ObjType.ts
File metadata and controls
168 lines (145 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {printTree} from 'tree-dump/lib/printTree';
import * as schema from '../../schema';
import type {ExcludeFromTuple, PickFromTuple} from '../../util/types';
import type {SchemaOf, SchemaOfObjectFields, Type} from '../types';
import {AbsType} from './AbsType';
export class KeyType<K extends string, V extends Type> extends AbsType<schema.KeySchema<K, SchemaOf<V>>> {
public readonly optional: boolean = false;
constructor(
public readonly key: K,
public readonly val: V,
) {
super(schema.s.Key(key, schema.s.any) as any);
}
public getSchema(): schema.KeySchema<K, SchemaOf<V>> {
return {
...this.schema,
value: this.val.getSchema() as any,
};
}
public getOptions(): schema.Optional<schema.KeySchema<K, SchemaOf<V>>> {
const {kind, key, value, optional, ...options} = this.schema;
return options as any;
}
protected toStringTitle(): string {
return JSON.stringify(this.key);
}
public toString(tab: string = ''): string {
return super.toString(tab) + printTree(tab + ' ', [(tab) => this.val.toString(tab)]);
}
}
export class KeyOptType<K extends string, V extends Type> extends AbsType<schema.OptKeySchema<K, SchemaOf<V>>> {
public readonly optional: boolean = true;
constructor(
public readonly key: K,
public readonly val: V,
) {
super(schema.s.KeyOpt(key, schema.s.any) as any);
}
public getSchema(): schema.OptKeySchema<K, SchemaOf<V>> {
return {
...this.schema,
value: this.val.getSchema() as any,
};
}
public getOptions(): schema.Optional<schema.KeySchema<K, SchemaOf<V>>> {
const {kind, key, value, optional, ...options} = this.schema;
return options as any;
}
protected toStringTitle(): string {
return JSON.stringify(this.key) + '?';
}
public toString(tab: string = ''): string {
return super.toString(tab) + printTree(tab + ' ', [(tab) => this.val.toString(tab)]);
}
}
export class ObjType<
F extends (KeyType<any, any> | KeyOptType<any, any>)[] = (KeyType<any, any> | KeyOptType<any, any>)[],
> extends AbsType<schema.ObjSchema<SchemaOfObjectFields<F>>> {
constructor(public readonly keys: F) {
super(schema.s.obj as any);
}
private _key(
field: KeyType<any, any> | KeyOptType<any, any>,
options?: schema.Optional<schema.KeySchema<any, any>>,
): void {
if (options) field.options(options);
field.system = this.system;
this.keys.push(field as any);
}
/**
* Adds a property to the object type.
* @param key The key of the property.
* @param value The value type of the property.
* @param options Optional schema options for the property.
* @returns A new object type with the added property.
*/
public prop<K extends string, V extends Type>(
key: K,
value: V,
options?: schema.Optional<schema.KeySchema<K, SchemaOf<V>>>,
): ObjType<[...F, KeyType<K, V>]> {
this._key(new KeyType<K, V>(key, value), options);
return <any>this;
}
/**
* Adds an optional property to the object type.
* @param key The key of the property.
* @param value The value type of the property.
* @param options Optional schema options for the property.
* @returns A new object type with the added property.
*/
public opt<K extends string, V extends Type>(
key: K,
value: V,
options?: schema.Optional<schema.KeySchema<K, SchemaOf<V>>>,
): ObjType<[...F, KeyOptType<K, V>]> {
this._key(new KeyOptType<K, V>(key, value), options);
return <any>this;
}
public getSchema(): schema.ObjSchema<SchemaOfObjectFields<F>> {
return {
...this.schema,
keys: this.keys.map((f) => f.getSchema()) as any,
};
}
public getOptions(): schema.Optional<schema.ObjSchema<SchemaOfObjectFields<F>>> {
const {kind, keys: fields, ...options} = this.schema;
return options as any;
}
public getField<K extends keyof schema.TypeOf<schema.ObjSchema<SchemaOfObjectFields<F>>>>(
key: K,
): KeyType<string, Type> | KeyOptType<string, Type> | undefined {
return this.keys.find((f) => f.key === key);
}
public extend<F2 extends KeyType<any, any>[]>(o: ObjType<F2>): ObjType<[...F, ...F2]> {
const type = new ObjType([...this.keys, ...o.keys]) as ObjType<[...F, ...F2]>;
type.system = this.system;
return type;
}
public omit<K extends keyof schema.TypeOf<schema.ObjSchema<SchemaOfObjectFields<F>>>>(
key: K,
): ObjType<ExcludeFromTuple<F, KeyType<K extends string ? K : never, any>>> {
const type = new ObjType(this.keys.filter((f) => f.key !== key) as any);
type.system = this.system;
return type;
}
public pick<K extends keyof schema.TypeOf<schema.ObjSchema<SchemaOfObjectFields<F>>>>(
key: K,
): ObjType<PickFromTuple<F, KeyType<K extends string ? K : never, any>>> {
const field = this.keys.find((f) => f.key === key);
if (!field) throw new Error('FIELD_NOT_FOUND');
const type = new ObjType([field] as any);
type.system = this.system;
return type;
}
public toString(tab: string = ''): string {
return (
super.toString(tab) +
printTree(
tab,
this.keys.map((field) => (tab) => field.toString(tab)),
)
);
}
}