arktype 2.0.0-dev.10 → 2.0.0-dev.12

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 (146) hide show
  1. package/out/api.d.ts +6 -0
  2. package/out/api.js +5 -0
  3. package/out/ark.d.ts +1 -1
  4. package/out/config.d.ts +1 -0
  5. package/out/config.js +2 -0
  6. package/out/generic.d.ts +3 -3
  7. package/out/match.js +1 -1
  8. package/out/module.d.ts +7 -6
  9. package/out/module.js +2 -3
  10. package/out/parser/definition.d.ts +11 -9
  11. package/out/parser/definition.js +2 -2
  12. package/out/parser/objectLiteral.d.ts +8 -13
  13. package/out/parser/objectLiteral.js +20 -23
  14. package/out/parser/semantic/bounds.d.ts +3 -3
  15. package/out/parser/semantic/divisor.d.ts +2 -2
  16. package/out/parser/semantic/infer.d.ts +1 -1
  17. package/out/parser/semantic/validate.d.ts +6 -4
  18. package/out/parser/string/reduce/dynamic.d.ts +6 -6
  19. package/out/parser/string/reduce/shared.d.ts +16 -16
  20. package/out/parser/string/reduce/shared.js +4 -2
  21. package/out/parser/string/shift/operand/date.d.ts +2 -2
  22. package/out/parser/string/shift/operand/enclosed.d.ts +17 -3
  23. package/out/parser/string/shift/operand/enclosed.js +2 -1
  24. package/out/parser/string/shift/operand/genericArgs.d.ts +4 -4
  25. package/out/parser/string/shift/operand/unenclosed.d.ts +9 -7
  26. package/out/parser/string/shift/operand/unenclosed.js +2 -2
  27. package/out/parser/string/shift/operator/bounds.d.ts +5 -5
  28. package/out/parser/string/shift/operator/bounds.js +3 -3
  29. package/out/parser/string/shift/operator/divisor.d.ts +1 -1
  30. package/out/parser/string/shift/operator/operator.d.ts +1 -1
  31. package/out/parser/string/string.d.ts +2 -2
  32. package/out/parser/tuple.d.ts +11 -11
  33. package/out/parser/tuple.js +3 -3
  34. package/out/scope.d.ts +15 -13
  35. package/out/scope.js +7 -7
  36. package/out/type.d.ts +4 -4
  37. package/out/type.js +3 -3
  38. package/package.json +8 -10
  39. package/__tests__/array.test.ts +0 -391
  40. package/__tests__/badDefinitionType.test.ts +0 -58
  41. package/__tests__/basis.test.ts +0 -50
  42. package/__tests__/bounds.test.ts +0 -318
  43. package/__tests__/cast.test.ts +0 -37
  44. package/__tests__/completions.test.ts +0 -51
  45. package/__tests__/config.test.ts +0 -96
  46. package/__tests__/dateLiteral.test.ts +0 -36
  47. package/__tests__/declared.test.ts +0 -124
  48. package/__tests__/discrimination.test.ts +0 -74
  49. package/__tests__/divisor.test.ts +0 -114
  50. package/__tests__/enclosed.test.ts +0 -107
  51. package/__tests__/examples.test.ts +0 -56
  52. package/__tests__/expressions.test.ts +0 -210
  53. package/__tests__/generateBenchData.ts +0 -89
  54. package/__tests__/generated/cyclic.ts +0 -3058
  55. package/__tests__/generic.test.ts +0 -313
  56. package/__tests__/group.test.ts +0 -85
  57. package/__tests__/hkt.test.ts +0 -22
  58. package/__tests__/imports.test.ts +0 -126
  59. package/__tests__/instanceof.test.ts +0 -104
  60. package/__tests__/intersection.test.ts +0 -108
  61. package/__tests__/keyTraversal.test.ts +0 -77
  62. package/__tests__/keyof.test.ts +0 -150
  63. package/__tests__/keywords.test.ts +0 -214
  64. package/__tests__/literal.test.ts +0 -90
  65. package/__tests__/match.bench.ts +0 -31
  66. package/__tests__/match.test.ts +0 -234
  67. package/__tests__/methods.bench.ts +0 -22
  68. package/__tests__/narrow.test.ts +0 -100
  69. package/__tests__/object.bench.ts +0 -28
  70. package/__tests__/objectLiteral.test.ts +0 -442
  71. package/__tests__/operand.bench.ts +0 -45
  72. package/__tests__/operator.bench.ts +0 -130
  73. package/__tests__/pipe.test.ts +0 -501
  74. package/__tests__/realWorld.test.ts +0 -63
  75. package/__tests__/regex.test.ts +0 -68
  76. package/__tests__/scope.bench.ts +0 -39
  77. package/__tests__/scope.test.ts +0 -374
  78. package/__tests__/serialization.test.ts +0 -19
  79. package/__tests__/string.test.ts +0 -52
  80. package/__tests__/submodule.test.ts +0 -86
  81. package/__tests__/this.test.ts +0 -83
  82. package/__tests__/thunk.test.ts +0 -86
  83. package/__tests__/traverse.test.ts +0 -141
  84. package/__tests__/tsGenerics.test.ts +0 -12
  85. package/__tests__/type.test.ts +0 -36
  86. package/__tests__/typeReference.test.ts +0 -39
  87. package/__tests__/unenclosed.test.ts +0 -136
  88. package/__tests__/union.test.ts +0 -214
  89. package/ark.ts +0 -47
  90. package/generic.ts +0 -69
  91. package/main.ts +0 -6
  92. package/match.ts +0 -218
  93. package/module.ts +0 -15
  94. package/out/ark.js.map +0 -1
  95. package/out/generic.js.map +0 -1
  96. package/out/main.js.map +0 -1
  97. package/out/match.js.map +0 -1
  98. package/out/module.js.map +0 -1
  99. package/out/parser/definition.js.map +0 -1
  100. package/out/parser/generic.js.map +0 -1
  101. package/out/parser/objectLiteral.js.map +0 -1
  102. package/out/parser/semantic/bounds.js.map +0 -1
  103. package/out/parser/semantic/divisor.js.map +0 -1
  104. package/out/parser/semantic/infer.js.map +0 -1
  105. package/out/parser/semantic/utils.js.map +0 -1
  106. package/out/parser/semantic/validate.js.map +0 -1
  107. package/out/parser/string/reduce/dynamic.js.map +0 -1
  108. package/out/parser/string/reduce/shared.js.map +0 -1
  109. package/out/parser/string/reduce/static.js.map +0 -1
  110. package/out/parser/string/shift/operand/date.js.map +0 -1
  111. package/out/parser/string/shift/operand/enclosed.js.map +0 -1
  112. package/out/parser/string/shift/operand/genericArgs.js.map +0 -1
  113. package/out/parser/string/shift/operand/operand.js.map +0 -1
  114. package/out/parser/string/shift/operand/unenclosed.js.map +0 -1
  115. package/out/parser/string/shift/operator/bounds.js.map +0 -1
  116. package/out/parser/string/shift/operator/divisor.js.map +0 -1
  117. package/out/parser/string/shift/operator/operator.js.map +0 -1
  118. package/out/parser/string/shift/scanner.js.map +0 -1
  119. package/out/parser/string/string.js.map +0 -1
  120. package/out/parser/tuple.js.map +0 -1
  121. package/out/scope.js.map +0 -1
  122. package/out/type.js.map +0 -1
  123. package/parser/definition.ts +0 -143
  124. package/parser/generic.ts +0 -66
  125. package/parser/objectLiteral.ts +0 -233
  126. package/parser/semantic/bounds.ts +0 -62
  127. package/parser/semantic/divisor.ts +0 -11
  128. package/parser/semantic/infer.ts +0 -141
  129. package/parser/semantic/utils.ts +0 -15
  130. package/parser/semantic/validate.ts +0 -117
  131. package/parser/string/reduce/dynamic.ts +0 -208
  132. package/parser/string/reduce/shared.ts +0 -99
  133. package/parser/string/reduce/static.ts +0 -296
  134. package/parser/string/shift/operand/date.ts +0 -55
  135. package/parser/string/shift/operand/enclosed.ts +0 -123
  136. package/parser/string/shift/operand/genericArgs.ts +0 -128
  137. package/parser/string/shift/operand/operand.ts +0 -42
  138. package/parser/string/shift/operand/unenclosed.ts +0 -217
  139. package/parser/string/shift/operator/bounds.ts +0 -270
  140. package/parser/string/shift/operator/divisor.ts +0 -33
  141. package/parser/string/shift/operator/operator.ts +0 -71
  142. package/parser/string/shift/scanner.ts +0 -210
  143. package/parser/string/string.ts +0 -85
  144. package/parser/tuple.ts +0 -542
  145. package/scope.ts +0 -291
  146. package/type.ts +0 -266
