@nmtjs/type 0.7.0 → 0.7.2
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/dist/index.js +5 -2
- package/dist/index.js.map +1 -1
- package/dist/temporal.js +7 -7
- package/dist/temporal.js.map +1 -1
- package/dist/types/array.js +5 -7
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +1 -1
- package/dist/types/base.js.map +1 -1
- package/dist/types/custom.js +3 -3
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +6 -4
- package/dist/types/date.js.map +1 -1
- package/dist/types/number.js +22 -8
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +1 -1
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +28 -10
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +43 -8
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +3 -2
- package/dist/types/union.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +15 -16
- package/src/temporal.ts +7 -7
- package/src/types/array.ts +18 -5
- package/src/types/base.ts +8 -5
- package/src/types/custom.ts +39 -28
- package/src/types/date.ts +6 -11
- package/src/types/number.ts +23 -8
- package/src/types/object.ts +3 -2
- package/src/types/string.ts +40 -11
- package/src/types/temporal.ts +52 -76
- package/src/types/union.ts +3 -8
- package/dist/utils.js +0 -0
- package/dist/utils.js.map +0 -1
- package/src/utils.ts +0 -24
package/src/types/temporal.ts
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
custom,
|
|
3
|
-
string,
|
|
4
|
-
type ZodMiniCustom,
|
|
5
|
-
type ZodMiniString,
|
|
6
|
-
} from '@zod/mini'
|
|
1
|
+
import { iso, regex, string, type ZodMiniString } from '@zod/mini'
|
|
7
2
|
import { Temporal } from 'temporal-polyfill'
|
|
8
3
|
import { CustomType, TransformType } from './custom.ts'
|
|
9
4
|
|
|
@@ -34,137 +29,118 @@ const createTemporalTransformer = <T extends Types>(
|
|
|
34
29
|
} as TemporalTransformer<T>
|
|
35
30
|
}
|
|
36
31
|
|
|
32
|
+
type EncodedType = ZodMiniString<string>
|
|
33
|
+
|
|
37
34
|
export class PlainDateType extends TransformType<
|
|
38
35
|
Temporal.PlainDate,
|
|
39
|
-
|
|
36
|
+
EncodedType
|
|
40
37
|
> {
|
|
41
38
|
static transformer = createTemporalTransformer('PlainDate')
|
|
42
39
|
|
|
43
40
|
static factory() {
|
|
44
|
-
return CustomType.factory<
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
PlainDateType.transformer.encode,
|
|
51
|
-
string(),
|
|
52
|
-
)
|
|
41
|
+
return CustomType.factory<Temporal.PlainDate, EncodedType>({
|
|
42
|
+
decode: PlainDateType.transformer.decode,
|
|
43
|
+
encode: PlainDateType.transformer.encode,
|
|
44
|
+
type: iso.date(),
|
|
45
|
+
error: 'Invalid date format',
|
|
46
|
+
})
|
|
53
47
|
}
|
|
54
48
|
}
|
|
55
49
|
|
|
56
50
|
export class PlainDateTimeType extends TransformType<
|
|
57
51
|
Temporal.PlainDateTime,
|
|
58
|
-
|
|
52
|
+
EncodedType
|
|
59
53
|
> {
|
|
60
54
|
static transformer = createTemporalTransformer('PlainDateTime')
|
|
61
55
|
|
|
62
56
|
static factory() {
|
|
63
|
-
return CustomType.factory<
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
PlainDateTimeType.transformer.encode,
|
|
70
|
-
string(),
|
|
71
|
-
)
|
|
57
|
+
return CustomType.factory<Temporal.PlainDateTime, EncodedType>({
|
|
58
|
+
decode: PlainDateTimeType.transformer.decode,
|
|
59
|
+
encode: PlainDateTimeType.transformer.encode,
|
|
60
|
+
type: iso.datetime({ local: true }),
|
|
61
|
+
error: 'Invalid datetime format',
|
|
62
|
+
})
|
|
72
63
|
}
|
|
73
64
|
}
|
|
74
65
|
|
|
75
66
|
export class ZonedDateTimeType extends TransformType<
|
|
76
67
|
Temporal.ZonedDateTime,
|
|
77
|
-
|
|
68
|
+
EncodedType
|
|
78
69
|
> {
|
|
79
70
|
static transformer = createTemporalTransformer('ZonedDateTime', (value) =>
|
|
80
71
|
Temporal.Instant.from(value).toZonedDateTimeISO('UTC'),
|
|
81
72
|
)
|
|
82
73
|
|
|
83
74
|
static factory() {
|
|
84
|
-
return CustomType.factory<
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
ZonedDateTimeType.transformer.encode,
|
|
91
|
-
string(),
|
|
92
|
-
)
|
|
75
|
+
return CustomType.factory<Temporal.ZonedDateTime, EncodedType>({
|
|
76
|
+
decode: ZonedDateTimeType.transformer.decode,
|
|
77
|
+
encode: ZonedDateTimeType.transformer.encode,
|
|
78
|
+
type: iso.datetime({ local: true }),
|
|
79
|
+
error: 'Invalid datetime format',
|
|
80
|
+
})
|
|
93
81
|
}
|
|
94
82
|
}
|
|
95
83
|
|
|
96
84
|
export class PlainTimeType extends TransformType<
|
|
97
85
|
Temporal.PlainTime,
|
|
98
|
-
|
|
86
|
+
EncodedType
|
|
99
87
|
> {
|
|
100
88
|
static transformer = createTemporalTransformer('PlainTime')
|
|
101
89
|
|
|
102
90
|
static factory() {
|
|
103
|
-
return CustomType.factory<
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
PlainTimeType.transformer.encode,
|
|
110
|
-
string(),
|
|
111
|
-
)
|
|
91
|
+
return CustomType.factory<Temporal.PlainTime, EncodedType>({
|
|
92
|
+
decode: PlainTimeType.transformer.decode,
|
|
93
|
+
encode: PlainTimeType.transformer.encode,
|
|
94
|
+
type: iso.time(),
|
|
95
|
+
error: 'Invalid time format',
|
|
96
|
+
})
|
|
112
97
|
}
|
|
113
98
|
}
|
|
114
99
|
|
|
115
100
|
export class DurationType extends TransformType<
|
|
116
101
|
Temporal.Duration,
|
|
117
|
-
|
|
102
|
+
EncodedType
|
|
118
103
|
> {
|
|
119
104
|
static transformer = createTemporalTransformer('Duration')
|
|
120
105
|
|
|
121
106
|
static factory() {
|
|
122
|
-
return CustomType.factory<
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
DurationType.transformer.encode,
|
|
129
|
-
string(),
|
|
130
|
-
)
|
|
107
|
+
return CustomType.factory<Temporal.Duration, EncodedType>({
|
|
108
|
+
decode: DurationType.transformer.decode,
|
|
109
|
+
encode: DurationType.transformer.encode,
|
|
110
|
+
type: iso.duration(),
|
|
111
|
+
error: 'Invalid duration format',
|
|
112
|
+
})
|
|
131
113
|
}
|
|
132
114
|
}
|
|
133
115
|
|
|
134
116
|
export class PlainYearMonthType extends TransformType<
|
|
135
117
|
Temporal.PlainYearMonth,
|
|
136
|
-
|
|
118
|
+
EncodedType
|
|
137
119
|
> {
|
|
138
120
|
static transformer = createTemporalTransformer('PlainYearMonth')
|
|
139
121
|
|
|
140
122
|
static factory() {
|
|
141
|
-
return CustomType.factory<
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
PlainYearMonthType.transformer.encode,
|
|
148
|
-
string(),
|
|
149
|
-
)
|
|
123
|
+
return CustomType.factory<Temporal.PlainYearMonth, EncodedType>({
|
|
124
|
+
decode: PlainYearMonthType.transformer.decode,
|
|
125
|
+
encode: PlainYearMonthType.transformer.encode,
|
|
126
|
+
type: string().check(regex(/^\d{4}-\d{2}$/)),
|
|
127
|
+
error: 'Invalid year-month format',
|
|
128
|
+
})
|
|
150
129
|
}
|
|
151
130
|
}
|
|
152
131
|
|
|
153
132
|
export class PlainMonthDayType extends TransformType<
|
|
154
133
|
Temporal.PlainMonthDay,
|
|
155
|
-
|
|
134
|
+
EncodedType
|
|
156
135
|
> {
|
|
157
136
|
static transformer = createTemporalTransformer('PlainMonthDay')
|
|
158
137
|
|
|
159
138
|
static factory() {
|
|
160
|
-
return CustomType.factory<
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
PlainMonthDayType.transformer.encode,
|
|
167
|
-
string(),
|
|
168
|
-
)
|
|
139
|
+
return CustomType.factory<Temporal.PlainMonthDay, EncodedType>({
|
|
140
|
+
decode: PlainMonthDayType.transformer.decode,
|
|
141
|
+
encode: PlainMonthDayType.transformer.encode,
|
|
142
|
+
type: string().check(regex(/^\d{2}-\d{2}$/)),
|
|
143
|
+
error: 'Invalid month-day format',
|
|
144
|
+
})
|
|
169
145
|
}
|
|
170
146
|
}
|
package/src/types/union.ts
CHANGED
|
@@ -39,15 +39,10 @@ export class IntersactionType<
|
|
|
39
39
|
static factory<
|
|
40
40
|
T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],
|
|
41
41
|
>(...options: T) {
|
|
42
|
+
const [first, second] = options
|
|
42
43
|
return new IntersactionType<T>({
|
|
43
|
-
encodedZodType: intersection(
|
|
44
|
-
|
|
45
|
-
options[1].encodedZodType,
|
|
46
|
-
),
|
|
47
|
-
decodedZodType: intersection(
|
|
48
|
-
options[0].decodedZodType,
|
|
49
|
-
options[1].decodedZodType,
|
|
50
|
-
),
|
|
44
|
+
encodedZodType: intersection(first.encodedZodType, second.encodedZodType),
|
|
45
|
+
decodedZodType: intersection(first.decodedZodType, second.decodedZodType),
|
|
51
46
|
props: { options },
|
|
52
47
|
})
|
|
53
48
|
}
|
package/dist/utils.js
DELETED
|
File without changes
|
package/dist/utils.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":"","names":[],"sources":["src/utils.ts"],"sourcesContent":["// export type UnionToIntersectionFn<T> = (\n// T extends unknown\n// ? (k: () => T) => void\n// : never\n// ) extends (k: infer Intersection) => void\n// ? Intersection\n// : never\n// export type GetUnionLast<T> = UnionToIntersectionFn<T> extends () => infer Last\n// ? Last\n// : never\n// export type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never]\n// ? Tuple\n// : UnionToTuple<Exclude<T, GetUnionLast<T>>, [GetUnionLast<T>, ...Tuple]>\n// export type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never\n\n// export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>\n\n// export type Merge<T, U> = {\n// [K in keyof T | keyof U]: K extends keyof U\n// ? U[K]\n// : K extends keyof T\n// ? T[K]\n// : never\n// }\n"],"version":3}
|
package/src/utils.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
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>>
|
|
17
|
-
|
|
18
|
-
// export type Merge<T, U> = {
|
|
19
|
-
// [K in keyof T | keyof U]: K extends keyof U
|
|
20
|
-
// ? U[K]
|
|
21
|
-
// : K extends keyof T
|
|
22
|
-
// ? T[K]
|
|
23
|
-
// : never
|
|
24
|
-
// }
|