arktype 2.0.0-dev.1 → 2.0.0-dev.10

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.
Files changed (219) hide show
  1. package/__tests__/array.test.ts +270 -151
  2. package/__tests__/badDefinitionType.test.ts +10 -3
  3. package/__tests__/basis.test.ts +6 -2
  4. package/__tests__/bounds.test.ts +282 -352
  5. package/__tests__/cast.test.ts +7 -2
  6. package/__tests__/completions.test.ts +8 -3
  7. package/__tests__/config.test.ts +95 -167
  8. package/__tests__/dateLiteral.test.ts +8 -4
  9. package/__tests__/declared.test.ts +7 -6
  10. package/__tests__/discrimination.test.ts +1 -1
  11. package/__tests__/divisor.test.ts +46 -22
  12. package/__tests__/enclosed.test.ts +39 -25
  13. package/__tests__/examples.test.ts +56 -56
  14. package/__tests__/expressions.test.ts +184 -112
  15. package/__tests__/generic.test.ts +307 -288
  16. package/__tests__/group.test.ts +67 -54
  17. package/__tests__/imports.test.ts +62 -49
  18. package/__tests__/instanceof.test.ts +25 -23
  19. package/__tests__/intersection.test.ts +52 -48
  20. package/__tests__/keyTraversal.test.ts +77 -73
  21. package/__tests__/keyof.test.ts +68 -52
  22. package/__tests__/keywords.test.ts +164 -138
  23. package/__tests__/literal.test.ts +60 -21
  24. package/__tests__/match.bench.ts +31 -0
  25. package/__tests__/match.test.ts +222 -22
  26. package/__tests__/narrow.test.ts +52 -54
  27. package/__tests__/objectLiteral.test.ts +347 -205
  28. package/__tests__/operator.bench.ts +4 -4
  29. package/__tests__/pipe.test.ts +501 -0
  30. package/__tests__/realWorld.test.ts +63 -0
  31. package/__tests__/regex.test.ts +17 -7
  32. package/__tests__/scope.bench.ts +1 -1
  33. package/__tests__/scope.test.ts +257 -23
  34. package/__tests__/serialization.test.ts +19 -0
  35. package/__tests__/string.test.ts +11 -7
  36. package/__tests__/submodule.test.ts +26 -39
  37. package/__tests__/this.test.ts +83 -57
  38. package/__tests__/thunk.test.ts +13 -7
  39. package/__tests__/traverse.test.ts +76 -54
  40. package/__tests__/tsGenerics.test.ts +12 -12
  41. package/__tests__/type.test.ts +36 -0
  42. package/__tests__/typeReference.test.ts +7 -7
  43. package/__tests__/unenclosed.test.ts +116 -100
  44. package/__tests__/union.test.ts +171 -104
  45. package/ark.ts +21 -65
  46. package/generic.ts +69 -0
  47. package/main.ts +6 -18
  48. package/match.ts +195 -93
  49. package/module.ts +15 -0
  50. package/out/ark.d.ts +15 -39
  51. package/out/ark.js +9 -18
  52. package/out/ark.js.map +1 -1
  53. package/out/generic.d.ts +21 -0
  54. package/out/generic.js +24 -0
  55. package/out/generic.js.map +1 -0
  56. package/out/main.d.ts +6 -7
  57. package/out/main.js +4 -3
  58. package/out/main.js.map +1 -1
  59. package/out/match.d.ts +57 -34
  60. package/out/match.js +51 -17
  61. package/out/match.js.map +1 -1
  62. package/out/module.d.ts +10 -0
  63. package/out/module.js +3 -0
  64. package/out/module.js.map +1 -0
  65. package/out/parser/definition.d.ts +11 -13
  66. package/out/parser/definition.js +10 -13
  67. package/out/parser/definition.js.map +1 -1
  68. package/out/parser/generic.d.ts +5 -7
  69. package/out/parser/generic.js +7 -11
  70. package/out/parser/generic.js.map +1 -1
  71. package/out/parser/objectLiteral.d.ts +45 -14
  72. package/out/parser/objectLiteral.js +61 -52
  73. package/out/parser/objectLiteral.js.map +1 -1
  74. package/out/parser/semantic/bounds.d.ts +10 -8
  75. package/out/parser/semantic/bounds.js +0 -1
  76. package/out/parser/semantic/bounds.js.map +1 -1
  77. package/out/parser/semantic/divisor.d.ts +5 -7
  78. package/out/parser/semantic/divisor.js +0 -1
  79. package/out/parser/semantic/infer.d.ts +24 -0
  80. package/out/parser/semantic/infer.js +1 -0
  81. package/out/parser/semantic/infer.js.map +1 -0
  82. package/out/parser/semantic/utils.d.ts +3 -7
  83. package/out/parser/semantic/utils.js +0 -1
  84. package/out/parser/semantic/validate.d.ts +15 -14
  85. package/out/parser/semantic/validate.js +1 -1
  86. package/out/parser/semantic/validate.js.map +1 -1
  87. package/out/parser/string/reduce/dynamic.d.ts +13 -13
  88. package/out/parser/string/reduce/dynamic.js +31 -26
  89. package/out/parser/string/reduce/dynamic.js.map +1 -1
  90. package/out/parser/string/reduce/shared.d.ts +6 -20
  91. package/out/parser/string/reduce/shared.js +0 -1
  92. package/out/parser/string/reduce/shared.js.map +1 -1
  93. package/out/parser/string/reduce/static.d.ts +27 -24
  94. package/out/parser/string/reduce/static.js +0 -1
  95. package/out/parser/string/shift/operand/date.d.ts +3 -3
  96. package/out/parser/string/shift/operand/date.js +4 -7
  97. package/out/parser/string/shift/operand/date.js.map +1 -1
  98. package/out/parser/string/shift/operand/enclosed.d.ts +4 -19
  99. package/out/parser/string/shift/operand/enclosed.js +12 -13
  100. package/out/parser/string/shift/operand/enclosed.js.map +1 -1
  101. package/out/parser/string/shift/operand/genericArgs.d.ts +9 -10
  102. package/out/parser/string/shift/operand/genericArgs.js +6 -10
  103. package/out/parser/string/shift/operand/genericArgs.js.map +1 -1
  104. package/out/parser/string/shift/operand/operand.d.ts +3 -4
  105. package/out/parser/string/shift/operand/operand.js +8 -12
  106. package/out/parser/string/shift/operand/operand.js.map +1 -1
  107. package/out/parser/string/shift/operand/unenclosed.d.ts +15 -22
  108. package/out/parser/string/shift/operand/unenclosed.js +24 -36
  109. package/out/parser/string/shift/operand/unenclosed.js.map +1 -1
  110. package/out/parser/string/shift/operator/bounds.d.ts +9 -13
  111. package/out/parser/string/shift/operator/bounds.js +43 -54
  112. package/out/parser/string/shift/operator/bounds.js.map +1 -1
  113. package/out/parser/string/shift/operator/divisor.d.ts +2 -4
  114. package/out/parser/string/shift/operator/divisor.js +1 -3
  115. package/out/parser/string/shift/operator/divisor.js.map +1 -1
  116. package/out/parser/string/shift/operator/operator.d.ts +2 -3
  117. package/out/parser/string/shift/operator/operator.js +12 -19
  118. package/out/parser/string/shift/operator/operator.js.map +1 -1
  119. package/out/parser/string/shift/scanner.d.ts +2 -2
  120. package/out/parser/string/shift/scanner.js +14 -16
  121. package/out/parser/string/shift/scanner.js.map +1 -1
  122. package/out/parser/string/string.d.ts +8 -6
  123. package/out/parser/string/string.js +1 -3
  124. package/out/parser/string/string.js.map +1 -1
  125. package/out/parser/tuple.d.ts +102 -43
  126. package/out/parser/tuple.js +119 -87
  127. package/out/parser/tuple.js.map +1 -1
  128. package/out/scope.d.ts +43 -120
  129. package/out/scope.js +39 -209
  130. package/out/scope.js.map +1 -1
  131. package/out/type.d.ts +43 -64
  132. package/out/type.js +30 -131
  133. package/out/type.js.map +1 -1
  134. package/package.json +7 -3
  135. package/parser/definition.ts +77 -93
  136. package/parser/generic.ts +38 -40
  137. package/parser/objectLiteral.ts +172 -136
  138. package/parser/semantic/bounds.ts +33 -32
  139. package/parser/semantic/divisor.ts +8 -10
  140. package/parser/semantic/infer.ts +141 -0
  141. package/parser/semantic/utils.ts +12 -23
  142. package/parser/semantic/validate.ts +81 -73
  143. package/parser/string/reduce/dynamic.ts +67 -55
  144. package/parser/string/reduce/shared.ts +1 -3
  145. package/parser/string/reduce/static.ts +67 -69
  146. package/parser/string/shift/operand/date.ts +15 -14
  147. package/parser/string/shift/operand/enclosed.ts +30 -25
  148. package/parser/string/shift/operand/genericArgs.ts +46 -45
  149. package/parser/string/shift/operand/operand.ts +32 -38
  150. package/parser/string/shift/operand/unenclosed.ts +107 -158
  151. package/parser/string/shift/operator/bounds.ts +118 -112
  152. package/parser/string/shift/operator/divisor.ts +14 -17
  153. package/parser/string/shift/operator/operator.ts +35 -46
  154. package/parser/string/shift/scanner.ts +51 -50
  155. package/parser/string/string.ts +30 -35
  156. package/parser/tuple.ts +375 -307
  157. package/scope.ts +170 -453
  158. package/type.ts +193 -274
  159. package/__tests__/astUtils.test.ts +0 -17
  160. package/__tests__/cyclic.test.ts +0 -144
  161. package/__tests__/jsdoc.test.ts +0 -64
  162. package/__tests__/morph.test.ts +0 -409
  163. package/__tests__/nodeDefinitions.test.ts +0 -190
  164. package/__tests__/typeMethods.test.ts +0 -69
  165. package/out/ark.d.ts.map +0 -1
  166. package/out/arkConfig.d.ts +0 -2
  167. package/out/arkConfig.d.ts.map +0 -1
  168. package/out/arkConfig.js +0 -10
  169. package/out/arkConfig.js.map +0 -1
  170. package/out/main.d.ts.map +0 -1
  171. package/out/match.d.ts.map +0 -1
  172. package/out/parser/definition.d.ts.map +0 -1
  173. package/out/parser/generic.d.ts.map +0 -1
  174. package/out/parser/objectLiteral.d.ts.map +0 -1
  175. package/out/parser/semantic/bounds.d.ts.map +0 -1
  176. package/out/parser/semantic/divisor.d.ts.map +0 -1
  177. package/out/parser/semantic/intersections.d.ts +0 -14
  178. package/out/parser/semantic/intersections.d.ts.map +0 -1
  179. package/out/parser/semantic/intersections.js +0 -2
  180. package/out/parser/semantic/intersections.js.map +0 -1
  181. package/out/parser/semantic/semantic.d.ts +0 -33
  182. package/out/parser/semantic/semantic.d.ts.map +0 -1
  183. package/out/parser/semantic/semantic.js +0 -2
  184. package/out/parser/semantic/semantic.js.map +0 -1
  185. package/out/parser/semantic/utils.d.ts.map +0 -1
  186. package/out/parser/semantic/validate.d.ts.map +0 -1
  187. package/out/parser/shared.d.ts +0 -54
  188. package/out/parser/shared.d.ts.map +0 -1
  189. package/out/parser/shared.js +0 -47
  190. package/out/parser/shared.js.map +0 -1
  191. package/out/parser/string/reduce/dynamic.d.ts.map +0 -1
  192. package/out/parser/string/reduce/shared.d.ts.map +0 -1
  193. package/out/parser/string/reduce/static.d.ts.map +0 -1
  194. package/out/parser/string/shift/operand/date.d.ts.map +0 -1
  195. package/out/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  196. package/out/parser/string/shift/operand/genericArgs.d.ts.map +0 -1
  197. package/out/parser/string/shift/operand/operand.d.ts.map +0 -1
  198. package/out/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  199. package/out/parser/string/shift/operator/bounds.d.ts.map +0 -1
  200. package/out/parser/string/shift/operator/divisor.d.ts.map +0 -1
  201. package/out/parser/string/shift/operator/operator.d.ts.map +0 -1
  202. package/out/parser/string/shift/scanner.d.ts.map +0 -1
  203. package/out/parser/string/string.d.ts.map +0 -1
  204. package/out/parser/tuple.d.ts.map +0 -1
  205. package/out/scope.d.ts.map +0 -1
  206. package/out/test.d.ts +0 -2
  207. package/out/test.d.ts.map +0 -1
  208. package/out/test.js +0 -7
  209. package/out/test.js.map +0 -1
  210. package/out/tsconfig.build.tsbuildinfo +0 -1
  211. package/out/type.d.ts.map +0 -1
  212. package/out/util.d.ts +0 -19
  213. package/out/util.d.ts.map +0 -1
  214. package/out/util.js +0 -7
  215. package/out/util.js.map +0 -1
  216. package/parser/semantic/intersections.ts +0 -46
  217. package/parser/semantic/semantic.ts +0 -144
  218. package/parser/shared.ts +0 -152
  219. package/util.ts +0 -36
