arktype 2.0.0-rc.3 → 2.0.0-rc.30

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 (176) hide show
  1. package/out/attributes.d.ts +94 -0
  2. package/out/attributes.js +3 -0
  3. package/out/config.d.ts +1 -1
  4. package/out/generic.d.ts +55 -58
  5. package/out/generic.js +6 -8
  6. package/out/index.d.ts +6 -4
  7. package/out/index.js +4 -3
  8. package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +3 -3
  9. package/out/keywords/{constructors/Array.js → Array.js} +7 -7
  10. package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +3 -3
  11. package/out/keywords/{constructors/FormData.js → FormData.js} +8 -8
  12. package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
  13. package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
  14. package/out/keywords/builtins.js +2 -2
  15. package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -2
  16. package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
  17. package/out/keywords/keywords.d.ts +61 -0
  18. package/out/keywords/keywords.js +29 -0
  19. package/out/keywords/number.d.ts +23 -0
  20. package/out/keywords/{number/epoch.js → number.js} +24 -2
  21. package/out/keywords/string.d.ts +327 -0
  22. package/out/keywords/string.js +398 -0
  23. package/out/keywords/ts.d.ts +42 -4
  24. package/out/keywords/ts.js +24 -7
  25. package/out/methods/array.d.ts +12 -0
  26. package/out/methods/base.d.ts +178 -0
  27. package/out/methods/date.d.ts +10 -0
  28. package/out/methods/instantiate.d.ts +17 -0
  29. package/out/methods/morph.d.ts +12 -0
  30. package/out/methods/number.d.ts +11 -0
  31. package/out/methods/object.d.ts +106 -0
  32. package/out/methods/string.d.ts +12 -0
  33. package/out/module.d.ts +2 -2
  34. package/out/parser/{semantic → ast}/bounds.d.ts +3 -3
  35. package/out/parser/ast/default.d.ts +8 -0
  36. package/out/parser/ast/generic.d.ts +17 -0
  37. package/out/parser/ast/infer.d.ts +25 -0
  38. package/out/parser/{semantic → ast}/utils.d.ts +1 -1
  39. package/out/parser/ast/validate.d.ts +30 -0
  40. package/out/parser/ast/validate.js +3 -0
  41. package/out/parser/definition.d.ts +21 -13
  42. package/out/parser/definition.js +20 -2
  43. package/out/parser/objectLiteral.d.ts +41 -67
  44. package/out/parser/objectLiteral.js +79 -71
  45. package/out/parser/property.d.ts +36 -0
  46. package/out/parser/property.js +16 -0
  47. package/out/parser/{string/reduce → reduce}/dynamic.d.ts +10 -13
  48. package/out/parser/{string/reduce → reduce}/dynamic.js +5 -6
  49. package/out/parser/{string/reduce → reduce}/shared.d.ts +1 -1
  50. package/out/parser/{string/reduce → reduce}/static.d.ts +4 -4
  51. package/out/parser/{string/shift → shift}/operand/date.d.ts +3 -1
  52. package/out/parser/{string/shift → shift}/operand/enclosed.d.ts +4 -4
  53. package/out/parser/{string/shift → shift}/operand/enclosed.js +7 -3
  54. package/out/parser/shift/operand/operand.d.ts +9 -0
  55. package/out/parser/{string/shift → shift}/operand/operand.js +2 -2
  56. package/out/parser/{string/shift → shift}/operand/unenclosed.d.ts +15 -7
  57. package/out/parser/{string/shift → shift}/operand/unenclosed.js +12 -7
  58. package/out/parser/{string/shift → shift}/operator/bounds.d.ts +7 -7
  59. package/out/parser/{string/shift → shift}/operator/bounds.js +5 -4
  60. package/out/parser/shift/operator/brand.d.ts +6 -0
  61. package/out/parser/shift/operator/brand.js +5 -0
  62. package/out/parser/{string/shift → shift}/operator/default.d.ts +4 -6
  63. package/out/parser/{string/shift → shift}/operator/default.js +4 -6
  64. package/out/parser/{string/shift → shift}/operator/divisor.d.ts +2 -2
  65. package/out/parser/shift/operator/operator.d.ts +14 -0
  66. package/out/parser/{string/shift → shift}/operator/operator.js +7 -5
  67. package/out/parser/shift/scanner.d.ts +47 -0
  68. package/out/parser/shift/scanner.js +43 -0
  69. package/out/parser/{string/string.d.ts → string.d.ts} +9 -9
  70. package/out/parser/{string/string.js → string.js} +19 -0
  71. package/out/parser/tupleExpressions.d.ts +47 -0
  72. package/out/parser/tupleExpressions.js +67 -0
  73. package/out/parser/tupleLiteral.d.ts +95 -0
  74. package/out/parser/tupleLiteral.js +122 -0
  75. package/out/scope.d.ts +41 -34
  76. package/out/scope.js +54 -62
  77. package/out/type.d.ts +46 -22
  78. package/out/type.js +15 -11
  79. package/package.json +3 -3
  80. package/out/intersect.d.ts +0 -28
  81. package/out/keys.d.ts +0 -16
  82. package/out/keywords/ark.d.ts +0 -39
  83. package/out/keywords/ark.js +0 -24
  84. package/out/keywords/ast.d.ts +0 -193
  85. package/out/keywords/ast.js +0 -2
  86. package/out/keywords/number/epoch.d.ts +0 -10
  87. package/out/keywords/number/integer.d.ts +0 -3
  88. package/out/keywords/number/integer.js +0 -5
  89. package/out/keywords/number/number.d.ts +0 -28
  90. package/out/keywords/number/number.js +0 -9
  91. package/out/keywords/string/alpha.d.ts +0 -7
  92. package/out/keywords/string/alpha.js +0 -2
  93. package/out/keywords/string/alphanumeric.d.ts +0 -7
  94. package/out/keywords/string/alphanumeric.js +0 -2
  95. package/out/keywords/string/capitalize.d.ts +0 -15
  96. package/out/keywords/string/capitalize.js +0 -12
  97. package/out/keywords/string/creditCard.d.ts +0 -8
  98. package/out/keywords/string/creditCard.js +0 -37
  99. package/out/keywords/string/date.d.ts +0 -59
  100. package/out/keywords/string/date.js +0 -98
  101. package/out/keywords/string/digits.d.ts +0 -7
  102. package/out/keywords/string/digits.js +0 -2
  103. package/out/keywords/string/email.d.ts +0 -7
  104. package/out/keywords/string/email.js +0 -4
  105. package/out/keywords/string/integer.d.ts +0 -16
  106. package/out/keywords/string/integer.js +0 -16
  107. package/out/keywords/string/ip.d.ts +0 -20
  108. package/out/keywords/string/ip.js +0 -23
  109. package/out/keywords/string/json.d.ts +0 -15
  110. package/out/keywords/string/json.js +0 -45
  111. package/out/keywords/string/lower.d.ts +0 -15
  112. package/out/keywords/string/lower.js +0 -12
  113. package/out/keywords/string/normalize.d.ts +0 -92
  114. package/out/keywords/string/normalize.js +0 -43
  115. package/out/keywords/string/numeric.d.ts +0 -15
  116. package/out/keywords/string/numeric.js +0 -13
  117. package/out/keywords/string/semver.d.ts +0 -7
  118. package/out/keywords/string/semver.js +0 -4
  119. package/out/keywords/string/string.d.ts +0 -112
  120. package/out/keywords/string/string.js +0 -41
  121. package/out/keywords/string/trim.d.ts +0 -15
  122. package/out/keywords/string/trim.js +0 -14
  123. package/out/keywords/string/upper.d.ts +0 -15
  124. package/out/keywords/string/upper.js +0 -12
  125. package/out/keywords/string/url.d.ts +0 -15
  126. package/out/keywords/string/url.js +0 -37
  127. package/out/keywords/string/utils.d.ts +0 -2
  128. package/out/keywords/string/utils.js +0 -10
  129. package/out/keywords/string/uuid.d.ts +0 -42
  130. package/out/keywords/string/uuid.js +0 -19
  131. package/out/keywords/utils.d.ts +0 -1
  132. package/out/keywords/utils.js +0 -2
  133. package/out/parser/semantic/default.d.ts +0 -9
  134. package/out/parser/semantic/default.js +0 -1
  135. package/out/parser/semantic/infer.d.ts +0 -39
  136. package/out/parser/semantic/validate.d.ts +0 -26
  137. package/out/parser/semantic/validate.js +0 -1
  138. package/out/parser/string/shift/operand/operand.d.ts +0 -9
  139. package/out/parser/string/shift/operator/operator.d.ts +0 -13
  140. package/out/parser/string/shift/scanner.d.ts +0 -69
  141. package/out/parser/string/shift/scanner.js +0 -100
  142. package/out/parser/tuple.d.ts +0 -132
  143. package/out/parser/tuple.js +0 -158
  144. package/out/subtypes/array.d.ts +0 -13
  145. package/out/subtypes/base.d.ts +0 -85
  146. package/out/subtypes/date.d.ts +0 -11
  147. package/out/subtypes/instantiate.d.ts +0 -16
  148. package/out/subtypes/morph.d.ts +0 -13
  149. package/out/subtypes/number.d.ts +0 -12
  150. package/out/subtypes/object.d.ts +0 -33
  151. package/out/subtypes/string.d.ts +0 -13
  152. package/out/subtypes/validator.d.ts +0 -11
  153. package/out/subtypes/validator.js +0 -1
  154. /package/out/{subtypes → methods}/array.js +0 -0
  155. /package/out/{subtypes → methods}/base.js +0 -0
  156. /package/out/{subtypes → methods}/date.js +0 -0
  157. /package/out/{subtypes → methods}/instantiate.js +0 -0
  158. /package/out/{subtypes → methods}/morph.js +0 -0
  159. /package/out/{subtypes → methods}/number.js +0 -0
  160. /package/out/{subtypes → methods}/object.js +0 -0
  161. /package/out/{subtypes → methods}/string.js +0 -0
  162. /package/out/parser/{semantic → ast}/bounds.js +0 -0
  163. /package/out/{intersect.js → parser/ast/default.js} +0 -0
  164. /package/out/parser/{semantic → ast}/divisor.d.ts +0 -0
  165. /package/out/parser/{semantic → ast}/divisor.js +0 -0
  166. /package/out/{keys.js → parser/ast/generic.js} +0 -0
  167. /package/out/parser/{semantic → ast}/infer.js +0 -0
  168. /package/out/parser/{semantic → ast}/keyof.d.ts +0 -0
  169. /package/out/parser/{semantic → ast}/keyof.js +0 -0
  170. /package/out/parser/{semantic → ast}/utils.js +0 -0
  171. /package/out/parser/{string/reduce → reduce}/shared.js +0 -0
  172. /package/out/parser/{string/reduce → reduce}/static.js +0 -0
  173. /package/out/parser/{string/shift → shift}/operand/date.js +0 -0
  174. /package/out/parser/{string/shift → shift}/operand/genericArgs.d.ts +0 -0
  175. /package/out/parser/{string/shift → shift}/operand/genericArgs.js +0 -0
  176. /package/out/parser/{string/shift → shift}/operator/divisor.js +0 -0