@@ -1,208 +0,0 @@
1
- import type { LimitLiteral, RawSchema } from "@arktype/schema"
2
- import {
3
- isKeyOf,
4
- type requireKeys,
5
- throwInternalError,
6
- throwParseError
7
- } from "@arktype/util"
8
- import type { ParseContext } from "../../../scope.js"
9
- import type { InfixOperator } from "../../semantic/infer.js"
10
- import { parseOperand } from "../shift/operand/operand.js"
11
- import { parseOperator } from "../shift/operator/operator.js"
12
- import { Scanner } from "../shift/scanner.js"
13
- import { parseUntilFinalizer } from "../string.js"
14
- import {
15
- type Comparator,
16
- type MinComparator,
17
- type OpenLeftBound,
18
- type StringifiablePrefixOperator,
19
- invertedComparators,
20
- minComparators,
21
- writeMultipleLeftBoundsMessage,
22
- writeOpenRangeMessage,
23
- writeUnclosedGroupMessage,
24
- writeUnmatchedGroupCloseMessage,
25
- writeUnpairableComparatorMessage
26
- } from "./shared.js"
27
-
28
- type BranchState = {
29
- prefixes: StringifiablePrefixOperator[]
30
- leftBound: OpenLeftBound | null
31
- intersection: RawSchema | null
32
- union: RawSchema | null
33
- }
34
-
35
- export type DynamicStateWithRoot = requireKeys<DynamicState, "root">
36
-
37
- export class DynamicState {
38
- readonly scanner: Scanner
39
- // set root type to `any` so that all constraints can be applied
40
- root: RawSchema<any> | undefined
41
- branches: BranchState = {
42
- prefixes: [],
43
- leftBound: null,
44
- intersection: null,
45
- union: null
46
- }
47
- finalizer: Scanner.FinalizingLookahead | undefined
48
- groups: BranchState[] = []
49
-
50
- constructor(
51
- def: string,
52
- public readonly ctx: ParseContext
53
- ) {
54
- this.scanner = new Scanner(def)
55
- }
56
-
57
- error(message: string): never {
58
- return throwParseError(message)
59
- }
60
-
61
- hasRoot(): this is DynamicStateWithRoot {
62
- return this.root !== undefined
63
- }
64
-
65
- setRoot(root: RawSchema): void {
66
- this.root = root
67
- }
68
-
69
- unsetRoot(): this["root"] {
70
- const value = this.root
71
- this.root = undefined
72
- return value
73
- }
74
-
75
- constrainRoot(...args: Parameters<RawSchema<any>["constrain"]>): void {
76
- this.root = this.root!.constrain(args[0], args[1])
77
- }
78
-
79
- finalize(finalizer: Scanner.FinalizingLookahead): void {
80
- if (this.groups.length) return this.error(writeUnclosedGroupMessage(")"))
81
-
82
- this.finalizeBranches()
83
- this.finalizer = finalizer
84
- }
85
-
86
- reduceLeftBound(limit: LimitLiteral, comparator: Comparator): void {
87
- const invertedComparator = invertedComparators[comparator]
88
- if (!isKeyOf(invertedComparator, minComparators))
89
- return this.error(writeUnpairableComparatorMessage(comparator))
90
-
91
- if (this.branches.leftBound) {
92
- return this.error(
93
- writeMultipleLeftBoundsMessage(
94
- this.branches.leftBound.limit,
95
- this.branches.leftBound.comparator,
96
- limit,
97
- invertedComparator
98
- )
99
- )
100
- }
101
- this.branches.leftBound = {
102
- comparator: invertedComparator,
103
- limit
104
- }
105
- }
106
-
107
- finalizeBranches(): void {
108
- this.assertRangeUnset()
109
- if (this.branches.union) {
110
- this.pushRootToBranch("|")
111
- this.root = this.branches.union
112
- } else if (this.branches.intersection) {
113
- this.pushRootToBranch("&")
114
- this.root = this.branches.intersection
115
- } else this.applyPrefixes()
116
- }
117
-
118
- finalizeGroup(): void {
119
- this.finalizeBranches()
120
- const topBranchState = this.groups.pop()
121
- if (!topBranchState)
122
- return this.error(writeUnmatchedGroupCloseMessage(this.scanner.unscanned))
123
-
124
- this.branches = topBranchState
125
- }
126
-
127
- addPrefix(prefix: StringifiablePrefixOperator): void {
128
- this.branches.prefixes.push(prefix)
129
- }
130
-
131
- applyPrefixes(): void {
132
- while (this.branches.prefixes.length) {
133
- const lastPrefix = this.branches.prefixes.pop()!
134
- this.root =
135
- lastPrefix === "keyof" ?
136
- this.root!.keyof()
137
- : throwInternalError(`Unexpected prefix '${lastPrefix}'`)
138
- }
139
- }
140
-
141
- pushRootToBranch(token: "|" | "&"): void {
142
- this.assertRangeUnset()
143
- this.applyPrefixes()
144
- const root = this.root!
145
- this.branches.intersection = this.branches.intersection?.and(root) ?? root
146
- if (token === "|") {
147
- this.branches.union =
148
- this.branches.union?.or(this.branches.intersection) ??
149
- this.branches.intersection
150
- this.branches.intersection = null
151
- }
152
- this.root = undefined
153
- }
154
-
155
- parseUntilFinalizer(): DynamicStateWithRoot {
156
- return parseUntilFinalizer(
157
- new DynamicState(this.scanner.unscanned, this.ctx)
158
- )
159
- }
160
-
161
- parseOperator(this: DynamicStateWithRoot): void {
162
- return parseOperator(this)
163
- }
164
-
165
- parseOperand(): void {
166
- return parseOperand(this)
167
- }
168
-
169
- private assertRangeUnset() {
170
- if (this.branches.leftBound) {
171
- return this.error(
172
- writeOpenRangeMessage(
173
- this.branches.leftBound.limit,
174
- this.branches.leftBound.comparator
175
- )
176
- )
177
- }
178
- }
179
-
180
- reduceGroupOpen(): void {
181
- this.groups.push(this.branches)
182
- this.branches = {
183
- prefixes: [],
184
- leftBound: null,
185
- union: null,
186
- intersection: null
187
- }
188
- }
189
-
190
- previousOperator():
191
- | MinComparator
192
- | StringifiablePrefixOperator
193
- | InfixOperator
194
- | undefined {
195
- return (
196
- this.branches.leftBound?.comparator ??
197
- this.branches.prefixes.at(-1) ??
198
- (this.branches.intersection ? "&"
199
- : this.branches.union ? "|"
200
- : undefined)
201
- )
202
- }
203
-
204
- shiftedByOne(): this {
205
- this.scanner.shift()
206
- return this
207
- }
208
- }
@@ -1,99 +0,0 @@
1
- import type { LimitLiteral } from "@arktype/schema"
2
-
3
- export type StringifiablePrefixOperator = "keyof"
4
-
5
- export const minComparators = {
6
- ">": true,
7
- ">=": true
8
- } as const
9
-
10
- export type MinComparator = keyof typeof minComparators
11
-
12
- export const maxComparators = {
13
- "<": true,
14
- "<=": true
15
- } as const
16
-
17
- export type MaxComparator = keyof typeof maxComparators
18
-
19
- export const comparators = {
20
- ...minComparators,
21
- ...maxComparators,
22
- "==": true
23
- }
24
-
25
- export type Comparator = keyof typeof comparators
26
-
27
- export const invertedComparators = {
28
- "<": ">",
29
- ">": "<",
30
- "<=": ">=",
31
- ">=": "<=",
32
- "==": "=="
33
- } as const satisfies Record<Comparator, Comparator>
34
-
35
- export type InvertedComparators = typeof invertedComparators
36
-
37
- export type OpenLeftBound = { limit: LimitLiteral; comparator: MinComparator }
38
-
39
- export const writeUnmatchedGroupCloseMessage = <unscanned extends string>(
40
- unscanned: unscanned
41
- ): writeUnmatchedGroupCloseMessage<unscanned> =>
42
- `Unmatched )${(unscanned === "" ? "" : ` before ${unscanned}`) as any}`
43
-
44
- export type writeUnmatchedGroupCloseMessage<unscanned extends string> =
45
- `Unmatched )${unscanned extends "" ? "" : ` before ${unscanned}`}`
46
-
47
- export const writeUnclosedGroupMessage = <missingChar extends string>(
48
- missingChar: missingChar
49
- ): writeUnclosedGroupMessage<missingChar> => `Missing ${missingChar}`
50
-
51
- export type writeUnclosedGroupMessage<missingChar extends string> =
52
- `Missing ${missingChar}`
53
-
54
- export const writeOpenRangeMessage = <
55
- min extends LimitLiteral,
56
- comparator extends MinComparator
57
- >(
58
- min: min,
59
- comparator: comparator
60
- ): writeOpenRangeMessage<min, comparator> =>
61
- `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
62
-
63
- export type writeOpenRangeMessage<
64
- min extends LimitLiteral,
65
- comparator extends MinComparator
66
- > = `Left bounds are only valid when paired with right bounds (try ...${comparator}${min})`
67
-
68
- export type writeUnpairableComparatorMessage<comparator extends Comparator> =
69
- `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
70
-
71
- export const writeUnpairableComparatorMessage = <comparator extends Comparator>(
72
- comparator: comparator
73
- ): writeUnpairableComparatorMessage<comparator> =>
74
- `Left-bounded expressions must specify their limits using < or <= (was ${comparator})`
75
-
76
- export const writeMultipleLeftBoundsMessage = <
77
- openLimit extends LimitLiteral,
78
- openComparator extends MinComparator,
79
- limit extends LimitLiteral,
80
- comparator extends MinComparator
81
- >(
82
- openLimit: openLimit,
83
- openComparator: openComparator,
84
- limit: limit,
85
- comparator: comparator
86
- ): writeMultipleLeftBoundsMessage<
87
- openLimit,
88
- openComparator,
89
- limit,
90
- comparator
91
- > =>
92
- `An expression may have at most one left bound (parsed ${openLimit}${invertedComparators[openComparator]}, ${limit}${invertedComparators[comparator]})`
93
-
94
- export type writeMultipleLeftBoundsMessage<
95
- openLimit extends LimitLiteral,
96
- openComparator extends MinComparator,
97
- limit extends LimitLiteral,
98
- comparator extends MinComparator
99
- > = `An expression may have at most one left bound (parsed ${openLimit}${InvertedComparators[openComparator]}, ${limit}${InvertedComparators[comparator]})`
@@ -1,296 +0,0 @@
1
- import type { LimitLiteral } from "@arktype/schema"
2
- import type { Completion, ErrorMessage, defined } from "@arktype/util"
3
- import type { Scanner } from "../shift/scanner.js"
4
- import type {
5
- Comparator,
6
- InvertedComparators,
7
- MaxComparator,
8
- MinComparator,
9
- OpenLeftBound,
10
- StringifiablePrefixOperator,
11
- writeMultipleLeftBoundsMessage,
12
- writeOpenRangeMessage,
13
- writeUnclosedGroupMessage,
14
- writeUnmatchedGroupCloseMessage,
15
- writeUnpairableComparatorMessage
16
- } from "./shared.js"
17
-
18
- export type StaticState = {
19
- root: unknown
20
- branches: BranchState
21
- groups: BranchState[]
22
- finalizer: Scanner.FinalizingLookahead | ErrorMessage | undefined
23
- scanned: string
24
- unscanned: string
25
- }
26
-
27
- type BranchState = {
28
- prefixes: StringifiablePrefixOperator[]
29
- leftBound: OpenLeftBound | undefined
30
- intersection: unknown
31
- union: unknown
32
- }
33
-
34
- export type AutocompletePrefix = `${StringifiablePrefixOperator} `
35
-
36
- export namespace state {
37
- export type initialize<def extends string> = from<{
38
- root: undefined
39
- branches: initialBranches
40
- groups: []
41
- finalizer: undefined
42
- scanned: ""
43
- unscanned: def
44
- }>
45
-
46
- export type error<message extends string> = from<{
47
- root: undefined
48
- branches: initialBranches
49
- groups: []
50
- finalizer: ErrorMessage<message>
51
- scanned: ""
52
- unscanned: ""
53
- }>
54
-
55
- export type completion<text extends string> = from<{
56
- root: undefined
57
- branches: initialBranches
58
- groups: []
59
- finalizer: Completion<text>
60
- scanned: ""
61
- unscanned: ""
62
- }>
63
-
64
- type initialBranches = branchesFrom<{
65
- prefixes: []
66
- leftBound: undefined
67
- intersection: undefined
68
- union: undefined
69
- }>
70
-
71
- type updateScanned<
72
- previousScanned extends string,
73
- previousUnscanned extends string,
74
- updatedUnscanned extends string
75
- > =
76
- previousUnscanned extends `${infer justScanned}${updatedUnscanned}` ?
77
- `${previousScanned}${justScanned}`
78
- : previousScanned
79
-
80
- export type setRoot<
81
- s extends StaticState,
82
- root,
83
- unscanned extends string
84
- > = from<{
85
- root: root
86
- branches: s["branches"]
87
- groups: s["groups"]
88
- finalizer: s["finalizer"]
89
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
90
- unscanned: unscanned
91
- }>
92
-
93
- export type addPrefix<
94
- s extends StaticState,
95
- prefix extends StringifiablePrefixOperator,
96
- unscanned extends string
97
- > = from<{
98
- root: s["root"]
99
- branches: {
100
- prefixes: [...s["branches"]["prefixes"], prefix]
101
- leftBound: s["branches"]["leftBound"]
102
- intersection: s["branches"]["intersection"]
103
- union: s["branches"]["union"]
104
- }
105
- groups: s["groups"]
106
- finalizer: s["finalizer"]
107
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
108
- unscanned: unscanned
109
- }>
110
-
111
- export type reduceBranch<
112
- s extends StaticState,
113
- token extends "|" | "&",
114
- unscanned extends string
115
- > =
116
- s["branches"]["leftBound"] extends {} ?
117
- openRangeError<s["branches"]["leftBound"]>
118
- : from<{
119
- root: undefined
120
- branches: {
121
- prefixes: []
122
- leftBound: undefined
123
- intersection: token extends "&" ? mergeToIntersection<s> : undefined
124
- union: token extends "|" ? mergeToUnion<s> : s["branches"]["union"]
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
- > =
138
- comparator extends "<" | "<=" ?
139
- s["branches"]["leftBound"] extends {} ?
140
- state.error<
141
- writeMultipleLeftBoundsMessage<
142
- s["branches"]["leftBound"]["limit"],
143
- s["branches"]["leftBound"]["comparator"],
144
- limit,
145
- InvertedComparators[comparator]
146
- >
147
- >
148
- : from<{
149
- root: undefined
150
- branches: {
151
- prefixes: s["branches"]["prefixes"]
152
- leftBound: {
153
- limit: limit
154
- comparator: InvertedComparators[comparator]
155
- }
156
- intersection: s["branches"]["intersection"]
157
- union: s["branches"]["union"]
158
- }
159
- groups: s["groups"]
160
- finalizer: s["finalizer"]
161
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
162
- unscanned: unscanned
163
- }>
164
- : state.error<writeUnpairableComparatorMessage<comparator>>
165
-
166
- export type reduceRange<
167
- s extends StaticState,
168
- minLimit extends LimitLiteral,
169
- minComparator extends MinComparator,
170
- maxComparator extends MaxComparator,
171
- maxLimit extends LimitLiteral,
172
- unscanned extends string
173
- > = state.from<{
174
- root: [minLimit, minComparator, [s["root"], maxComparator, maxLimit]]
175
- branches: {
176
- prefixes: s["branches"]["prefixes"]
177
- leftBound: undefined
178
- intersection: s["branches"]["intersection"]
179
- union: s["branches"]["union"]
180
- }
181
- groups: s["groups"]
182
- finalizer: s["finalizer"]
183
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
184
- unscanned: unscanned
185
- }>
186
-
187
- export type reduceSingleBound<
188
- s extends StaticState,
189
- comparator extends Comparator,
190
- limit extends number | string,
191
- unscanned extends string
192
- > = state.from<{
193
- root: [s["root"], comparator, limit]
194
- branches: {
195
- prefixes: s["branches"]["prefixes"]
196
- leftBound: undefined
197
- intersection: s["branches"]["intersection"]
198
- union: s["branches"]["union"]
199
- }
200
- groups: s["groups"]
201
- finalizer: s["finalizer"]
202
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
203
- unscanned: unscanned
204
- }>
205
-
206
- type mergeToUnion<s extends StaticState> =
207
- s["branches"]["union"] extends undefined ? mergeToIntersection<s>
208
- : [s["branches"]["union"], "|", mergeToIntersection<s>]
209
-
210
- type mergeToIntersection<s extends StaticState> =
211
- s["branches"]["intersection"] extends undefined ? mergePrefixes<s>
212
- : [s["branches"]["intersection"], "&", mergePrefixes<s>]
213
-
214
- type mergePrefixes<
215
- s extends StaticState,
216
- remaining extends unknown[] = s["branches"]["prefixes"]
217
- > =
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<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
- groups: stack
233
- branches: top
234
- root: mergeToUnion<s>
235
- finalizer: s["finalizer"]
236
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
237
- unscanned: unscanned
238
- }>
239
- : state.error<writeUnmatchedGroupCloseMessage<unscanned>>
240
-
241
- export type reduceGroupOpen<
242
- s extends StaticState,
243
- unscanned extends string
244
- > = from<{
245
- groups: [...s["groups"], s["branches"]]
246
- branches: initialBranches
247
- root: undefined
248
- finalizer: s["finalizer"]
249
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
250
- unscanned: unscanned
251
- }>
252
-
253
- export type finalize<
254
- s extends StaticState,
255
- finalizer extends Scanner.FinalizingLookahead
256
- > =
257
- s["groups"] extends [] ?
258
- s["branches"]["leftBound"] extends {} ?
259
- openRangeError<s["branches"]["leftBound"]>
260
- : from<{
261
- root: mergeToUnion<s>
262
- groups: s["groups"]
263
- branches: initialBranches
264
- finalizer: finalizer
265
- scanned: s["scanned"]
266
- unscanned: s["unscanned"]
267
- }>
268
- : state.error<writeUnclosedGroupMessage<")">>
269
-
270
- type openRangeError<range extends defined<BranchState["leftBound"]>> =
271
- state.error<writeOpenRangeMessage<range["limit"], range["comparator"]>>
272
-
273
- export type previousOperator<s extends StaticState> =
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
283
-
284
- export type scanTo<s extends StaticState, unscanned extends string> = from<{
285
- root: s["root"]
286
- branches: s["branches"]
287
- groups: s["groups"]
288
- finalizer: s["finalizer"]
289
- scanned: updateScanned<s["scanned"], s["unscanned"], unscanned>
290
- unscanned: unscanned
291
- }>
292
-
293
- export type from<s extends StaticState> = s
294
-
295
- export type branchesFrom<b extends BranchState> = b
296
- }
@@ -1,55 +0,0 @@
1
- import type { DateLiteral } from "@arktype/schema"
2
- import { throwParseError, tryParseNumber } from "@arktype/util"
3
-
4
- export const isDateLiteral = (value: unknown): value is DateLiteral =>
5
- typeof value === "string" &&
6
- value[0] === "d" &&
7
- (value[1] === "'" || value[1] === '"') &&
8
- value.at(-1) === value[1]
9
-
10
- export const isValidDate = (d: Date): boolean => d.toString() !== "Invalid Date"
11
-
12
- export const extractDateLiteralSource = <literal extends DateLiteral>(
13
- literal: literal
14
- ): extractDateLiteralSource<literal> => literal.slice(2, -1) as never
15
-
16
- type extractDateLiteralSource<literal extends DateLiteral> =
17
- literal extends DateLiteral<infer source> ? source : never
18
-
19
- export const writeInvalidDateMessage = <source extends string>(
20
- source: source
21
- ): writeInvalidDateMessage<source> =>
22
- `'${source}' could not be parsed by the Date constructor`
23
-
24
- export type writeInvalidDateMessage<source extends string> =
25
- `'${source}' could not be parsed by the Date constructor`
26
-
27
- export type DateInput = ConstructorParameters<typeof Date>[0]
28
-
29
- export type DateParseResult<
30
- errorOnFail extends boolean | string = boolean | string
31
- > = Date | (errorOnFail extends true | string ? never : undefined)
32
-
33
- export const tryParseDate = <errorOnFail extends boolean | string>(
34
- source: string,
35
- errorOnFail?: errorOnFail
36
- ): DateParseResult<errorOnFail> => maybeParseDate(source, errorOnFail)
37
-
38
- const maybeParseDate = <errorOnFail extends boolean | string>(
39
- source: string,
40
- errorOnFail?: errorOnFail
41
- ): DateParseResult<errorOnFail> => {
42
- const stringParsedDate = new Date(source)
43
- if (isValidDate(stringParsedDate)) return stringParsedDate
44
-
45
- const epochMillis = tryParseNumber(source)
46
- if (epochMillis !== undefined) {
47
- const numberParsedDate = new Date(epochMillis)
48
- if (isValidDate(numberParsedDate)) return numberParsedDate
49
- }
50
- return errorOnFail ?
51
- throwParseError(
52
- errorOnFail === true ? writeInvalidDateMessage(source) : errorOnFail
53
- )
54
- : (undefined as never)
55
- }