@nmtjs/type 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +7 -0
- package/README.md +9 -0
- package/dist/compiler.js +57 -0
- package/dist/compiler.js.map +1 -0
- package/dist/formats.js +127 -0
- package/dist/formats.js.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/schemas/native-enum.js +14 -0
- package/dist/schemas/native-enum.js.map +1 -0
- package/dist/schemas/nullable.js +5 -0
- package/dist/schemas/nullable.js.map +1 -0
- package/dist/schemas/union-enum.js +13 -0
- package/dist/schemas/union-enum.js.map +1 -0
- package/dist/temporal.js +15 -0
- package/dist/temporal.js.map +1 -0
- package/dist/types/any.js +16 -0
- package/dist/types/any.js.map +1 -0
- package/dist/types/array.js +42 -0
- package/dist/types/array.js.map +1 -0
- package/dist/types/base.js +77 -0
- package/dist/types/base.js.map +1 -0
- package/dist/types/boolean.js +16 -0
- package/dist/types/boolean.js.map +1 -0
- package/dist/types/custom.js +23 -0
- package/dist/types/custom.js.map +1 -0
- package/dist/types/datetime.js +34 -0
- package/dist/types/datetime.js.map +1 -0
- package/dist/types/enum.js +41 -0
- package/dist/types/enum.js.map +1 -0
- package/dist/types/literal.js +21 -0
- package/dist/types/literal.js.map +1 -0
- package/dist/types/never.js +16 -0
- package/dist/types/never.js.map +1 -0
- package/dist/types/number.js +94 -0
- package/dist/types/number.js.map +1 -0
- package/dist/types/object.js +49 -0
- package/dist/types/object.js.map +1 -0
- package/dist/types/string.js +55 -0
- package/dist/types/string.js.map +1 -0
- package/dist/types/temporal.js +144 -0
- package/dist/types/temporal.js.map +1 -0
- package/dist/types/union.js +40 -0
- package/dist/types/union.js.map +1 -0
- package/dist/utils.js +1 -0
- package/dist/utils.js.map +1 -0
- package/package.json +43 -0
- package/src/compiler.ts +68 -0
- package/src/formats.ts +182 -0
- package/src/index.ts +79 -0
- package/src/schemas/native-enum.ts +29 -0
- package/src/schemas/nullable.ts +13 -0
- package/src/schemas/union-enum.ts +43 -0
- package/src/temporal.ts +34 -0
- package/src/types/any.ts +27 -0
- package/src/types/array.ts +66 -0
- package/src/types/base.ts +158 -0
- package/src/types/boolean.ts +27 -0
- package/src/types/custom.ts +36 -0
- package/src/types/datetime.ts +60 -0
- package/src/types/enum.ts +62 -0
- package/src/types/literal.ts +31 -0
- package/src/types/never.ts +30 -0
- package/src/types/number.ts +124 -0
- package/src/types/object.ts +83 -0
- package/src/types/string.ts +87 -0
- package/src/types/temporal.ts +227 -0
- package/src/types/union.ts +79 -0
- package/src/utils.ts +16 -0
- package/tsconfig.json +3 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { type TString, type TTransform, Type } from '@sinclair/typebox'
|
|
2
|
+
import { Temporal } from 'temporal-polyfill'
|
|
3
|
+
import { BaseType } from './base.ts'
|
|
4
|
+
|
|
5
|
+
export class PlainDateType<
|
|
6
|
+
N extends boolean = false,
|
|
7
|
+
O extends boolean = false,
|
|
8
|
+
> extends BaseType<TTransform<TString, Temporal.PlainDate>, N, O> {
|
|
9
|
+
constructor(nullable: N = false as N, optional: O = false as O) {
|
|
10
|
+
super(
|
|
11
|
+
Type.Transform(Type.String({ format: 'iso-date' }))
|
|
12
|
+
.Decode((value) => Temporal.PlainDate.from(value))
|
|
13
|
+
.Encode((value) => value.toString({ calendarName: 'never' })),
|
|
14
|
+
nullable,
|
|
15
|
+
optional,
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
nullable() {
|
|
20
|
+
const [_, ...args] = this._nullable()
|
|
21
|
+
return new PlainDateType(...args)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
optional() {
|
|
25
|
+
const [_, ...args] = this._optional()
|
|
26
|
+
return new PlainDateType(...args)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
nullish() {
|
|
30
|
+
const [_, ...args] = this._nullish()
|
|
31
|
+
return new PlainDateType(...args)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class PlainDateTimeType<
|
|
36
|
+
N extends boolean = false,
|
|
37
|
+
O extends boolean = false,
|
|
38
|
+
> extends BaseType<TTransform<TString, Temporal.PlainDateTime>, N, O> {
|
|
39
|
+
constructor(nullable: N = false as N, optional: O = false as O) {
|
|
40
|
+
super(
|
|
41
|
+
Type.Transform(Type.String({ format: 'iso-date-time' }))
|
|
42
|
+
.Decode((value) => Temporal.PlainDateTime.from(value))
|
|
43
|
+
.Encode((value) => value.toString({ calendarName: 'never' })),
|
|
44
|
+
nullable,
|
|
45
|
+
optional,
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
nullable() {
|
|
50
|
+
const [_, ...args] = this._nullable()
|
|
51
|
+
return new PlainDateTimeType(...args)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
optional() {
|
|
55
|
+
const [_, ...args] = this._optional()
|
|
56
|
+
return new PlainDateTimeType(...args)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
nullish() {
|
|
60
|
+
const [_, ...args] = this._nullish()
|
|
61
|
+
return new PlainDateTimeType(...args)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class ZonedDateTimeType<
|
|
66
|
+
N extends boolean = false,
|
|
67
|
+
O extends boolean = false,
|
|
68
|
+
> extends BaseType<TTransform<TString, Temporal.ZonedDateTime>, N, O> {
|
|
69
|
+
constructor(nullable: N = false as N, optional: O = false as O) {
|
|
70
|
+
super(
|
|
71
|
+
Type.Transform(Type.String({ format: 'date-time' }))
|
|
72
|
+
.Decode((value) =>
|
|
73
|
+
Temporal.Instant.from(value).toZonedDateTimeISO('UTC'),
|
|
74
|
+
)
|
|
75
|
+
.Encode((value) => value.toString({ calendarName: 'never' })),
|
|
76
|
+
nullable,
|
|
77
|
+
optional,
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
nullable() {
|
|
82
|
+
const [_, ...args] = this._nullable()
|
|
83
|
+
return new ZonedDateTimeType(...args)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
optional() {
|
|
87
|
+
const [_, ...args] = this._optional()
|
|
88
|
+
return new ZonedDateTimeType(...args)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
nullish() {
|
|
92
|
+
const [_, ...args] = this._nullish()
|
|
93
|
+
return new ZonedDateTimeType(...args)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export class PlainTimeType<
|
|
98
|
+
N extends boolean = false,
|
|
99
|
+
O extends boolean = false,
|
|
100
|
+
> extends BaseType<TTransform<TString, Temporal.PlainTime>, N, O> {
|
|
101
|
+
constructor(nullable: N = false as N, optional: O = false as O) {
|
|
102
|
+
super(
|
|
103
|
+
Type.Transform(Type.String({ format: 'time' }))
|
|
104
|
+
.Decode((value) => Temporal.PlainTime.from(value))
|
|
105
|
+
.Encode((value) => value.toString({ smallestUnit: 'microsecond' })),
|
|
106
|
+
nullable,
|
|
107
|
+
optional,
|
|
108
|
+
)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
nullable() {
|
|
112
|
+
const [_, ...args] = this._nullable()
|
|
113
|
+
return new PlainTimeType(...args)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
optional() {
|
|
117
|
+
const [_, ...args] = this._optional()
|
|
118
|
+
return new PlainTimeType(...args)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
nullish() {
|
|
122
|
+
const [_, ...args] = this._nullish()
|
|
123
|
+
return new PlainTimeType(...args)
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export class DurationType<
|
|
128
|
+
N extends boolean = false,
|
|
129
|
+
O extends boolean = false,
|
|
130
|
+
> extends BaseType<TTransform<TString, Temporal.Duration>, N, O> {
|
|
131
|
+
constructor(nullable: N = false as N, optional: O = false as O) {
|
|
132
|
+
super(
|
|
133
|
+
Type.Transform(
|
|
134
|
+
Type.String({
|
|
135
|
+
/* TODO: duration format, or regex? */
|
|
136
|
+
}),
|
|
137
|
+
)
|
|
138
|
+
.Decode((value) => Temporal.Duration.from(value))
|
|
139
|
+
.Encode((value) => value.toString({ smallestUnit: 'microsecond' })),
|
|
140
|
+
nullable,
|
|
141
|
+
optional,
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
nullable() {
|
|
146
|
+
const [_, ...args] = this._nullable()
|
|
147
|
+
return new DurationType(...args)
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
optional() {
|
|
151
|
+
const [_, ...args] = this._optional()
|
|
152
|
+
return new DurationType(...args)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
nullish() {
|
|
156
|
+
const [_, ...args] = this._nullish()
|
|
157
|
+
return new DurationType(...args)
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export class PlainYearMonthType<
|
|
162
|
+
N extends boolean = false,
|
|
163
|
+
O extends boolean = false,
|
|
164
|
+
> extends BaseType<TTransform<TString, Temporal.PlainYearMonth>, N, O> {
|
|
165
|
+
constructor(nullable: N = false as N, optional: O = false as O) {
|
|
166
|
+
super(
|
|
167
|
+
Type.Transform(
|
|
168
|
+
Type.String({
|
|
169
|
+
/* TODO: duration format, or regex? */
|
|
170
|
+
}),
|
|
171
|
+
)
|
|
172
|
+
.Decode((value) => Temporal.PlainYearMonth.from(value))
|
|
173
|
+
.Encode((value) => value.toString({ calendarName: 'never' })),
|
|
174
|
+
nullable,
|
|
175
|
+
optional,
|
|
176
|
+
)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
nullable() {
|
|
180
|
+
const [_, ...args] = this._nullable()
|
|
181
|
+
return new PlainYearMonthType(...args)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
optional() {
|
|
185
|
+
const [_, ...args] = this._optional()
|
|
186
|
+
return new PlainYearMonthType(...args)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
nullish() {
|
|
190
|
+
const [_, ...args] = this._nullish()
|
|
191
|
+
return new PlainYearMonthType(...args)
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export class PlainMonthDayType<
|
|
196
|
+
N extends boolean = false,
|
|
197
|
+
O extends boolean = false,
|
|
198
|
+
> extends BaseType<TTransform<TString, Temporal.PlainMonthDay>, N, O> {
|
|
199
|
+
constructor(nullable: N = false as N, optional: O = false as O) {
|
|
200
|
+
super(
|
|
201
|
+
Type.Transform(
|
|
202
|
+
Type.String({
|
|
203
|
+
/* TODO: duration format, or regex? */
|
|
204
|
+
}),
|
|
205
|
+
)
|
|
206
|
+
.Decode((value) => Temporal.PlainMonthDay.from(value))
|
|
207
|
+
.Encode((value) => value.toString({ calendarName: 'never' })),
|
|
208
|
+
nullable,
|
|
209
|
+
optional,
|
|
210
|
+
)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
nullable() {
|
|
214
|
+
const [_, ...args] = this._nullable()
|
|
215
|
+
return new PlainMonthDayType(...args)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
optional() {
|
|
219
|
+
const [_, ...args] = this._optional()
|
|
220
|
+
return new PlainMonthDayType(...args)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
nullish() {
|
|
224
|
+
const [_, ...args] = this._nullish()
|
|
225
|
+
return new PlainMonthDayType(...args)
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { type TIntersect, type TUnion, Type } from '@sinclair/typebox'
|
|
2
|
+
import type { UnionToTuple } from '../utils.ts'
|
|
3
|
+
import { BaseType, typeFinalSchema } from './base.ts'
|
|
4
|
+
|
|
5
|
+
export class UnionType<
|
|
6
|
+
T extends [BaseType, BaseType, ...BaseType[]] = [
|
|
7
|
+
BaseType,
|
|
8
|
+
BaseType,
|
|
9
|
+
...BaseType[],
|
|
10
|
+
],
|
|
11
|
+
N extends boolean = false,
|
|
12
|
+
O extends boolean = false,
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
> extends BaseType<TUnion<UnionToTuple<T[number][typeFinal]>>, N, O> {
|
|
15
|
+
constructor(
|
|
16
|
+
readonly types: T,
|
|
17
|
+
nullable: N = false as N,
|
|
18
|
+
optional: O = false as O,
|
|
19
|
+
) {
|
|
20
|
+
super(
|
|
21
|
+
Type.Union(types.map((t) => t[typeFinalSchema])) as any,
|
|
22
|
+
nullable,
|
|
23
|
+
optional,
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
nullable() {
|
|
28
|
+
const [_, ...args] = this._nullable()
|
|
29
|
+
return new UnionType(this.types, ...args)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
optional() {
|
|
33
|
+
const [_, ...args] = this._optional()
|
|
34
|
+
return new UnionType(this.types, ...args)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
nullish() {
|
|
38
|
+
const [_, ...args] = this._nullish()
|
|
39
|
+
return new UnionType(this.types, ...args)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export class IntersactionType<
|
|
44
|
+
T extends [BaseType, BaseType, ...BaseType[]] = [
|
|
45
|
+
BaseType,
|
|
46
|
+
BaseType,
|
|
47
|
+
...BaseType[],
|
|
48
|
+
],
|
|
49
|
+
N extends boolean = false,
|
|
50
|
+
O extends boolean = false,
|
|
51
|
+
// @ts-expect-error
|
|
52
|
+
> extends BaseType<TIntersect<UnionToTuple<T[number][typeFinal]>>, N, O> {
|
|
53
|
+
constructor(
|
|
54
|
+
readonly types: T,
|
|
55
|
+
nullable: N = false as N,
|
|
56
|
+
optional: O = false as O,
|
|
57
|
+
) {
|
|
58
|
+
super(
|
|
59
|
+
Type.Intersect(types.map((t) => t[typeFinalSchema])) as any,
|
|
60
|
+
nullable,
|
|
61
|
+
optional,
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
nullable() {
|
|
66
|
+
const [_, ...args] = this._nullable()
|
|
67
|
+
return new IntersactionType(this.types, ...args)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
optional() {
|
|
71
|
+
const [_, ...args] = this._optional()
|
|
72
|
+
return new IntersactionType(this.types, ...args)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
nullish() {
|
|
76
|
+
const [_, ...args] = this._nullish()
|
|
77
|
+
return new IntersactionType(this.types, ...args)
|
|
78
|
+
}
|
|
79
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type UnionToIntersectionFn<T> = (
|
|
2
|
+
T extends unknown
|
|
3
|
+
? (k: () => T) => void
|
|
4
|
+
: never
|
|
5
|
+
) extends (k: infer Intersection) => void
|
|
6
|
+
? Intersection
|
|
7
|
+
: never
|
|
8
|
+
export type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last
|
|
9
|
+
? Last
|
|
10
|
+
: never
|
|
11
|
+
export type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never]
|
|
12
|
+
? Tuple
|
|
13
|
+
: UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>
|
|
14
|
+
export type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never
|
|
15
|
+
|
|
16
|
+
export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>
|
package/tsconfig.json
ADDED