@@ -1,39 +0,0 @@
1
- import type { ArkErrors, arkKind } from "@ark/schema";
2
- import type { inferred } from "@ark/util";
3
- import type { GenericParser } from "../generic.ts";
4
- import type { BoundModule, Module } from "../module.ts";
5
- import { type Scope } from "../scope.ts";
6
- import type { DeclarationParser, DefinitionParser, TypeParser } from "../type.ts";
7
- import { arkBuiltins } from "./builtins.ts";
8
- import { arkPrototypes } from "./constructors/constructors.ts";
9
- import { number } from "./number/number.ts";
10
- import { string } from "./string/string.ts";
11
- import { arkTsGenerics, arkTsKeywords } from "./ts.ts";
12
- export interface Ark extends Omit<Ark.keywords, keyof Ark.wrapped>, Ark.wrapped {
13
- }
14
- export declare namespace Ark {
15
- interface keywords extends arkTsKeywords.$, arkTsGenerics.$, arkPrototypes.keywords, arkBuiltins.$ {
16
- }
17
- interface wrapped extends arkPrototypes.wrapped {
18
- string: string.submodule;
19
- number: number.submodule;
20
- }
21
- interface typeAttachments extends arkTsKeywords.$ {
22
- Key: arkBuiltins.$["Key"];
23
- Record: arkTsGenerics.$["Record"];
24
- }
25
- interface boundTypeAttachments<$> extends Omit<BoundModule<typeAttachments, $>, arkKind> {
26
- }
27
- }
28
- export declare const ambient: Scope<Ark>;
29
- export declare const ark: Module<Ark>;
30
- export declare const type: TypeParser<{}>;
31
- export declare namespace type {
32
- type cast<t> = {
33
- [inferred]?: t;
34
- };
35
- type errors = ArkErrors;
36
- }
37
- export declare const generic: GenericParser<{}>;
38
- export declare const define: DefinitionParser<{}>;
39
- export declare const declare: DeclarationParser<{}>;
@@ -1,24 +0,0 @@
1
- import { $arkTypeRegistry, scope } from "../scope.js";
2
- import { arkBuiltins } from "./builtins.js";
3
- import { arkPrototypes } from "./constructors/constructors.js";
4
- import { number } from "./number/number.js";
5
- import { string } from "./string/string.js";
6
- import { arkTsGenerics, arkTsKeywords } from "./ts.js";
7
- $arkTypeRegistry.typeAttachments = {
8
- ...arkTsKeywords,
9
- Key: arkBuiltins.Key,
10
- Record: arkTsGenerics.Record
11
- };
12
- export const ambient = scope({
13
- ...arkTsKeywords,
14
- ...arkTsGenerics,
15
- ...arkPrototypes,
16
- ...arkBuiltins,
17
- string,
18
- number
19
- }, { prereducedAliases: true, ambient: true });
20
- export const ark = ambient.export();
21
- export const type = ambient.type;
22
- export const generic = ambient.generic;
23
- export const define = ambient.define;
24
- export const declare = ambient.declare;
@@ -1,193 +0,0 @@
1
- import type { ArkError, ArkErrors, Constraint, constraintKindOf, Morph, NodeSchema } from "@ark/schema";
2
- import { type anyOrNever, type array, type conform, type equals, type leftIfEqual, type Primitive, type show } from "@ark/util";
3
- import type { inferPipe } from "../intersect.ts";
4
- import type { Type } from "../type.ts";
5
- import type { type } from "./ark.ts";
6
- import type { arkPrototypes } from "./constructors/constructors.ts";
7
- import type { number } from "./number/number.ts";
8
- import type { string } from "./string/string.ts";
9
- export type { arkPrototypes as object } from "./constructors/constructors.ts";
10
- export type { number } from "./number/number.ts";
11
- export type { string } from "./string/string.ts";
12
- export type Comparator = "<" | "<=" | ">" | ">=" | "==";
13
- export type RegexLiteral<source extends string = string> = `/${source}/`;
14
- export type DateLiteral<source extends string = string> = `d"${source}"` | `d'${source}'`;
15
- export type ConstraintSet = Record<PropertyKey, 1>;
16
- export type Constraints = Record<string, ConstraintSet>;
17
- export declare const constrained: " arkConstrained";
18
- export type constrained = typeof constrained;
19
- export type constrain<base, constraints extends Constraints> = base & {
20
- [constrained]: constraints;
21
- };
22
- export type LimitLiteral = number | DateLiteral;
23
- export type normalizeLimit<limit> = limit extends DateLiteral<infer source> ? source : limit extends number | string ? limit : never;
24
- type constraint<rule> = {
25
- [k in rule & PropertyKey]: 1;
26
- };
27
- export type AtLeast<rule> = {
28
- atLeast: constraint<rule>;
29
- };
30
- export type AtMost<rule> = {
31
- atMost: constraint<rule>;
32
- };
33
- export type MoreThan<rule> = {
34
- moreThan: constraint<rule>;
35
- };
36
- export type LessThan<rule> = {
37
- lessThan: constraint<rule>;
38
- };
39
- export type Literal<rule> = {
40
- literal: constraint<rule>;
41
- };
42
- export type DivisibleBy<rule> = {
43
- divisibleBy: constraint<rule>;
44
- };
45
- export type Length<rule> = {
46
- length: constraint<rule>;
47
- };
48
- export type Matching<rule> = {
49
- matching: constraint<rule>;
50
- };
51
- export type Narrowed = {
52
- predicate: {
53
- [k in "?"]: 1;
54
- };
55
- };
56
- export type Branded<rule> = {
57
- predicate: constraint<rule>;
58
- };
59
- export type primitiveConstraintKindOf<In> = Extract<Constraint.PrimitiveKind, constraintKindOf<In>>;
60
- export type AtLeastLength<rule> = {
61
- atLeastLength: constraint<rule>;
62
- };
63
- export type AtMostLength<rule> = {
64
- atMostLength: constraint<rule>;
65
- };
66
- export type MoreThanLength<rule> = {
67
- moreThanLength: constraint<rule>;
68
- };
69
- export type LessThanLength<rule> = {
70
- lessThanLength: constraint<rule>;
71
- };
72
- export type ExactlyLength<rule> = {
73
- atLeastLength: constraint<rule>;
74
- atMostLength: constraint<rule>;
75
- };
76
- export type AtOrAfter<rule> = {
77
- atOrAfter: constraint<rule>;
78
- };
79
- export type AtOrBefore<rule> = {
80
- atOrBefore: constraint<rule>;
81
- };
82
- export type After<rule> = {
83
- after: constraint<rule>;
84
- };
85
- export type Before<rule> = {
86
- before: constraint<rule>;
87
- };
88
- export declare namespace Date {
89
- type atOrAfter<rule> = constrain<Date, AtOrAfter<rule>>;
90
- type after<rule> = constrain<Date, After<rule>>;
91
- type atOrBefore<rule> = constrain<Date, AtOrBefore<rule>>;
92
- type before<rule> = constrain<Date, Before<rule>>;
93
- type narrowed = constrain<Date, Narrowed>;
94
- type branded<rule> = constrain<Date, Branded<rule>>;
95
- type literal<rule> = constrain<Date, Literal<rule>>;
96
- type is<constraints extends Constraints> = constrain<Date, constraints>;
97
- type parseConstraint<kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = normalizePrimitiveConstraintRoot<schema> extends infer rule ? kind extends "after" ? schema extends {
98
- exclusive: true;
99
- } ? after<normalizeLimit<rule>> : atOrAfter<normalizeLimit<rule>> : kind extends "before" ? schema extends {
100
- exclusive: true;
101
- } ? before<normalizeLimit<rule>> : atOrBefore<normalizeLimit<rule>> : narrowed : never;
102
- }
103
- export type applyConstraint<t, kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = t extends MorphAst<infer i, infer o> ? (In: leftIfEqual<i, _applyConstraint<i, kind, schema>>) => o : leftIfEqual<t, _applyConstraint<t, kind, schema>>;
104
- type _applyConstraint<t, kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = schemaToConstraint<kind, schema> extends infer constraint ? parseConstraints<t> extends ([
105
- infer base,
106
- infer constraints extends Constraints
107
- ]) ? [
108
- number,
109
- base
110
- ] extends [base, number] ? number.is<constraint & constraints> : [string, base] extends [base, string] ? string.is<constraint & constraints> : [Date, base] extends [base, Date] ? Date.is<constraint & constraints> : constrain<base, constraints & constraint> : [number, t] extends [t, number] ? number.parseConstraint<kind, schema> : [string, t] extends [t, string] ? string.parseConstraint<kind, schema> : [Date, t] extends [t, Date] ? Date.parseConstraint<kind, schema> : constrain<t, conform<constraint, Constraints>> : never;
111
- export type parseConstraints<t> = t extends constrain<infer base, infer constraints> ? equals<t, number & {
112
- [constrained]: constraints;
113
- }> extends true ? [
114
- number,
115
- constraints
116
- ] : equals<t, string & {
117
- [constrained]: constraints;
118
- }> extends true ? [
119
- string,
120
- constraints
121
- ] : equals<t, Date & {
122
- [constrained]: constraints;
123
- }> extends true ? [
124
- Date,
125
- constraints
126
- ] : [base, constraints] : null;
127
- export type normalizePrimitiveConstraintRoot<schema extends NodeSchema<Constraint.PrimitiveKind>> = "rule" extends keyof schema ? conform<schema["rule"], PropertyKey> : conform<schema, PropertyKey>;
128
- export type schemaToConstraint<kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = normalizePrimitiveConstraintRoot<schema> extends infer rule ? kind extends "pattern" ? Matching<rule> : kind extends "divisor" ? DivisibleBy<rule> : kind extends "exactLength" ? Length<rule> : kind extends "min" ? schema extends {
129
- exclusive: true;
130
- } ? MoreThan<rule> : AtLeast<rule> : kind extends "max" ? schema extends {
131
- exclusive: true;
132
- } ? LessThan<rule> : AtMost<rule> : kind extends "minLength" ? schema extends {
133
- exclusive: true;
134
- } ? MoreThanLength<rule> : AtLeastLength<rule> : kind extends "maxLength" ? schema extends {
135
- exclusive: true;
136
- } ? LessThanLength<rule> : AtMostLength<rule> : kind extends "exactLength" ? ExactlyLength<rule> : kind extends "after" ? schema extends {
137
- exclusive: true;
138
- } ? After<normalizeLimit<rule>> : AtOrAfter<normalizeLimit<rule>> : kind extends "before" ? schema extends {
139
- exclusive: true;
140
- } ? Before<normalizeLimit<rule>> : AtOrBefore<normalizeLimit<rule>> : Narrowed : never;
141
- export type distillIn<t> = finalizeDistillation<t, _distill<t, "in", "base">>;
142
- export type distillOut<t> = finalizeDistillation<t, _distill<t, "out", "base">>;
143
- export type distillConstrainableIn<t> = finalizeDistillation<t, _distill<t, "in", "constrainable">>;
144
- export type distillConstrainableOut<t> = finalizeDistillation<t, _distill<t, "out", "constrainable">>;
145
- export type distillValidatedOut<t> = finalizeDistillation<t, _distill<t, "validatedOut", "constrainable">>;
146
- type finalizeDistillation<t, distilled> = equals<t, distilled> extends true ? t : distilled;
147
- export type includesMorphs<t> = [
148
- _distill<t, "in", "constrainable">,
149
- _distill<t, "out", "constrainable">
150
- ] extends ([
151
- _distill<t, "out", "constrainable">,
152
- _distill<t, "in", "constrainable">
153
- ]) ? false : true;
154
- type IoKind = "in" | "out" | "validatedOut";
155
- type DistilledKind = "base" | "constrainable";
156
- type _distill<t, io extends IoKind, distilledKind extends DistilledKind> = t extends undefined ? t : [t] extends [anyOrNever] ? t : parseConstraints<t> extends ([
157
- infer base,
158
- infer constraints extends Constraints
159
- ]) ? distilledKind extends "base" ? _distill<base, io, distilledKind> : constrain<_distill<base, io, distilledKind>, constraints> : t extends TerminallyInferredObjectKind | Primitive ? t : unknown extends t ? unknown : t extends MorphAst<infer i, infer o> ? io extends "in" ? _distill<i, io, distilledKind> : io extends "validatedOut" ? o extends To<infer validatedOut> ? _distill<validatedOut, io, distilledKind> : unknown : _distill<o[1], io, distilledKind> : t extends DefaultableAst<infer t> ? _distill<t, io, distilledKind> : t extends array ? distillArray<t, io, distilledKind, []> : t extends Function ? t : {
160
- [k in keyof t]: t[k];
161
- } extends t ? io extends "in" ? show<{
162
- [k in keyof t as k extends defaultableKeyOf<t> ? never : k]: _distill<t[k], io, distilledKind>;
163
- } & {
164
- [k in defaultableKeyOf<t>]?: _distill<t[k], io, distilledKind>;
165
- }> : {
166
- [k in keyof t]: _distill<t[k], io, distilledKind>;
167
- } : t;
168
- export type defaultableKeyOf<t> = {
169
- [k in keyof t]: [t[k]] extends [anyOrNever] ? never : t[k] extends DefaultableAst ? k : never;
170
- }[keyof t];
171
- type distillArray<t extends array, io extends IoKind, constraints extends DistilledKind, prefix extends array> = _distillArray<t, io, constraints, prefix> extends infer result ? t extends unknown[] ? result : Readonly<t> : never;
172
- type _distillArray<t extends array, io extends IoKind, constraints extends DistilledKind, prefix extends array> = t extends readonly [infer head, ...infer tail] ? _distillArray<tail, io, constraints, [
173
- ...prefix,
174
- _distill<head, io, constraints>
175
- ]> : [...prefix, ...distillPostfix<t, io, constraints>];
176
- type distillPostfix<t extends array, io extends IoKind, constraints extends DistilledKind, postfix extends array = []> = t extends readonly [...infer init, infer last] ? distillPostfix<init, io, constraints, [
177
- _distill<last, io, constraints>,
178
- ...postfix
179
- ]> : [...{
180
- [i in keyof t]: _distill<t[i], io, constraints>;
181
- }, ...postfix];
182
- /** Objects we don't want to expand during inference like Date or Promise */
183
- type TerminallyInferredObjectKind = arkPrototypes.instanceOfExcluding<"Array" | "Function"> | ArkEnv.prototypes;
184
- export type inferPredicate<t, predicate> = predicate extends (data: any, ...args: any[]) => data is infer narrowed ? t extends constrain<unknown, infer constraints> ? applyConstraint<constrain<narrowed, constraints>, "predicate", any> : applyConstraint<narrowed, "predicate", any> : applyConstraint<t, "predicate", any>;
185
- export type constrainWithPredicate<t> = t extends constrain<unknown, infer constraints> ? applyConstraint<constrain<t, constraints>, "predicate", any> : applyConstraint<t, "predicate", any>;
186
- export type inferPipes<t, pipes extends Morph[]> = pipes extends [infer head extends Morph, ...infer tail extends Morph[]] ? inferPipes<pipes[0] extends type.cast<infer tPipe> ? inferPipe<t, tPipe> : inferMorphOut<head> extends infer out ? (In: distillConstrainableIn<t>) => Out<out> : never, tail> : t;
187
- export type inferMorphOut<morph extends Morph> = Exclude<ReturnType<morph>, ArkError | ArkErrors>;
188
- export type Out<o = any> = ["=>", o, boolean];
189
- export type To<o = any> = ["=>", o, true];
190
- export type MorphAst<i = any, o extends Out = Out> = (In: i) => o;
191
- export type Default<v = any> = ["=", v];
192
- export type DefaultableAst<t = any, v = any> = (In?: t) => Default<v>;
193
- export type termOrType<t> = t | Type<t, any>;
@@ -1,2 +0,0 @@
1
- import { noSuggest } from "@ark/util";
2
- export const constrained = noSuggest("arkConstrained");
@@ -1,10 +0,0 @@
1
- import type { AtLeast, AtMost, DivisibleBy } from "../ast.ts";
2
- import type { number } from "./number.ts";
3
- /**
4
- * As per the ECMA-262 specification:
5
- * A time value supports a slightly smaller range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds.
6
- *
7
- * @see https://262.ecma-international.org/15.0/index.html#sec-time-values-and-time-range
8
- */
9
- export declare const epoch: import("@ark/schema").BaseRoot<import("@ark/schema").InternalRootDeclaration>;
10
- export type epoch = number.is<DivisibleBy<1> & AtMost<8640000000000000> & AtLeast<-8640000000000000>>;
@@ -1,3 +0,0 @@
1
- import type { number } from "./number.ts";
2
- export declare const integer: import("@ark/schema").BaseRoot<import("@ark/schema").InternalRootDeclaration>;
3
- export type integer = number.divisibleBy<1>;
@@ -1,5 +0,0 @@
1
- import { rootNode } from "@ark/schema";
2
- export const integer = rootNode({
3
- domain: "number",
4
- divisor: 1
5
- });
@@ -1,28 +0,0 @@
1
- import { type Constraint, type NodeSchema } from "@ark/schema";
2
- import type { Module, Submodule } from "../../module.ts";
3
- import type { AtLeast, AtMost, Branded, constrain, Constraints, DivisibleBy, LessThan, MoreThan, Narrowed, normalizePrimitiveConstraintRoot } from "../ast.ts";
4
- import { epoch } from "./epoch.ts";
5
- import { integer } from "./integer.ts";
6
- export declare const number: number.module;
7
- export declare namespace number {
8
- type atLeast<rule> = constrain<number, AtLeast<rule>>;
9
- type moreThan<rule> = constrain<number, MoreThan<rule>>;
10
- type atMost<rule> = constrain<number, AtMost<rule>>;
11
- type lessThan<rule> = constrain<number, LessThan<rule>>;
12
- type divisibleBy<rule> = constrain<number, DivisibleBy<rule>>;
13
- type narrowed = constrain<number, Narrowed>;
14
- type branded<rule> = constrain<number, Branded<rule>>;
15
- type is<constraints extends Constraints> = constrain<number, constraints>;
16
- type parseConstraint<kind extends Constraint.PrimitiveKind, schema extends NodeSchema<kind>> = normalizePrimitiveConstraintRoot<schema> extends infer rule ? kind extends "min" ? schema extends {
17
- exclusive: true;
18
- } ? moreThan<rule> : atLeast<rule> : kind extends "max" ? schema extends {
19
- exclusive: true;
20
- } ? lessThan<rule> : atMost<rule> : kind extends "divisor" ? divisibleBy<rule> : narrowed : never;
21
- type module = Module<submodule>;
22
- type submodule = Submodule<$>;
23
- type $ = {
24
- $root: number;
25
- epoch: epoch;
26
- integer: integer;
27
- };
28
- }
@@ -1,9 +0,0 @@
1
- import { intrinsic } from "@ark/schema";
2
- import { submodule } from "../utils.js";
3
- import { epoch } from "./epoch.js";
4
- import { integer } from "./integer.js";
5
- export const number = submodule({
6
- $root: intrinsic.number,
7
- integer,
8
- epoch
9
- });
@@ -1,7 +0,0 @@
1
- import type { Branded, constrain } from "../ast.ts";
2
- declare namespace string {
3
- type alpha = constrain<string, Branded<"alpha">>;
4
- }
5
- export declare const alpha: import("@ark/schema").IntersectionNode;
6
- export type alpha = string.alpha;
7
- export {};
@@ -1,2 +0,0 @@
1
- import { regexStringNode } from "./utils.js";
2
- export const alpha = regexStringNode(/^[A-Za-z]*$/, "only letters");
@@ -1,7 +0,0 @@
1
- import type { Branded, constrain } from "../ast.ts";
2
- declare namespace string {
3
- type alphanumeric = constrain<string, Branded<"alphanumeric">>;
4
- }
5
- export declare const alphanumeric: import("@ark/schema").IntersectionNode;
6
- export type alphanumeric = string.alphanumeric;
7
- export {};
@@ -1,2 +0,0 @@
1
- import { regexStringNode } from "./utils.js";
2
- export const alphanumeric = regexStringNode(/^[A-Za-z\d]*$/, "only letters and digits 0-9");
@@ -1,15 +0,0 @@
1
- import type { Module, Submodule } from "../../module.ts";
2
- import type { Branded, constrain, To } from "../ast.ts";
3
- declare namespace string {
4
- type capitalized = constrain<string, Branded<"capitalized">>;
5
- }
6
- export declare const capitalize: capitalize.module;
7
- export declare namespace capitalize {
8
- type module = Module<submodule>;
9
- type submodule = Submodule<$>;
10
- type $ = {
11
- $root: (In: string) => To<string.capitalized>;
12
- preformatted: string.capitalized;
13
- };
14
- }
15
- export {};
@@ -1,12 +0,0 @@
1
- import { rootNode } from "@ark/schema";
2
- import { submodule } from "../utils.js";
3
- import { regexStringNode } from "./utils.js";
4
- const preformatted = regexStringNode(/^[A-Z].*$/, "capitalized");
5
- export const capitalize = submodule({
6
- $root: rootNode({
7
- in: "string",
8
- morphs: (s) => s.charAt(0).toUpperCase() + s.slice(1),
9
- declaredOut: preformatted
10
- }),
11
- preformatted
12
- });
@@ -1,8 +0,0 @@
1
- import type { Branded, constrain } from "../ast.ts";
2
- export declare const isLuhnValid: (creditCardInput: string) => boolean;
3
- declare namespace string {
4
- type creditCard = constrain<string, Branded<"creditCard">>;
5
- }
6
- export declare const creditCard: import("@ark/schema").BaseRoot<import("@ark/schema").InternalRootDeclaration>;
7
- export type creditCard = string.creditCard;
8
- export {};
@@ -1,37 +0,0 @@
1
- import { rootNode } from "@ark/schema";
2
- // https://github.com/validatorjs/validator.js/blob/master/src/lib/isLuhnNumber.js
3
- export const isLuhnValid = (creditCardInput) => {
4
- const sanitized = creditCardInput.replace(/[- ]+/g, "");
5
- let sum = 0;
6
- let digit;
7
- let tmpNum;
8
- let shouldDouble = false;
9
- for (let i = sanitized.length - 1; i >= 0; i--) {
10
- digit = sanitized.substring(i, i + 1);
11
- tmpNum = Number.parseInt(digit, 10);
12
- if (shouldDouble) {
13
- tmpNum *= 2;
14
- if (tmpNum >= 10)
15
- sum += (tmpNum % 10) + 1;
16
- else
17
- sum += tmpNum;
18
- }
19
- else
20
- sum += tmpNum;
21
- shouldDouble = !shouldDouble;
22
- }
23
- return !!(sum % 10 === 0 ? sanitized : false);
24
- };
25
- // https://github.com/validatorjs/validator.js/blob/master/src/lib/isCreditCard.js
26
- const creditCardMatcher = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
27
- export const creditCard = rootNode({
28
- domain: "string",
29
- pattern: {
30
- meta: "a credit card number",
31
- rule: creditCardMatcher.source
32
- },
33
- predicate: {
34
- meta: "a credit card number",
35
- predicate: isLuhnValid
36
- }
37
- });
@@ -1,59 +0,0 @@
1
- import type { Module, Submodule } from "../../module.ts";
2
- import type { type } from "../ark.ts";
3
- import type { Branded, To, constrain } from "../ast.ts";
4
- type DayDelimiter = "." | "/" | "-";
5
- type DayPart = DayPatterns[PartKey];
6
- type PartKey = keyof DayPatterns;
7
- type DayPatterns = {
8
- y: "yy" | "yyyy";
9
- m: "mm" | "m";
10
- d: "dd" | "d";
11
- };
12
- type fragment<part extends DayPart, delimiter extends DayDelimiter> = `${delimiter}${part}` | "";
13
- export type DayPattern<delimiter extends DayDelimiter = DayDelimiter> = delimiter extends unknown ? {
14
- [k1 in keyof DayPatterns]: {
15
- [k2 in Exclude<keyof DayPatterns, k1>]: `${DayPatterns[k1]}${fragment<DayPatterns[k2], delimiter>}${fragment<DayPatterns[Exclude<keyof DayPatterns, k1 | k2>], delimiter>}`;
16
- }[Exclude<keyof DayPatterns, k1>];
17
- }[keyof DayPatterns] : never;
18
- export type DateFormat = "iso" | DayPattern;
19
- export type DateOptions = {
20
- format?: DateFormat;
21
- };
22
- export declare const iso8601Matcher: RegExp;
23
- export declare const tryParseDatePattern: (data: string, opts?: DateOptions) => Date | string;
24
- declare namespace string {
25
- type date = constrain<string, Branded<"date">>;
26
- namespace date {
27
- type epoch = constrain<string, Branded<"date.epoch">>;
28
- type iso = constrain<string, Branded<"date.iso">>;
29
- }
30
- }
31
- export declare const stringDate: stringDate.module;
32
- export declare namespace stringDate {
33
- type module = Module<stringDate.submodule>;
34
- type submodule = Submodule<$>;
35
- type $ = {
36
- $root: string.date;
37
- parse: (In: string.date) => To<Date>;
38
- iso: iso.submodule;
39
- epoch: epoch.submodule;
40
- };
41
- type shallowResolutions = {
42
- [k in keyof $ as `string.date.${k}`]: $[k] extends type.cast<infer t> ? t : $[k];
43
- };
44
- namespace iso {
45
- type submodule = Submodule<$>;
46
- type $ = {
47
- $root: string.date.iso;
48
- parse: (In: string.date.iso) => To<Date>;
49
- };
50
- }
51
- namespace epoch {
52
- type submodule = Submodule<$>;
53
- type $ = {
54
- $root: string.date.epoch;
55
- parse: (In: string.date.epoch) => To<Date>;
56
- };
57
- }
58
- }
59
- export {};
@@ -1,98 +0,0 @@
1
- import { ArkErrors, intrinsic, rootNode } from "@ark/schema";
2
- import { number } from "../number/number.js";
3
- import { submodule } from "../utils.js";
4
- import { integer } from "./integer.js";
5
- import { regexStringNode } from "./utils.js";
6
- const dayDelimiterMatcher = /^[./-]$/;
7
- // ISO 8601 date/time modernized from https://github.com/validatorjs/validator.js/blob/master/src/lib/isISO8601.js
8
- // Based on https://tc39.es/ecma262/#sec-date-time-string-format, the T
9
- // delimiter for date/time is mandatory. Regex from validator.js strict matcher:
10
- export const iso8601Matcher = /^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-3])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;
11
- const isValidDateInstance = (date) => !Number.isNaN(+date);
12
- const writeFormattedExpected = (format) => `a ${format}-formatted date`;
13
- export const tryParseDatePattern = (data, opts) => {
14
- if (!opts?.format) {
15
- const result = new Date(data);
16
- return isValidDateInstance(result) ? result : "a valid date";
17
- }
18
- if (opts.format === "iso") {
19
- return iso8601Matcher.test(data) ?
20
- new Date(data)
21
- : writeFormattedExpected("iso");
22
- }
23
- const dataParts = data.split(dayDelimiterMatcher);
24
- // will be the first delimiter matched, if there is one
25
- const delimiter = data[dataParts[0].length];
26
- const formatParts = delimiter ? opts.format.split(delimiter) : [opts.format];
27
- if (dataParts.length !== formatParts.length)
28
- return writeFormattedExpected(opts.format);
29
- const parsedParts = {};
30
- for (let i = 0; i < formatParts.length; i++) {
31
- if (dataParts[i].length !== formatParts[i].length &&
32
- // if format is "m" or "d", data is allowed to be 1 or 2 characters
33
- !(formatParts[i].length === 1 && dataParts[i].length === 2))
34
- return writeFormattedExpected(opts.format);
35
- parsedParts[formatParts[i][0]] = dataParts[i];
36
- }
37
- const date = new Date(`${parsedParts.m}/${parsedParts.d}/${parsedParts.y}`);
38
- if (`${date.getDate()}` === parsedParts.d)
39
- return date;
40
- return writeFormattedExpected(opts.format);
41
- };
42
- const isParsableDate = (s) => !Number.isNaN(new Date(s).valueOf());
43
- const parsableDate = rootNode({
44
- domain: "string",
45
- predicate: {
46
- meta: "a parsable date",
47
- predicate: isParsableDate
48
- }
49
- }).assertHasKind("intersection");
50
- const epoch$root = integer.$root.internal
51
- .narrow((s, ctx) => {
52
- // we know this is safe since it has already
53
- // been validated as an integer string
54
- const n = Number.parseInt(s);
55
- const out = number.epoch(n);
56
- if (out instanceof ArkErrors) {
57
- ctx.errors.merge(out);
58
- return false;
59
- }
60
- return true;
61
- })
62
- .withMeta({
63
- description: "an integer string representing a safe Unix timestamp"
64
- })
65
- .assertHasKind("intersection");
66
- const epoch = submodule({
67
- $root: epoch$root,
68
- parse: rootNode({
69
- in: epoch$root,
70
- morphs: (s) => new Date(s),
71
- declaredOut: intrinsic.Date
72
- })
73
- });
74
- const iso$root = regexStringNode(iso8601Matcher, "an ISO 8601 (YYYY-MM-DDTHH:mm:ss.sssZ) date").internal.assertHasKind("intersection");
75
- const iso = submodule({
76
- $root: iso$root,
77
- parse: rootNode({
78
- in: iso$root,
79
- morphs: (s) => new Date(s),
80
- declaredOut: intrinsic.Date
81
- })
82
- });
83
- export const stringDate = submodule({
84
- $root: parsableDate,
85
- parse: rootNode({
86
- declaredIn: parsableDate,
87
- in: "string",
88
- morphs: (s, ctx) => {
89
- const date = new Date(s);
90
- if (Number.isNaN(date.valueOf()))
91
- return ctx.error("a parsable date");
92
- return date;
93
- },
94
- declaredOut: intrinsic.Date
95
- }),
96
- iso,
97
- epoch
98
- });
@@ -1,7 +0,0 @@
1
- import type { Branded, constrain } from "../ast.ts";
2
- declare namespace string {
3
- type digits = constrain<string, Branded<"digits">>;
4
- }
5
- export declare const digits: import("@ark/schema").IntersectionNode;
6
- export type digits = string.digits;
7
- export {};
@@ -1,2 +0,0 @@
1
- import { regexStringNode } from "./utils.js";
2
- export const digits = regexStringNode(/^\d*$/, "only digits 0-9");
@@ -1,7 +0,0 @@
1
- import type { Branded, constrain } from "../ast.ts";
2
- declare namespace string {
3
- type email = constrain<string, Branded<"email">>;
4
- }
5
- export declare const email: import("@ark/schema").IntersectionNode;
6
- export type email = string.email;
7
- export {};
@@ -1,4 +0,0 @@
1
- import { regexStringNode } from "./utils.js";
2
- export const email = regexStringNode(
3
- // https://www.regular-expressions.info/email.html
4
- /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/, "an email address");
@@ -1,16 +0,0 @@
1
- import type { Module, Submodule } from "../../module.ts";
2
- import type { Branded, constrain, To } from "../ast.ts";
3
- import type { number } from "../number/number.ts";
4
- declare namespace string {
5
- type integer = constrain<string, Branded<"integer">>;
6
- }
7
- export declare const integer: stringInteger.module;
8
- export declare namespace stringInteger {
9
- type module = Module<submodule>;
10
- type submodule = Submodule<$>;
11
- type $ = {
12
- $root: string.integer;
13
- parse: (In: string.integer) => To<number.divisibleBy<1>>;
14
- };
15
- }
16
- export {};
@@ -1,16 +0,0 @@
1
- import { intrinsic, rootNode } from "@ark/schema";
2
- import { wellFormedIntegerMatcher } from "@ark/util";
3
- import { submodule } from "../utils.js";
4
- import { regexStringNode } from "./utils.js";
5
- const $root = regexStringNode(wellFormedIntegerMatcher, "a well-formed integer string");
6
- export const integer = submodule({
7
- $root,
8
- parse: rootNode({
9
- in: $root,
10
- morphs: (s, ctx) => {
11
- const parsed = Number.parseInt(s);
12
- return Number.isSafeInteger(parsed) ? parsed : (ctx.error("an integer in the range Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER"));
13
- },
14
- declaredOut: intrinsic.integer
15
- })
16
- });