arktype 1.1.0-dev-2 → 2.0.0-beta.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.
Files changed (225) hide show
  1. package/LICENSE +7 -0
  2. package/out/ark.d.ts +24 -0
  3. package/out/ark.js +8 -0
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +54 -0
  7. package/out/generic.js +39 -0
  8. package/out/index.d.ts +6 -0
  9. package/out/index.js +5 -0
  10. package/out/match.d.ts +71 -0
  11. package/out/match.js +50 -0
  12. package/out/module.d.ts +11 -0
  13. package/out/module.js +2 -0
  14. package/{dist → out}/parser/definition.d.ts +16 -16
  15. package/{dist → out}/parser/definition.js +12 -12
  16. package/out/parser/objectLiteral.d.ts +79 -0
  17. package/out/parser/objectLiteral.js +100 -0
  18. package/out/parser/semantic/bounds.d.ts +15 -0
  19. package/{dist → out}/parser/semantic/bounds.js +0 -1
  20. package/out/parser/semantic/default.d.ts +8 -0
  21. package/out/parser/semantic/default.js +1 -0
  22. package/out/parser/semantic/divisor.d.ts +7 -0
  23. package/out/parser/semantic/divisor.js +1 -0
  24. package/out/parser/semantic/infer.d.ts +25 -0
  25. package/out/parser/semantic/infer.js +1 -0
  26. package/out/parser/semantic/utils.d.ts +4 -0
  27. package/out/parser/semantic/utils.js +1 -0
  28. package/out/parser/semantic/validate.d.ts +30 -0
  29. package/out/parser/semantic/validate.js +2 -0
  30. package/out/parser/string/reduce/dynamic.d.ts +43 -0
  31. package/{dist → out}/parser/string/reduce/dynamic.js +50 -33
  32. package/out/parser/string/reduce/shared.d.ts +42 -0
  33. package/{dist → out}/parser/string/reduce/shared.js +22 -2
  34. package/{dist → out}/parser/string/reduce/static.d.ts +29 -27
  35. package/out/parser/string/reduce/static.js +1 -0
  36. package/out/parser/string/shift/operand/date.d.ts +9 -0
  37. package/{dist → out}/parser/string/shift/operand/date.js +5 -8
  38. package/{dist → out}/parser/string/shift/operand/enclosed.d.ts +5 -16
  39. package/{dist → out}/parser/string/shift/operand/enclosed.js +15 -15
  40. package/out/parser/string/shift/operand/genericArgs.d.ts +28 -0
  41. package/out/parser/string/shift/operand/genericArgs.js +16 -0
  42. package/out/parser/string/shift/operand/operand.d.ts +9 -0
  43. package/out/parser/string/shift/operand/operand.js +12 -0
  44. package/out/parser/string/shift/operand/unenclosed.d.ts +30 -0
  45. package/out/parser/string/shift/operand/unenclosed.js +53 -0
  46. package/out/parser/string/shift/operator/bounds.d.ts +27 -0
  47. package/out/parser/string/shift/operator/bounds.js +94 -0
  48. package/out/parser/string/shift/operator/default.d.ts +24 -0
  49. package/out/parser/string/shift/operator/default.js +23 -0
  50. package/{dist → out}/parser/string/shift/operator/divisor.d.ts +2 -4
  51. package/{dist → out}/parser/string/shift/operator/divisor.js +2 -4
  52. package/{dist → out}/parser/string/shift/operator/operator.d.ts +4 -4
  53. package/out/parser/string/shift/operator/operator.js +22 -0
  54. package/{dist → out}/parser/string/shift/scanner.d.ts +13 -17
  55. package/{dist → out}/parser/string/shift/scanner.js +27 -24
  56. package/out/parser/string/string.d.ts +28 -0
  57. package/out/parser/string/string.js +24 -0
  58. package/out/parser/tuple.d.ts +130 -0
  59. package/out/parser/tuple.js +157 -0
  60. package/out/scope.d.ts +84 -0
  61. package/out/scope.js +131 -0
  62. package/out/type.d.ts +79 -0
  63. package/out/type.js +37 -0
  64. package/package.json +37 -41
  65. package/dist/main.d.ts +0 -11
  66. package/dist/main.d.ts.map +0 -1
  67. package/dist/main.js +0 -8
  68. package/dist/main.js.map +0 -1
  69. package/dist/match.d.ts +0 -46
  70. package/dist/match.d.ts.map +0 -1
  71. package/dist/match.js +0 -23
  72. package/dist/match.js.map +0 -1
  73. package/dist/parser/definition.d.ts.map +0 -1
  74. package/dist/parser/definition.js.map +0 -1
  75. package/dist/parser/generic.d.ts +0 -44
  76. package/dist/parser/generic.d.ts.map +0 -1
  77. package/dist/parser/generic.js +0 -46
  78. package/dist/parser/generic.js.map +0 -1
  79. package/dist/parser/objectLiteral.d.ts +0 -24
  80. package/dist/parser/objectLiteral.d.ts.map +0 -1
  81. package/dist/parser/objectLiteral.js +0 -38
  82. package/dist/parser/objectLiteral.js.map +0 -1
  83. package/dist/parser/semantic/bounds.d.ts +0 -13
  84. package/dist/parser/semantic/bounds.d.ts.map +0 -1
  85. package/dist/parser/semantic/bounds.js.map +0 -1
  86. package/dist/parser/semantic/divisor.d.ts +0 -9
  87. package/dist/parser/semantic/divisor.d.ts.map +0 -1
  88. package/dist/parser/semantic/divisor.js +0 -2
  89. package/dist/parser/semantic/divisor.js.map +0 -1
  90. package/dist/parser/semantic/intersections.d.ts +0 -15
  91. package/dist/parser/semantic/intersections.d.ts.map +0 -1
  92. package/dist/parser/semantic/intersections.js +0 -2
  93. package/dist/parser/semantic/intersections.js.map +0 -1
  94. package/dist/parser/semantic/semantic.d.ts +0 -22
  95. package/dist/parser/semantic/semantic.d.ts.map +0 -1
  96. package/dist/parser/semantic/semantic.js +0 -2
  97. package/dist/parser/semantic/semantic.js.map +0 -1
  98. package/dist/parser/semantic/utils.d.ts +0 -8
  99. package/dist/parser/semantic/utils.d.ts.map +0 -1
  100. package/dist/parser/semantic/utils.js +0 -2
  101. package/dist/parser/semantic/utils.js.map +0 -1
  102. package/dist/parser/semantic/validate.d.ts +0 -24
  103. package/dist/parser/semantic/validate.d.ts.map +0 -1
  104. package/dist/parser/semantic/validate.js +0 -2
  105. package/dist/parser/semantic/validate.js.map +0 -1
  106. package/dist/parser/shared.d.ts +0 -48
  107. package/dist/parser/shared.d.ts.map +0 -1
  108. package/dist/parser/shared.js +0 -41
  109. package/dist/parser/shared.js.map +0 -1
  110. package/dist/parser/string/reduce/dynamic.d.ts +0 -40
  111. package/dist/parser/string/reduce/dynamic.d.ts.map +0 -1
  112. package/dist/parser/string/reduce/dynamic.js.map +0 -1
  113. package/dist/parser/string/reduce/shared.d.ts +0 -29
  114. package/dist/parser/string/reduce/shared.d.ts.map +0 -1
  115. package/dist/parser/string/reduce/shared.js.map +0 -1
  116. package/dist/parser/string/reduce/static.d.ts.map +0 -1
  117. package/dist/parser/string/reduce/static.js +0 -2
  118. package/dist/parser/string/reduce/static.js.map +0 -1
  119. package/dist/parser/string/shift/operand/date.d.ts +0 -11
  120. package/dist/parser/string/shift/operand/date.d.ts.map +0 -1
  121. package/dist/parser/string/shift/operand/date.js.map +0 -1
  122. package/dist/parser/string/shift/operand/enclosed.d.ts.map +0 -1
  123. package/dist/parser/string/shift/operand/enclosed.js.map +0 -1
  124. package/dist/parser/string/shift/operand/operand.d.ts +0 -9
  125. package/dist/parser/string/shift/operand/operand.d.ts.map +0 -1
  126. package/dist/parser/string/shift/operand/operand.js +0 -17
  127. package/dist/parser/string/shift/operand/operand.js.map +0 -1
  128. package/dist/parser/string/shift/operand/unenclosed.d.ts +0 -36
  129. package/dist/parser/string/shift/operand/unenclosed.d.ts.map +0 -1
  130. package/dist/parser/string/shift/operand/unenclosed.js +0 -68
  131. package/dist/parser/string/shift/operand/unenclosed.js.map +0 -1
  132. package/dist/parser/string/shift/operator/bounds.d.ts +0 -55
  133. package/dist/parser/string/shift/operator/bounds.d.ts.map +0 -1
  134. package/dist/parser/string/shift/operator/bounds.js +0 -95
  135. package/dist/parser/string/shift/operator/bounds.js.map +0 -1
  136. package/dist/parser/string/shift/operator/divisor.d.ts.map +0 -1
  137. package/dist/parser/string/shift/operator/divisor.js.map +0 -1
  138. package/dist/parser/string/shift/operator/operator.d.ts.map +0 -1
  139. package/dist/parser/string/shift/operator/operator.js +0 -29
  140. package/dist/parser/string/shift/operator/operator.js.map +0 -1
  141. package/dist/parser/string/shift/scanner.d.ts.map +0 -1
  142. package/dist/parser/string/shift/scanner.js.map +0 -1
  143. package/dist/parser/string/string.d.ts +0 -26
  144. package/dist/parser/string/string.d.ts.map +0 -1
  145. package/dist/parser/string/string.js +0 -31
  146. package/dist/parser/string/string.js.map +0 -1
  147. package/dist/parser/tuple.d.ts +0 -73
  148. package/dist/parser/tuple.d.ts.map +0 -1
  149. package/dist/parser/tuple.js +0 -134
  150. package/dist/parser/tuple.js.map +0 -1
  151. package/dist/scope.d.ts +0 -159
  152. package/dist/scope.d.ts.map +0 -1
  153. package/dist/scope.js +0 -248
  154. package/dist/scope.js.map +0 -1
  155. package/dist/scopes/ark.d.ts +0 -38
  156. package/dist/scopes/ark.d.ts.map +0 -1
  157. package/dist/scopes/ark.js +0 -24
  158. package/dist/scopes/ark.js.map +0 -1
  159. package/dist/scopes/jsObjects.d.ts +0 -19
  160. package/dist/scopes/jsObjects.d.ts.map +0 -1
  161. package/dist/scopes/jsObjects.js +0 -15
  162. package/dist/scopes/jsObjects.js.map +0 -1
  163. package/dist/scopes/parsing.d.ts +0 -18
  164. package/dist/scopes/parsing.d.ts.map +0 -1
  165. package/dist/scopes/parsing.js +0 -61
  166. package/dist/scopes/parsing.js.map +0 -1
  167. package/dist/scopes/tsGenerics.d.ts +0 -17
  168. package/dist/scopes/tsGenerics.d.ts.map +0 -1
  169. package/dist/scopes/tsGenerics.js +0 -13
  170. package/dist/scopes/tsGenerics.js.map +0 -1
  171. package/dist/scopes/tsKeywords.d.ts +0 -24
  172. package/dist/scopes/tsKeywords.d.ts.map +0 -1
  173. package/dist/scopes/tsKeywords.js +0 -20
  174. package/dist/scopes/tsKeywords.js.map +0 -1
  175. package/dist/scopes/utils/creditCard.d.ts +0 -3
  176. package/dist/scopes/utils/creditCard.d.ts.map +0 -1
  177. package/dist/scopes/utils/creditCard.js +0 -36
  178. package/dist/scopes/utils/creditCard.js.map +0 -1
  179. package/dist/scopes/utils/date.d.ts +0 -24
  180. package/dist/scopes/utils/date.d.ts.map +0 -1
  181. package/dist/scopes/utils/date.js +0 -53
  182. package/dist/scopes/utils/date.js.map +0 -1
  183. package/dist/scopes/validation.d.ts +0 -20
  184. package/dist/scopes/validation.d.ts.map +0 -1
  185. package/dist/scopes/validation.js +0 -57
  186. package/dist/scopes/validation.js.map +0 -1
  187. package/dist/type.d.ts +0 -101
  188. package/dist/type.d.ts.map +0 -1
  189. package/dist/type.js +0 -138
  190. package/dist/type.js.map +0 -1
  191. package/main.ts +0 -20
  192. package/match.ts +0 -137
  193. package/parser/definition.ts +0 -156
  194. package/parser/generic.ts +0 -208
  195. package/parser/objectLiteral.ts +0 -120
  196. package/parser/semantic/bounds.ts +0 -63
  197. package/parser/semantic/divisor.ts +0 -13
  198. package/parser/semantic/intersections.ts +0 -47
  199. package/parser/semantic/semantic.ts +0 -107
  200. package/parser/semantic/utils.ts +0 -26
  201. package/parser/semantic/validate.ts +0 -105
  202. package/parser/shared.ts +0 -141
  203. package/parser/string/reduce/dynamic.ts +0 -181
  204. package/parser/string/reduce/shared.ts +0 -73
  205. package/parser/string/reduce/static.ts +0 -300
  206. package/parser/string/shift/operand/date.ts +0 -57
  207. package/parser/string/shift/operand/enclosed.ts +0 -119
  208. package/parser/string/shift/operand/operand.ts +0 -48
  209. package/parser/string/shift/operand/unenclosed.ts +0 -282
  210. package/parser/string/shift/operator/bounds.ts +0 -229
  211. package/parser/string/shift/operator/divisor.ts +0 -36
  212. package/parser/string/shift/operator/operator.ts +0 -82
  213. package/parser/string/shift/scanner.ts +0 -209
  214. package/parser/string/string.ts +0 -93
  215. package/parser/tuple.ts +0 -475
  216. package/scope.ts +0 -573
  217. package/scopes/ark.ts +0 -64
  218. package/scopes/jsObjects.ts +0 -29
  219. package/scopes/parsing.ts +0 -81
  220. package/scopes/tsGenerics.ts +0 -28
  221. package/scopes/tsKeywords.ts +0 -39
  222. package/scopes/utils/creditCard.ts +0 -37
  223. package/scopes/utils/date.ts +0 -108
  224. package/scopes/validation.ts +0 -81
  225. package/type.ts +0 -403