@@ -1,9 +1,9 @@
1
- import type { Completion, defined, ErrorMessage } from "@arktype/util"
1
+ import type { LimitLiteral } from "@arktype/schema"
2
+ import type { Completion, ErrorMessage, defined } from "@arktype/util"
2
3
  import type { Scanner } from "../shift/scanner.js"
3
4
  import type {
4
5
  Comparator,
5
6
  InvertedComparators,
6
- LimitLiteral,
7
7
  MaxComparator,
8
8
  MinComparator,
9
9
  OpenLeftBound,
@@ -24,15 +24,15 @@ export type StaticState = {
24
24
  unscanned: string
25
25
  }
26
26
 
27
- export type AutocompletePrefix = `${StringifiablePrefixOperator} `
28
-
29
27
  type BranchState = {
30
28
  prefixes: StringifiablePrefixOperator[]
31
29
  leftBound: OpenLeftBound | undefined
32
- "&": unknown
33
- "|": unknown
30
+ intersection: unknown
31
+ union: unknown
34
32
  }
35
33
 
34
+ export type AutocompletePrefix = `${StringifiablePrefixOperator} `
35
+
36
36
  export namespace state {
37
37
  export type initialize<def extends string> = from<{
38
38
  root: undefined
@@ -64,17 +64,18 @@ export namespace state {
64
64
  type initialBranches = branchesFrom<{
65
65
  prefixes: []
66
66
  leftBound: undefined
67
- "&": undefined
68
- "|": undefined
67
+ intersection: undefined
68
+ union: undefined
69
69
  }>
70
70
 
71
71
  type updateScanned<
72
72
  previousScanned extends string,
73
73
  previousUnscanned extends string,
74
74
  updatedUnscanned extends string
75
- > = previousUnscanned extends `${infer justScanned}${updatedUnscanned}`
76
- ? `${previousScanned}${justScanned}`
77
- : previousScanned
75
+ > =
76
+ previousUnscanned extends `${infer justScanned}${updatedUnscanned}` ?
77
+ `${previousScanned}${justScanned}`
78
+ : previousScanned
78
79
 
79
80
  export type setRoot<
80
81
  s extends StaticState,
@@ -98,8 +99,8 @@ export namespace state {
98
99
  branches: {
99
100
  prefixes: [...s["branches"]["prefixes"], prefix]
100
101
  leftBound: s["branches"]["leftBound"]
101
- "&": s["branches"]["&"]
102
- "|": s["branches"]["|"]
102
+ intersection: s["branches"]["intersection"]
103
+ union: s["branches"]["union"]
103
104
  }
104
105
  groups: s["groups"]
105
106
  finalizer: s["finalizer"]
@@ -111,38 +112,40 @@ export namespace state {
111
112
  s extends StaticState,
112
113
  token extends "|" | "&",
113
114
  unscanned extends string
114
- > = s["branches"]["leftBound"] extends {}
115
- ? openRangeError<s["branches"]["leftBound"]>
116
- : from<{
115
+ > =
116
+ s["branches"]["leftBound"] extends {} ?
117
+ openRangeError<s["branches"]["leftBound"]>
118
+ : from<{
117
119
  root: undefined
118
120
  branches: {
119
121
  prefixes: []
120
122
  leftBound: undefined
121
- "&": token extends "&" ? mergeToIntersection<s> : undefined
122
- "|": token extends "|" ? mergeToUnion<s> : s["branches"]["|"]
123
+ intersection: token extends "&" ? mergeToIntersection<s> : undefined
124
+ union: token extends "|" ? mergeToUnion<s> : s["branches"]["union"]
123
125
  }
124
126
  groups: s["groups"]
125
127
  finalizer: s["finalizer"]
126
128
  scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
127
129
  unscanned: unscanned
128
- }>
130
+ }>
129
131
 
130
132
  export type reduceLeftBound<
131
133
  s extends StaticState,
132
134
  limit extends LimitLiteral,
133
135
  comparator extends Comparator,
134
136
  unscanned extends string
135
- > = comparator extends "<" | "<="
136
- ? s["branches"]["leftBound"] extends {}
137
- ? state.error<
137
+ > =
138
+ comparator extends "<" | "<=" ?
139
+ s["branches"]["leftBound"] extends {} ?
140
+ state.error<
138
141
  writeMultipleLeftBoundsMessage<
139
142
  s["branches"]["leftBound"]["limit"],
140
143
  s["branches"]["leftBound"]["comparator"],
141
144
  limit,
142
145
  InvertedComparators[comparator]
143
146
  >
144
- >
145
- : from<{
147
+ >
148
+ : from<{
146
149
  root: undefined
147
150
  branches: {
148
151
  prefixes: s["branches"]["prefixes"]
@@ -150,15 +153,15 @@ export namespace state {
150
153
  limit: limit
151
154
  comparator: InvertedComparators[comparator]
152
155
  }
153
- "&": s["branches"]["&"]
154
- "|": s["branches"]["|"]
156
+ intersection: s["branches"]["intersection"]
157
+ union: s["branches"]["union"]
155
158
  }
156
159
  groups: s["groups"]
157
160
  finalizer: s["finalizer"]
158
161
  scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
159
162
  unscanned: unscanned
160
- }>
161
- : state.error<writeUnpairableComparatorMessage<comparator>>
163
+ }>
164
+ : state.error<writeUnpairableComparatorMessage<comparator>>
162
165
 
163
166
  export type reduceRange<
164
167
  s extends StaticState,
@@ -172,8 +175,8 @@ export namespace state {
172
175
  branches: {
173
176
  prefixes: s["branches"]["prefixes"]
174
177
  leftBound: undefined
175
- "&": s["branches"]["&"]
176
- "|": s["branches"]["|"]
178
+ intersection: s["branches"]["intersection"]
179
+ union: s["branches"]["union"]
177
180
  }
178
181
  groups: s["groups"]
179
182
  finalizer: s["finalizer"]
@@ -184,15 +187,15 @@ export namespace state {
184
187
  export type reduceSingleBound<
185
188
  s extends StaticState,
186
189
  comparator extends Comparator,
187
- limit extends LimitLiteral,
190
+ limit extends number | string,
188
191
  unscanned extends string
189
192
  > = state.from<{
190
193
  root: [s["root"], comparator, limit]
191
194
  branches: {
192
195
  prefixes: s["branches"]["prefixes"]
193
196
  leftBound: undefined
194
- "&": s["branches"]["&"]
195
- "|": s["branches"]["|"]
197
+ intersection: s["branches"]["intersection"]
198
+ union: s["branches"]["union"]
196
199
  }
197
200
  groups: s["groups"]
198
201
  finalizer: s["finalizer"]
@@ -201,42 +204,39 @@ export namespace state {
201
204
  }>
202
205
 
203
206
  type mergeToUnion<s extends StaticState> =
204
- s["branches"]["|"] extends undefined
205
- ? mergeToIntersection<s>
206
- : [s["branches"]["|"], "|", mergeToIntersection<s>]
207
+ s["branches"]["union"] extends undefined ? mergeToIntersection<s>
208
+ : [s["branches"]["union"], "|", mergeToIntersection<s>]
207
209
 
208
210
  type mergeToIntersection<s extends StaticState> =
209
- s["branches"]["&"] extends undefined
210
- ? mergePrefixes<s>
211
- : [s["branches"]["&"], "&", mergePrefixes<s>]
211
+ s["branches"]["intersection"] extends undefined ? mergePrefixes<s>
212
+ : [s["branches"]["intersection"], "&", mergePrefixes<s>]
212
213
 
213
214
  type mergePrefixes<
214
215
  s extends StaticState,
215
216
  remaining extends unknown[] = s["branches"]["prefixes"]
216
- > = remaining extends [infer head, ...infer tail]
217
- ? [head, mergePrefixes<s, tail>]
218
- : s["root"]
217
+ > =
218
+ remaining extends [infer head, ...infer tail] ?
219
+ [head, mergePrefixes<s, tail>]
220
+ : s["root"]
219
221
 
220
222
  type popGroup<stack extends BranchState[], top extends BranchState> = [
221
223
  ...stack,
222
224
  top
223
225
  ]
224
226
 
225
- export type finalizeGroup<
226
- s extends StaticState,
227
- unscanned extends string
228
- > = s["branches"]["leftBound"] extends {}
229
- ? openRangeError<s["branches"]["leftBound"]>
230
- : s["groups"] extends popGroup<infer stack, infer top>
231
- ? from<{
227
+ export type finalizeGroup<s extends StaticState, unscanned extends string> =
228
+ s["branches"]["leftBound"] extends {} ?
229
+ openRangeError<s["branches"]["leftBound"]>
230
+ : s["groups"] extends popGroup<infer stack, infer top> ?
231
+ from<{
232
232
  groups: stack
233
233
  branches: top
234
234
  root: mergeToUnion<s>
235
235
  finalizer: s["finalizer"]
236
236
  scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
237
237
  unscanned: unscanned
238
- }>
239
- : state.error<writeUnmatchedGroupCloseMessage<unscanned>>
238
+ }>
239
+ : state.error<writeUnmatchedGroupCloseMessage<unscanned>>
240
240
 
241
241
  export type reduceGroupOpen<
242
242
  s extends StaticState,
@@ -253,35 +253,33 @@ export namespace state {
253
253
  export type finalize<
254
254
  s extends StaticState,
255
255
  finalizer extends Scanner.FinalizingLookahead
256
- > = s["groups"] extends []
257
- ? s["branches"]["leftBound"] extends {}
258
- ? openRangeError<s["branches"]["leftBound"]>
259
- : from<{
256
+ > =
257
+ s["groups"] extends [] ?
258
+ s["branches"]["leftBound"] extends {} ?
259
+ openRangeError<s["branches"]["leftBound"]>
260
+ : from<{
260
261
  root: mergeToUnion<s>
261
262
  groups: s["groups"]
262
263
  branches: initialBranches
263
264
  finalizer: finalizer
264
265
  scanned: s["scanned"]
265
266
  unscanned: s["unscanned"]
266
- }>
267
- : state.error<writeUnclosedGroupMessage<")">>
267
+ }>
268
+ : state.error<writeUnclosedGroupMessage<")">>
268
269
 
269
270
  type openRangeError<range extends defined<BranchState["leftBound"]>> =
270
271
  state.error<writeOpenRangeMessage<range["limit"], range["comparator"]>>
271
272
 
272
273
  export type previousOperator<s extends StaticState> =
273
- s["branches"]["leftBound"] extends {}
274
- ? s["branches"]["leftBound"]["comparator"]
275
- : s["branches"]["prefixes"] extends [
276
- ...unknown[],
277
- infer tail extends string
278
- ]
279
- ? tail
280
- : s["branches"]["&"] extends {}
281
- ? "&"
282
- : s["branches"]["|"] extends {}
283
- ? "|"
284
- : undefined
274
+ s["branches"]["leftBound"] extends {} ?
275
+ s["branches"]["leftBound"]["comparator"]
276
+ : s["branches"]["prefixes"] extends (
277
+ [...unknown[], infer tail extends string]
278
+ ) ?
279
+ tail
280
+ : s["branches"]["intersection"] extends {} ? "&"
281
+ : s["branches"]["union"] extends {} ? "|"
282
+ : undefined
285
283
 
286
284
  export type scanTo<s extends StaticState, unscanned extends string> = from<{
287
285
  root: s["root"]
@@ -7,11 +7,11 @@ export const isDateLiteral = (value: unknown): value is DateLiteral =>
7
7
  (value[1] === "'" || value[1] === '"') &&
8
8
  value.at(-1) === value[1]
9
9
 
10
- export const isValidDate = (d: Date) => d.toString() !== "Invalid Date"
10
+ export const isValidDate = (d: Date): boolean => d.toString() !== "Invalid Date"
11
11
 
12
12
  export const extractDateLiteralSource = <literal extends DateLiteral>(
13
13
  literal: literal
14
- ) => literal.slice(2, -1) as extractDateLiteralSource<literal>
14
+ ): extractDateLiteralSource<literal> => literal.slice(2, -1) as never
15
15
 
16
16
  type extractDateLiteralSource<literal extends DateLiteral> =
17
17
  literal extends DateLiteral<infer source> ? source : never
@@ -26,29 +26,30 @@ export type writeInvalidDateMessage<source extends string> =
26
26
 
27
27
  export type DateInput = ConstructorParameters<typeof Date>[0]
28
28
 
29
+ export type DateParseResult<
30
+ errorOnFail extends boolean | string = boolean | string
31
+ > = Date | (errorOnFail extends true | string ? never : undefined)
32
+
29
33
  export const tryParseDate = <errorOnFail extends boolean | string>(
30
34
  source: string,
31
35
  errorOnFail?: errorOnFail
32
- ) => maybeParseDate(source, errorOnFail)
36
+ ): DateParseResult<errorOnFail> => maybeParseDate(source, errorOnFail)
33
37
 
34
38
  const maybeParseDate = <errorOnFail extends boolean | string>(
35
39
  source: string,
36
40
  errorOnFail?: errorOnFail
37
- ): Date | (errorOnFail extends true | string ? never : undefined) => {
41
+ ): DateParseResult<errorOnFail> => {
38
42
  const stringParsedDate = new Date(source)
39
- if (isValidDate(stringParsedDate)) {
40
- return stringParsedDate
41
- }
43
+ if (isValidDate(stringParsedDate)) return stringParsedDate
44
+
42
45
  const epochMillis = tryParseNumber(source)
43
46
  if (epochMillis !== undefined) {
44
47
  const numberParsedDate = new Date(epochMillis)
45
- if (isValidDate(numberParsedDate)) {
46
- return numberParsedDate
47
- }
48
+ if (isValidDate(numberParsedDate)) return numberParsedDate
48
49
  }
49
- return errorOnFail
50
- ? throwParseError(
50
+ return errorOnFail ?
51
+ throwParseError(
51
52
  errorOnFail === true ? writeInvalidDateMessage(source) : errorOnFail
52
- )
53
- : (undefined as never)
53
+ )
54
+ : (undefined as never)
54
55
  }
@@ -1,4 +1,3 @@
1
- import { schema, type RegexLiteral } from "@arktype/schema"
2
1
  import { isKeyOf } from "@arktype/util"
3
2
  import type { DynamicState } from "../../reduce/dynamic.js"
4
3
  import type { StaticState, state } from "../../reduce/static.js"
@@ -18,24 +17,31 @@ export type SingleQuotedStringLiteral<Text extends string = string> =
18
17
  export const parseEnclosed = (
19
18
  s: DynamicState,
20
19
  enclosing: EnclosingStartToken
21
- ) => {
20
+ ): void => {
22
21
  const enclosed = s.scanner.shiftUntil(
23
22
  untilLookaheadIsClosing[enclosingTokens[enclosing]]
24
23
  )
25
- if (s.scanner.lookahead === "") {
24
+ if (s.scanner.lookahead === "")
26
25
  return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing))
27
- }
26
+
28
27
  // Shift the scanner one additional time for the second enclosing token
29
- const token = `${enclosing}${enclosed}${s.scanner.shift()}`
28
+ s.scanner.shift()
30
29
  if (enclosing === "/") {
31
30
  // fail parsing if the regex is invalid
32
31
  new RegExp(enclosed)
33
- s.root = schema({ basis: "string", pattern: token as RegexLiteral })
34
- } else if (isKeyOf(enclosing, enclosingQuote)) {
35
- s.root = schema({ unit: enclosed })
36
- } else {
32
+ s.root = s.ctx.$.node(
33
+ "intersection",
34
+ {
35
+ domain: "string",
36
+ regex: enclosed
37
+ },
38
+ { prereduced: true }
39
+ )
40
+ } else if (isKeyOf(enclosing, enclosingQuote))
41
+ s.root = s.ctx.$.node("unit", { unit: enclosed })
42
+ else {
37
43
  const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed))
38
- s.root = schema({ unit: date, description: token })
44
+ s.root = s.ctx.$.node("unit", { unit: date, description: enclosed })
39
45
  }
40
46
  }
41
47
 
@@ -43,20 +49,19 @@ export type parseEnclosed<
43
49
  s extends StaticState,
44
50
  enclosingStart extends EnclosingStartToken,
45
51
  unscanned extends string
46
- > = Scanner.shiftUntil<
47
- unscanned,
48
- EnclosingTokens[enclosingStart]
49
- > extends Scanner.shiftResult<infer scanned, infer nextUnscanned>
50
- ? nextUnscanned extends ""
51
- ? state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>>
52
- : state.setRoot<
52
+ > =
53
+ Scanner.shiftUntil<unscanned, EnclosingTokens[enclosingStart]> extends (
54
+ Scanner.shiftResult<infer scanned, infer nextUnscanned>
55
+ ) ?
56
+ nextUnscanned extends "" ?
57
+ state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>>
58
+ : state.setRoot<
53
59
  s,
54
60
  `${enclosingStart}${scanned}${EnclosingTokens[enclosingStart]}`,
55
- nextUnscanned extends Scanner.shift<string, infer unscanned>
56
- ? unscanned
57
- : ""
58
- >
59
- : never
61
+ nextUnscanned extends Scanner.shift<string, infer unscanned> ? unscanned
62
+ : ""
63
+ >
64
+ : never
60
65
 
61
66
  export const enclosingQuote = {
62
67
  "'": 1,
@@ -88,9 +93,9 @@ export const untilLookaheadIsClosing: Record<
88
93
  EnclosingEndToken,
89
94
  Scanner.UntilCondition
90
95
  > = {
91
- "'": (scanner) => scanner.lookahead === `'`,
92
- '"': (scanner) => scanner.lookahead === `"`,
93
- "/": (scanner) => scanner.lookahead === `/`
96
+ "'": scanner => scanner.lookahead === `'`,
97
+ '"': scanner => scanner.lookahead === `"`,
98
+ "/": scanner => scanner.lookahead === `/`
94
99
  }
95
100
 
96
101
  const enclosingCharDescriptions = {
@@ -1,4 +1,4 @@
1
- import type { TypeNode } from "@arktype/schema"
1
+ import type { RawSchema } from "@arktype/schema"
2
2
  import type { ErrorMessage, join } from "@arktype/util"
3
3
  import type { DynamicState } from "../../reduce/dynamic.js"
4
4
  import { writeUnclosedGroupMessage } from "../../reduce/shared.js"
@@ -17,7 +17,7 @@ export const parseGenericArgs = (
17
17
  name: string,
18
18
  params: string[],
19
19
  s: DynamicState
20
- ) => parseGenericArgsRecurse(name, params, s, [], [])
20
+ ): ParsedArgs<RawSchema[]> => _parseGenericArgs(name, params, s, [], [])
21
21
 
22
22
  export type parseGenericArgs<
23
23
  name extends string,
@@ -25,15 +25,15 @@ export type parseGenericArgs<
25
25
  unscanned extends string,
26
26
  $,
27
27
  args
28
- > = parseGenericArgsRecurse<name, params, unscanned, $, args, [], []>
28
+ > = _parseGenericArgs<name, params, unscanned, $, args, [], []>
29
29
 
30
- const parseGenericArgsRecurse = (
30
+ const _parseGenericArgs = (
31
31
  name: string,
32
32
  params: string[],
33
33
  s: DynamicState,
34
34
  argDefs: string[],
35
- argNodes: TypeNode[]
36
- ): ParsedArgs<TypeNode[]> => {
35
+ argNodes: RawSchema[]
36
+ ): ParsedArgs<RawSchema[]> => {
37
37
  const argState = s.parseUntilFinalizer()
38
38
  // remove the finalizing token from the argDef
39
39
  argDefs.push(argState.scanner.scanned.slice(0, -1))
@@ -44,18 +44,16 @@ const parseGenericArgsRecurse = (
44
44
  result: argNodes,
45
45
  unscanned: argState.scanner.unscanned
46
46
  }
47
- } else {
48
- return argState.error(
49
- writeInvalidGenericArgsMessage(name, params, argDefs)
50
- )
51
47
  }
52
- } else if (argState.finalizer === ",") {
53
- return parseGenericArgsRecurse(name, params, s, argDefs, argNodes)
48
+ return argState.error(writeInvalidGenericArgsMessage(name, params, argDefs))
54
49
  }
50
+ if (argState.finalizer === ",")
51
+ return _parseGenericArgs(name, params, s, argDefs, argNodes)
52
+
55
53
  return argState.error(writeUnclosedGroupMessage(">"))
56
54
  }
57
55
 
58
- type parseGenericArgsRecurse<
56
+ type _parseGenericArgs<
59
57
  name extends string,
60
58
  params extends string[],
61
59
  unscanned extends string,
@@ -63,31 +61,31 @@ type parseGenericArgsRecurse<
63
61
  args,
64
62
  argDefs extends string[],
65
63
  argAsts extends unknown[]
66
- > = parseUntilFinalizer<
67
- state.initialize<unscanned>,
68
- $,
69
- args
70
- > extends infer finalArgState extends StaticState
71
- ? {
64
+ > =
65
+ parseUntilFinalizer<state.initialize<unscanned>, $, args> extends (
66
+ infer finalArgState extends StaticState
67
+ ) ?
68
+ {
72
69
  defs: [
73
70
  ...argDefs,
74
- finalArgState["scanned"] extends `${infer def}${"," | ">"}`
75
- ? def
76
- : finalArgState["scanned"]
71
+ finalArgState["scanned"] extends `${infer def}${"," | ">"}` ? def
72
+ : finalArgState["scanned"]
77
73
  ]
78
74
  asts: [...argAsts, finalArgState["root"]]
79
75
  unscanned: finalArgState["unscanned"]
80
- } extends {
81
- defs: infer nextDefs extends string[]
82
- asts: infer nextAsts extends unknown[]
83
- unscanned: infer nextUnscanned extends string
84
- }
85
- ? finalArgState["finalizer"] extends ">"
86
- ? nextAsts["length"] extends params["length"]
87
- ? ParsedArgs<nextAsts, nextUnscanned>
88
- : state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
89
- : finalArgState["finalizer"] extends ","
90
- ? parseGenericArgsRecurse<
76
+ } extends (
77
+ {
78
+ defs: infer nextDefs extends string[]
79
+ asts: infer nextAsts extends unknown[]
80
+ unscanned: infer nextUnscanned extends string
81
+ }
82
+ ) ?
83
+ finalArgState["finalizer"] extends ">" ?
84
+ nextAsts["length"] extends params["length"] ?
85
+ ParsedArgs<nextAsts, nextUnscanned>
86
+ : state.error<writeInvalidGenericArgsMessage<name, params, nextDefs>>
87
+ : finalArgState["finalizer"] extends "," ?
88
+ _parseGenericArgs<
91
89
  name,
92
90
  params,
93
91
  nextUnscanned,
@@ -95,12 +93,11 @@ type parseGenericArgsRecurse<
95
93
  args,
96
94
  nextDefs,
97
95
  nextAsts
98
- >
99
- : finalArgState["finalizer"] extends ErrorMessage
100
- ? finalArgState
96
+ >
97
+ : finalArgState["finalizer"] extends ErrorMessage ? finalArgState
101
98
  : state.error<writeUnclosedGroupMessage<">">>
102
- : never
103
- : never
99
+ : never
100
+ : never
104
101
 
105
102
  export const writeInvalidGenericArgsMessage = <
106
103
  name extends string,
@@ -110,10 +107,12 @@ export const writeInvalidGenericArgsMessage = <
110
107
  name: name,
111
108
  params: params,
112
109
  argDefs: argDefs
113
- ) =>
114
- `${name}<${params.join(", ")}> requires exactly ${params.length} args (got ${
115
- argDefs.length
116
- }${argDefs.length === 0 ? "" : ": " + argDefs.join(", ")})`
110
+ ): writeInvalidGenericArgsMessage<name, params, argDefs> =>
111
+ `${name}<${params.join(", ")}> requires exactly ${
112
+ params.length
113
+ } args (got ${argDefs.length}${
114
+ argDefs.length === 0 ? "" : `: ${argDefs.join(", ")}`
115
+ })` as never
117
116
 
118
117
  export type writeInvalidGenericArgsMessage<
119
118
  name extends string,
@@ -122,6 +121,8 @@ export type writeInvalidGenericArgsMessage<
122
121
  > = `${name}<${join<
123
122
  params,
124
123
  ", "
125
- >}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends 0
126
- ? ""
127
- : `: ${join<argDefs, ",">}`})`
124
+ >}> requires exactly ${params["length"]} args (got ${argDefs["length"]}${argDefs["length"] extends (
125
+ 0
126
+ ) ?
127
+ ""
128
+ : `: ${join<argDefs, ",">}`})`
@@ -1,48 +1,42 @@
1
1
  import type { DynamicState } from "../../reduce/dynamic.js"
2
- import type { state, StaticState } from "../../reduce/static.js"
2
+ import type { StaticState, state } from "../../reduce/static.js"
3
3
  import type { BaseCompletions } from "../../string.js"
4
4
  import { Scanner } from "../scanner.js"
5
5
  import {
6
- enclosingChar,
7
- parseEnclosed,
8
- type EnclosingEndToken,
9
6
  type EnclosingQuote,
10
- type EnclosingStartToken
7
+ type EnclosingStartToken,
8
+ enclosingChar,
9
+ parseEnclosed
11
10
  } from "./enclosed.js"
12
11
  import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js"
13
12
 
14
13
  export const parseOperand = (s: DynamicState): void =>
15
- s.scanner.lookahead === ""
16
- ? s.error(writeMissingOperandMessage(s))
17
- : s.scanner.lookahead === "("
18
- ? s.shiftedByOne().reduceGroupOpen()
19
- : s.scanner.lookaheadIsIn(enclosingChar)
20
- ? parseEnclosed(s, s.scanner.shift())
21
- : s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens)
22
- ? parseOperand(s.shiftedByOne())
23
- : s.scanner.lookahead === "d"
24
- ? s.shiftedByOne().scanner.lookaheadIsIn(enclosingChar)
25
- ? parseEnclosed(s, `d${s.scanner.shift()}` as EnclosingStartToken)
26
- : parseUnenclosed(s)
27
- : parseUnenclosed(s)
14
+ s.scanner.lookahead === "" ? s.error(writeMissingOperandMessage(s))
15
+ : s.scanner.lookahead === "(" ? s.shiftedByOne().reduceGroupOpen()
16
+ : s.scanner.lookaheadIsIn(enclosingChar) ? parseEnclosed(s, s.scanner.shift())
17
+ : s.scanner.lookaheadIsIn(Scanner.whiteSpaceTokens) ?
18
+ parseOperand(s.shiftedByOne())
19
+ : s.scanner.lookahead === "d" ?
20
+ s.shiftedByOne().scanner.lookaheadIsIn(enclosingChar) ?
21
+ parseEnclosed(s, `d${s.scanner.shift()}` as EnclosingStartToken)
22
+ : parseUnenclosed(s)
23
+ : parseUnenclosed(s)
28
24
 
29
- export type parseOperand<
30
- s extends StaticState,
31
- $,
32
- args
33
- > = s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned>
34
- ? lookahead extends "("
35
- ? state.reduceGroupOpen<s, unscanned>
36
- : lookahead extends EnclosingEndToken
37
- ? parseEnclosed<s, lookahead, unscanned>
38
- : lookahead extends Scanner.WhiteSpaceToken
39
- ? parseOperand<state.scanTo<s, unscanned>, $, args>
40
- : lookahead extends "d"
41
- ? unscanned extends Scanner.shift<
42
- infer enclosing extends EnclosingQuote,
43
- infer nextUnscanned
44
- >
45
- ? parseEnclosed<s, `d${enclosing}`, nextUnscanned>
46
- : parseUnenclosed<s, $, args>
47
- : parseUnenclosed<s, $, args>
48
- : state.completion<`${s["scanned"]}${BaseCompletions<$, args>}`>
25
+ export type parseOperand<s extends StaticState, $, args> =
26
+ s["unscanned"] extends Scanner.shift<infer lookahead, infer unscanned> ?
27
+ lookahead extends "(" ? state.reduceGroupOpen<s, unscanned>
28
+ : lookahead extends EnclosingStartToken ?
29
+ parseEnclosed<s, lookahead, unscanned>
30
+ : lookahead extends Scanner.WhiteSpaceToken ?
31
+ parseOperand<state.scanTo<s, unscanned>, $, args>
32
+ : lookahead extends "d" ?
33
+ unscanned extends (
34
+ Scanner.shift<
35
+ infer enclosing extends EnclosingQuote,
36
+ infer nextUnscanned
37
+ >
38
+ ) ?
39
+ parseEnclosed<s, `d${enclosing}`, nextUnscanned>
40
+ : parseUnenclosed<s, $, args>
41
+ : parseUnenclosed<s, $, args>
42
+ : state.completion<`${s["scanned"]}${BaseCompletions<$, args>}`>