@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.
@@ -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
- ZodMiniString
36
+ EncodedType
40
37
  > {
41
38
  static transformer = createTemporalTransformer('PlainDate')
42
39
 
43
40
  static factory() {
44
- return CustomType.factory<
45
- Temporal.PlainDate,
46
- ZodMiniString,
47
- ZodMiniCustom<Temporal.PlainDate, Temporal.PlainDate>
48
- >(
49
- PlainDateType.transformer.decode,
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
- ZodMiniString
52
+ EncodedType
59
53
  > {
60
54
  static transformer = createTemporalTransformer('PlainDateTime')
61
55
 
62
56
  static factory() {
63
- return CustomType.factory<
64
- Temporal.PlainDateTime,
65
- ZodMiniString,
66
- ZodMiniCustom<Temporal.PlainDateTime, Temporal.PlainDateTime>
67
- >(
68
- PlainDateTimeType.transformer.decode,
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
- ZodMiniString
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
- Temporal.ZonedDateTime,
86
- ZodMiniString,
87
- ZodMiniCustom<Temporal.ZonedDateTime, Temporal.ZonedDateTime>
88
- >(
89
- ZonedDateTimeType.transformer.decode,
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
- ZodMiniString
86
+ EncodedType
99
87
  > {
100
88
  static transformer = createTemporalTransformer('PlainTime')
101
89
 
102
90
  static factory() {
103
- return CustomType.factory<
104
- Temporal.PlainTime,
105
- ZodMiniString,
106
- ZodMiniCustom<Temporal.PlainTime, Temporal.PlainTime>
107
- >(
108
- PlainTimeType.transformer.decode,
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
- ZodMiniString
102
+ EncodedType
118
103
  > {
119
104
  static transformer = createTemporalTransformer('Duration')
120
105
 
121
106
  static factory() {
122
- return CustomType.factory<
123
- Temporal.Duration,
124
- ZodMiniString,
125
- ZodMiniCustom<Temporal.Duration, Temporal.Duration>
126
- >(
127
- DurationType.transformer.decode,
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
- ZodMiniString
118
+ EncodedType
137
119
  > {
138
120
  static transformer = createTemporalTransformer('PlainYearMonth')
139
121
 
140
122
  static factory() {
141
- return CustomType.factory<
142
- Temporal.PlainYearMonth,
143
- ZodMiniString,
144
- ZodMiniCustom<Temporal.PlainYearMonth, Temporal.PlainYearMonth>
145
- >(
146
- PlainYearMonthType.transformer.decode,
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
- ZodMiniString
134
+ EncodedType
156
135
  > {
157
136
  static transformer = createTemporalTransformer('PlainMonthDay')
158
137
 
159
138
  static factory() {
160
- return CustomType.factory<
161
- Temporal.PlainMonthDay,
162
- ZodMiniString,
163
- ZodMiniCustom<Temporal.PlainMonthDay, Temporal.PlainMonthDay>
164
- >(
165
- PlainMonthDayType.transformer.decode,
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
  }
@@ -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
- options[0].encodedZodType,
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
- // }