@@ -1,300 +0,0 @@
1
- import type { Completion, defined, ErrorMessage } from "@arktype/util"
2
- import type {
3
- Comparator,
4
- InvertedComparators,
5
- LimitLiteral,
6
- MaxComparator,
7
- MinComparator
8
- } from "../shift/operator/bounds.js"
9
- import type { Scanner } from "../shift/scanner.js"
10
- import type {
11
- OpenLeftBound,
12
- StringifiablePrefixOperator,
13
- writeMultipleLeftBoundsMessage,
14
- writeOpenRangeMessage,
15
- writeUnclosedGroupMessage,
16
- writeUnmatchedGroupCloseMessage,
17
- writeUnpairableComparatorMessage
18
- } from "./shared.js"
19
-
20
- export type StaticState = {
21
- root: unknown
22
- branches: BranchState
23
- groups: BranchState[]
24
- finalizer: Scanner.FinalizingLookahead | ErrorMessage | undefined
25
- scanned: string
26
- unscanned: string
27
- }
28
-
29
- export type AutocompletePrefix = `${StringifiablePrefixOperator} `
30
-
31
- type BranchState = {
32
- prefixes: StringifiablePrefixOperator[]
33
- leftBound: OpenLeftBound | undefined
34
- "&": unknown
35
- "|": unknown
36
- }
37
-
38
- export namespace state {
39
- export type initialize<def extends string> = from<{
40
- root: undefined
41
- branches: initialBranches
42
- groups: []
43
- finalizer: undefined
44
- scanned: ""
45
- unscanned: def
46
- }>
47
-
48
- export type error<message extends string> = from<{
49
- root: undefined
50
- branches: initialBranches
51
- groups: []
52
- finalizer: ErrorMessage<message>
53
- scanned: ""
54
- unscanned: ""
55
- }>
56
-
57
- export type completion<text extends string> = from<{
58
- root: undefined
59
- branches: initialBranches
60
- groups: []
61
- finalizer: Completion<text>
62
- scanned: ""
63
- unscanned: ""
64
- }>
65
-
66
- type initialBranches = branchesFrom<{
67
- prefixes: []
68
- leftBound: undefined
69
- "&": undefined
70
- "|": undefined
71
- }>
72
-
73
- type updateScanned<
74
- previousScanned extends string,
75
- previousUnscanned extends string,
76
- updatedUnscanned extends string
77
- > = previousUnscanned extends `${infer justScanned}${updatedUnscanned}`
78
- ? `${previousScanned}${justScanned}`
79
- : previousScanned
80
-
81
- export type setRoot<
82
- s extends StaticState,
83
- root,
84
- unscanned extends string
85
- > = from<{
86
- root: root
87
- branches: s["branches"]
88
- groups: s["groups"]
89
- finalizer: s["finalizer"]
90
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
91
- unscanned: unscanned
92
- }>
93
-
94
- export type addPrefix<
95
- s extends StaticState,
96
- prefix extends StringifiablePrefixOperator,
97
- unscanned extends string
98
- > = from<{
99
- root: s["root"]
100
- branches: {
101
- prefixes: [...s["branches"]["prefixes"], prefix]
102
- leftBound: s["branches"]["leftBound"]
103
- "&": s["branches"]["&"]
104
- "|": s["branches"]["|"]
105
- }
106
- groups: s["groups"]
107
- finalizer: s["finalizer"]
108
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
109
- unscanned: unscanned
110
- }>
111
-
112
- export type reduceBranch<
113
- s extends StaticState,
114
- token extends "|" | "&",
115
- unscanned extends string
116
- > = s["branches"]["leftBound"] extends {}
117
- ? openRangeError<s["branches"]["leftBound"]>
118
- : from<{
119
- root: undefined
120
- branches: {
121
- prefixes: []
122
- leftBound: undefined
123
- "&": token extends "&" ? mergeToIntersection<s> : undefined
124
- "|": token extends "|" ? mergeToUnion<s> : s["branches"]["|"]
125
- }
126
- groups: s["groups"]
127
- finalizer: s["finalizer"]
128
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
129
- unscanned: unscanned
130
- }>
131
-
132
- export type reduceLeftBound<
133
- s extends StaticState,
134
- limit extends LimitLiteral,
135
- comparator extends Comparator,
136
- unscanned extends string
137
- > = comparator extends "<" | "<="
138
- ? s["branches"]["leftBound"] extends {}
139
- ? state.error<
140
- writeMultipleLeftBoundsMessage<
141
- s["branches"]["leftBound"]["limit"],
142
- s["branches"]["leftBound"]["comparator"],
143
- limit,
144
- InvertedComparators[comparator]
145
- >
146
- >
147
- : from<{
148
- root: undefined
149
- branches: {
150
- prefixes: s["branches"]["prefixes"]
151
- leftBound: {
152
- limit: limit
153
- comparator: InvertedComparators[comparator]
154
- }
155
- "&": s["branches"]["&"]
156
- "|": s["branches"]["|"]
157
- }
158
- groups: s["groups"]
159
- finalizer: s["finalizer"]
160
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
161
- unscanned: unscanned
162
- }>
163
- : state.error<writeUnpairableComparatorMessage<comparator>>
164
-
165
- export type reduceRange<
166
- s extends StaticState,
167
- minLimit extends LimitLiteral,
168
- minComparator extends MinComparator,
169
- maxComparator extends MaxComparator,
170
- maxLimit extends LimitLiteral,
171
- unscanned extends string
172
- > = state.from<{
173
- root: [minLimit, minComparator, [s["root"], maxComparator, maxLimit]]
174
- branches: {
175
- prefixes: s["branches"]["prefixes"]
176
- leftBound: undefined
177
- "&": s["branches"]["&"]
178
- "|": s["branches"]["|"]
179
- }
180
- groups: s["groups"]
181
- finalizer: s["finalizer"]
182
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
183
- unscanned: unscanned
184
- }>
185
-
186
- export type reduceSingleBound<
187
- s extends StaticState,
188
- comparator extends Comparator,
189
- limit extends LimitLiteral,
190
- unscanned extends string
191
- > = state.from<{
192
- root: [s["root"], comparator, limit]
193
- branches: {
194
- prefixes: s["branches"]["prefixes"]
195
- leftBound: undefined
196
- "&": s["branches"]["&"]
197
- "|": s["branches"]["|"]
198
- }
199
- groups: s["groups"]
200
- finalizer: s["finalizer"]
201
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
202
- unscanned: unscanned
203
- }>
204
-
205
- type mergeToUnion<s extends StaticState> =
206
- s["branches"]["|"] extends undefined
207
- ? mergeToIntersection<s>
208
- : [s["branches"]["|"], "|", mergeToIntersection<s>]
209
-
210
- type mergeToIntersection<s extends StaticState> =
211
- s["branches"]["&"] extends undefined
212
- ? mergePrefixes<s>
213
- : [s["branches"]["&"], "&", mergePrefixes<s>]
214
-
215
- type mergePrefixes<
216
- s extends StaticState,
217
- remaining extends unknown[] = s["branches"]["prefixes"]
218
- > = remaining extends [infer head, ...infer tail]
219
- ? [head, mergePrefixes<s, tail>]
220
- : s["root"]
221
-
222
- type popGroup<stack extends BranchState[], top extends BranchState> = [
223
- ...stack,
224
- top
225
- ]
226
-
227
- export type finalizeGroup<
228
- s extends StaticState,
229
- unscanned extends string
230
- > = s["branches"]["leftBound"] extends {}
231
- ? openRangeError<s["branches"]["leftBound"]>
232
- : s["groups"] extends popGroup<infer stack, infer top>
233
- ? from<{
234
- groups: stack
235
- branches: top
236
- root: mergeToUnion<s>
237
- finalizer: s["finalizer"]
238
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
239
- unscanned: unscanned
240
- }>
241
- : state.error<writeUnmatchedGroupCloseMessage<unscanned>>
242
-
243
- export type reduceGroupOpen<
244
- s extends StaticState,
245
- unscanned extends string
246
- > = from<{
247
- groups: [...s["groups"], s["branches"]]
248
- branches: initialBranches
249
- root: undefined
250
- finalizer: s["finalizer"]
251
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
252
- unscanned: unscanned
253
- }>
254
-
255
- export type finalize<
256
- s extends StaticState,
257
- finalizer extends Scanner.FinalizingLookahead
258
- > = s["groups"] extends []
259
- ? s["branches"]["leftBound"] extends {}
260
- ? openRangeError<s["branches"]["leftBound"]>
261
- : from<{
262
- root: mergeToUnion<s>
263
- groups: s["groups"]
264
- branches: initialBranches
265
- finalizer: finalizer
266
- scanned: s["scanned"]
267
- unscanned: s["unscanned"]
268
- }>
269
- : state.error<writeUnclosedGroupMessage<")">>
270
-
271
- type openRangeError<range extends defined<BranchState["leftBound"]>> =
272
- state.error<writeOpenRangeMessage<range["limit"], range["comparator"]>>
273
-
274
- export type previousOperator<s extends StaticState> =
275
- s["branches"]["leftBound"] extends {}
276
- ? s["branches"]["leftBound"]["comparator"]
277
- : s["branches"]["prefixes"] extends [
278
- ...unknown[],
279
- infer tail extends string
280
- ]
281
- ? tail
282
- : s["branches"]["&"] extends {}
283
- ? "&"
284
- : s["branches"]["|"] extends {}
285
- ? "|"
286
- : undefined
287
-
288
- export type scanTo<s extends StaticState, unscanned extends string> = from<{
289
- root: s["root"]
290
- branches: s["branches"]
291
- groups: s["groups"]
292
- finalizer: s["finalizer"]
293
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
294
- unscanned: unscanned
295
- }>
296
-
297
- export type from<s extends StaticState> = s
298
-
299
- export type branchesFrom<b extends BranchState> = b
300
- }
@@ -1,57 +0,0 @@
1
- import { throwParseError, tryParseNumber } from "@arktype/util"
2
-
3
- export type DateLiteral<source extends string = string> =
4
- | `d"${source}"`
5
- | `d'${source}'`
6
-
7
- export const isDateLiteral = (value: unknown): value is DateLiteral =>
8
- typeof value === "string" &&
9
- value[0] === "d" &&
10
- (value[1] === "'" || value[1] === '"') &&
11
- value.at(-1) === value[1]
12
-
13
- export const isValidDate = (d: Date) => d.toString() !== "Invalid Date"
14
-
15
- export const extractDateLiteralSource = <literal extends DateLiteral>(
16
- literal: literal
17
- ) => literal.slice(2, -1) as extractDateLiteralSource<literal>
18
-
19
- type extractDateLiteralSource<literal extends DateLiteral> =
20
- literal extends DateLiteral<infer source> ? source : never
21
-
22
- export const writeInvalidDateMessage = <source extends string>(
23
- source: source
24
- ): writeInvalidDateMessage<source> =>
25
- `'${source}' could not be parsed by the Date constructor`
26
-
27
- export type writeInvalidDateMessage<source extends string> =
28
- `'${source}' could not be parsed by the Date constructor`
29
-
30
- export type DateInput = ConstructorParameters<typeof Date>[0]
31
-
32
- export const tryParseDate = <errorOnFail extends boolean | string>(
33
- source: string,
34
- errorOnFail?: errorOnFail
35
- ) => maybeParseDate(source, errorOnFail)
36
-
37
- const maybeParseDate = <errorOnFail extends boolean | string>(
38
- source: string,
39
- errorOnFail?: errorOnFail
40
- ): Date | (errorOnFail extends true | string ? never : undefined) => {
41
- const stringParsedDate = new Date(source)
42
- if (isValidDate(stringParsedDate)) {
43
- return stringParsedDate
44
- }
45
- const epochMillis = tryParseNumber(source)
46
- if (epochMillis !== undefined) {
47
- const numberParsedDate = new Date(epochMillis)
48
- if (isValidDate(numberParsedDate)) {
49
- return numberParsedDate
50
- }
51
- }
52
- return errorOnFail
53
- ? throwParseError(
54
- errorOnFail === true ? writeInvalidDateMessage(source) : errorOnFail
55
- )
56
- : (undefined as never)
57
- }
@@ -1,119 +0,0 @@
1
- import { node } from "@arktype/schema"
2
- import { isKeyOf } from "@arktype/util"
3
- import type { RegexLiteral } from "../../../semantic/semantic.js"
4
- import type { DynamicState } from "../../reduce/dynamic.js"
5
- import type { state, StaticState } from "../../reduce/static.js"
6
- import type { Scanner } from "../scanner.js"
7
- import { tryParseDate, writeInvalidDateMessage } from "./date.js"
8
-
9
- export type StringLiteral<Text extends string = string> =
10
- | DoubleQuotedStringLiteral<Text>
11
- | SingleQuotedStringLiteral<Text>
12
-
13
- export type DoubleQuotedStringLiteral<Text extends string = string> =
14
- `"${Text}"`
15
-
16
- export type SingleQuotedStringLiteral<Text extends string = string> =
17
- `'${Text}'`
18
-
19
- export const parseEnclosed = (
20
- s: DynamicState,
21
- enclosing: EnclosingStartToken
22
- ) => {
23
- const enclosed = s.scanner.shiftUntil(
24
- untilLookaheadIsClosing[enclosingTokens[enclosing]]
25
- )
26
- if (s.scanner.lookahead === "") {
27
- return s.error(writeUnterminatedEnclosedMessage(enclosed, enclosing))
28
- }
29
- // Shift the scanner one additional time for the second enclosing token
30
- const token = `${enclosing}${enclosed}${s.scanner.shift()}`
31
- if (enclosing === "/") {
32
- // fail parsing if the regex is invalid
33
- new RegExp(enclosed)
34
- s.root = node({ basis: "string", pattern: token as RegexLiteral })
35
- } else if (isKeyOf(enclosing, enclosingQuote)) {
36
- s.root = node({ is: enclosed })
37
- } else {
38
- const date = tryParseDate(enclosed, writeInvalidDateMessage(enclosed))
39
- s.root = node({ is: date, description: token })
40
- }
41
- }
42
-
43
- export type parseEnclosed<
44
- s extends StaticState,
45
- enclosingStart extends EnclosingStartToken,
46
- unscanned extends string
47
- > = Scanner.shiftUntil<
48
- unscanned,
49
- EnclosingTokens[enclosingStart]
50
- > extends Scanner.shiftResult<infer scanned, infer nextUnscanned>
51
- ? nextUnscanned extends ""
52
- ? state.error<writeUnterminatedEnclosedMessage<scanned, enclosingStart>>
53
- : state.setRoot<
54
- s,
55
- `${enclosingStart}${scanned}${EnclosingTokens[enclosingStart]}`,
56
- nextUnscanned extends Scanner.shift<string, infer unscanned>
57
- ? unscanned
58
- : ""
59
- >
60
- : never
61
-
62
- export const enclosingQuote = {
63
- "'": 1,
64
- '"': 1
65
- } as const
66
-
67
- export type EnclosingQuote = keyof typeof enclosingQuote
68
-
69
- export const enclosingChar = {
70
- "/": 1,
71
- ...enclosingQuote
72
- } as const
73
-
74
- export const enclosingTokens = {
75
- "d'": "'",
76
- 'd"': '"',
77
- "'": "'",
78
- '"': '"',
79
- "/": "/"
80
- } as const
81
-
82
- export type EnclosingTokens = typeof enclosingTokens
83
-
84
- export type EnclosingStartToken = keyof EnclosingTokens
85
-
86
- export type EnclosingEndToken = EnclosingTokens[keyof EnclosingTokens]
87
-
88
- export const untilLookaheadIsClosing: Record<
89
- EnclosingEndToken,
90
- Scanner.UntilCondition
91
- > = {
92
- "'": (scanner) => scanner.lookahead === `'`,
93
- '"': (scanner) => scanner.lookahead === `"`,
94
- "/": (scanner) => scanner.lookahead === `/`
95
- }
96
-
97
- const enclosingCharDescriptions = {
98
- '"': "double-quote",
99
- "'": "single-quote",
100
- "/": "forward slash"
101
- } as const
102
-
103
- type enclosingCharDescriptions = typeof enclosingCharDescriptions
104
-
105
- export const writeUnterminatedEnclosedMessage = <
106
- fragment extends string,
107
- enclosingStart extends EnclosingStartToken
108
- >(
109
- fragment: fragment,
110
- enclosingStart: enclosingStart
111
- ): writeUnterminatedEnclosedMessage<fragment, enclosingStart> =>
112
- `${enclosingStart}${fragment} requires a closing ${
113
- enclosingCharDescriptions[enclosingTokens[enclosingStart]]
114
- }`
115
-
116
- export type writeUnterminatedEnclosedMessage<
117
- fragment extends string,
118
- enclosingStart extends EnclosingStartToken
119
- > = `${enclosingStart}${fragment} requires a closing ${enclosingCharDescriptions[EnclosingTokens[enclosingStart]]}`
@@ -1,48 +0,0 @@
1
- import type { DynamicState } from "../../reduce/dynamic.js"
2
- import type { state, StaticState } from "../../reduce/static.js"
3
- import type { BaseCompletions } from "../../string.js"
4
- import { Scanner } from "../scanner.js"
5
- import {
6
- enclosingChar,
7
- parseEnclosed,
8
- type EnclosingEndToken,
9
- type EnclosingQuote,
10
- type EnclosingStartToken
11
- } from "./enclosed.js"
12
- import { parseUnenclosed, writeMissingOperandMessage } from "./unenclosed.js"
13
-
14
- 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)
28
-
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>}`>