arktype 2.0.0-rc.9 → 2.0.1

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 (128) hide show
  1. package/out/attributes.d.ts +94 -0
  2. package/out/attributes.js +3 -0
  3. package/out/generic.d.ts +13 -13
  4. package/out/index.d.ts +3 -1
  5. package/out/index.js +2 -1
  6. package/out/keywords/{constructors/Array.d.ts → Array.d.ts} +2 -2
  7. package/out/keywords/{constructors/Array.js → Array.js} +2 -2
  8. package/out/keywords/{constructors/FormData.d.ts → FormData.d.ts} +2 -2
  9. package/out/keywords/{constructors/FormData.js → FormData.js} +2 -2
  10. package/out/keywords/{constructors/TypedArray.d.ts → TypedArray.d.ts} +1 -1
  11. package/out/keywords/{constructors/TypedArray.js → TypedArray.js} +2 -2
  12. package/out/keywords/builtins.js +2 -2
  13. package/out/keywords/{constructors/constructors.d.ts → constructors.d.ts} +1 -1
  14. package/out/keywords/{constructors/constructors.js → constructors.js} +2 -2
  15. package/out/keywords/keywords.d.ts +14 -13
  16. package/out/keywords/keywords.js +9 -5
  17. package/out/keywords/number.d.ts +23 -0
  18. package/out/keywords/{number/epoch.js → number.js} +22 -1
  19. package/out/keywords/string.d.ts +327 -0
  20. package/out/keywords/string.js +398 -0
  21. package/out/keywords/ts.d.ts +31 -4
  22. package/out/keywords/ts.js +21 -8
  23. package/out/methods/array.d.ts +6 -7
  24. package/out/methods/base.d.ts +461 -35
  25. package/out/methods/date.d.ts +5 -6
  26. package/out/methods/instantiate.d.ts +4 -4
  27. package/out/methods/morph.d.ts +6 -6
  28. package/out/methods/number.d.ts +8 -9
  29. package/out/methods/object.d.ts +39 -15
  30. package/out/methods/string.d.ts +9 -10
  31. package/out/parser/ast/bounds.d.ts +1 -1
  32. package/out/parser/ast/default.d.ts +2 -2
  33. package/out/parser/ast/generic.d.ts +17 -0
  34. package/out/parser/ast/infer.d.ts +8 -22
  35. package/out/parser/ast/validate.d.ts +13 -9
  36. package/out/parser/ast/validate.js +3 -1
  37. package/out/parser/definition.d.ts +17 -8
  38. package/out/parser/definition.js +38 -3
  39. package/out/parser/objectLiteral.d.ts +39 -39
  40. package/out/parser/objectLiteral.js +83 -67
  41. package/out/parser/property.d.ts +36 -0
  42. package/out/parser/property.js +16 -0
  43. package/out/parser/reduce/dynamic.d.ts +8 -9
  44. package/out/parser/reduce/shared.d.ts +1 -1
  45. package/out/parser/reduce/static.d.ts +4 -4
  46. package/out/parser/shift/operand/date.d.ts +1 -1
  47. package/out/parser/shift/operand/enclosed.d.ts +3 -4
  48. package/out/parser/shift/operand/operand.d.ts +3 -3
  49. package/out/parser/shift/operand/operand.js +2 -2
  50. package/out/parser/shift/operand/unenclosed.d.ts +13 -5
  51. package/out/parser/shift/operand/unenclosed.js +5 -4
  52. package/out/parser/shift/operator/bounds.d.ts +5 -5
  53. package/out/parser/shift/operator/bounds.js +5 -4
  54. package/out/parser/shift/operator/brand.d.ts +6 -0
  55. package/out/parser/shift/operator/brand.js +5 -0
  56. package/out/parser/shift/operator/default.d.ts +3 -2
  57. package/out/parser/shift/operator/default.js +4 -1
  58. package/out/parser/shift/operator/divisor.d.ts +2 -2
  59. package/out/parser/shift/operator/operator.d.ts +4 -3
  60. package/out/parser/shift/operator/operator.js +7 -5
  61. package/out/parser/shift/scanner.d.ts +31 -55
  62. package/out/parser/shift/scanner.js +24 -83
  63. package/out/parser/string.d.ts +5 -4
  64. package/out/parser/string.js +18 -1
  65. package/out/parser/tupleExpressions.d.ts +48 -0
  66. package/out/parser/tupleExpressions.js +67 -0
  67. package/out/parser/tupleLiteral.d.ts +95 -0
  68. package/out/parser/tupleLiteral.js +122 -0
  69. package/out/scope.d.ts +17 -12
  70. package/out/scope.js +25 -41
  71. package/out/type.d.ts +63 -5
  72. package/out/type.js +3 -2
  73. package/package.json +8 -8
  74. package/out/keywords/constructors/Date.d.ts +0 -31
  75. package/out/keywords/inference.d.ts +0 -220
  76. package/out/keywords/inference.js +0 -2
  77. package/out/keywords/number/epoch.d.ts +0 -9
  78. package/out/keywords/number/integer.d.ts +0 -3
  79. package/out/keywords/number/integer.js +0 -5
  80. package/out/keywords/number/number.d.ts +0 -53
  81. package/out/keywords/number/number.js +0 -21
  82. package/out/keywords/string/alpha.d.ts +0 -7
  83. package/out/keywords/string/alpha.js +0 -2
  84. package/out/keywords/string/alphanumeric.d.ts +0 -7
  85. package/out/keywords/string/alphanumeric.js +0 -2
  86. package/out/keywords/string/capitalize.d.ts +0 -15
  87. package/out/keywords/string/capitalize.js +0 -12
  88. package/out/keywords/string/creditCard.d.ts +0 -8
  89. package/out/keywords/string/creditCard.js +0 -37
  90. package/out/keywords/string/date.d.ts +0 -55
  91. package/out/keywords/string/date.js +0 -98
  92. package/out/keywords/string/digits.d.ts +0 -7
  93. package/out/keywords/string/digits.js +0 -2
  94. package/out/keywords/string/email.d.ts +0 -7
  95. package/out/keywords/string/email.js +0 -4
  96. package/out/keywords/string/integer.d.ts +0 -16
  97. package/out/keywords/string/integer.js +0 -16
  98. package/out/keywords/string/ip.d.ts +0 -20
  99. package/out/keywords/string/ip.js +0 -23
  100. package/out/keywords/string/json.d.ts +0 -16
  101. package/out/keywords/string/json.js +0 -53
  102. package/out/keywords/string/lower.d.ts +0 -15
  103. package/out/keywords/string/lower.js +0 -12
  104. package/out/keywords/string/normalize.d.ts +0 -88
  105. package/out/keywords/string/normalize.js +0 -43
  106. package/out/keywords/string/numeric.d.ts +0 -15
  107. package/out/keywords/string/numeric.js +0 -13
  108. package/out/keywords/string/semver.d.ts +0 -7
  109. package/out/keywords/string/semver.js +0 -4
  110. package/out/keywords/string/string.d.ts +0 -108
  111. package/out/keywords/string/string.js +0 -41
  112. package/out/keywords/string/trim.d.ts +0 -15
  113. package/out/keywords/string/trim.js +0 -14
  114. package/out/keywords/string/upper.d.ts +0 -15
  115. package/out/keywords/string/upper.js +0 -12
  116. package/out/keywords/string/url.d.ts +0 -15
  117. package/out/keywords/string/url.js +0 -37
  118. package/out/keywords/string/utils.d.ts +0 -2
  119. package/out/keywords/string/utils.js +0 -10
  120. package/out/keywords/string/uuid.d.ts +0 -42
  121. package/out/keywords/string/uuid.js +0 -19
  122. package/out/keywords/utils.d.ts +0 -1
  123. package/out/keywords/utils.js +0 -2
  124. package/out/methods/validator.d.ts +0 -11
  125. package/out/methods/validator.js +0 -1
  126. package/out/parser/tuple.d.ts +0 -143
  127. package/out/parser/tuple.js +0 -164
  128. /package/out/{keywords/constructors/Date.js → parser/ast/generic.js} +0 -0
@@ -1,164 +0,0 @@
1
- import { $ark, Disjoint, intersectNodesRoot, makeRootAndArrayPropertiesMutable } from "@ark/schema";
2
- import { append, isEmptyObject, objectKindOrDomainOf, throwParseError } from "@ark/util";
3
- import { writeMissingRightOperandMessage } from "./shift/operand/unenclosed.js";
4
- export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
5
- export const parseTupleLiteral = (def, ctx) => {
6
- let sequences = [{}];
7
- let i = 0;
8
- while (i < def.length) {
9
- let spread = false;
10
- if (def[i] === "..." && i < def.length - 1) {
11
- spread = true;
12
- i++;
13
- }
14
- const element = ctx.$.parseOwnDefinitionFormat(def[i], ctx);
15
- i++;
16
- if (spread) {
17
- if (!element.extends($ark.intrinsic.Array))
18
- return throwParseError(writeNonArraySpreadMessage(element.expression));
19
- // a spread must be distributed over branches e.g.:
20
- // def: [string, ...(number[] | [true, false])]
21
- // nodes: [string, ...number[]] | [string, true, false]
22
- sequences = sequences.flatMap(base =>
23
- // since appendElement mutates base, we have to shallow-ish clone it for each branch
24
- element.distribute(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
25
- }
26
- else {
27
- sequences = sequences.map(base => appendElement(base, element.meta.optional ? "optional" : "required", element));
28
- }
29
- }
30
- return ctx.$.parseSchema(sequences.map(sequence => isEmptyObject(sequence) ?
31
- {
32
- proto: Array,
33
- exactLength: 0
34
- }
35
- : {
36
- proto: Array,
37
- sequence
38
- }));
39
- };
40
- const appendElement = (base, kind, element) => {
41
- switch (kind) {
42
- case "required":
43
- if (base.optionals)
44
- // e.g. [string?, number]
45
- return throwParseError(requiredPostOptionalMessage);
46
- if (base.variadic) {
47
- // e.g. [...string[], number]
48
- base.postfix = append(base.postfix, element);
49
- }
50
- else {
51
- // e.g. [string, number]
52
- base.prefix = append(base.prefix, element);
53
- }
54
- return base;
55
- case "optional":
56
- if (base.variadic)
57
- // e.g. [...string[], number?]
58
- return throwParseError(optionalPostVariadicMessage);
59
- // e.g. [string, number?]
60
- base.optionals = append(base.optionals, element);
61
- return base;
62
- case "variadic":
63
- // e.g. [...string[], number, ...string[]]
64
- if (base.postfix)
65
- throwParseError(multipleVariadicMesage);
66
- if (base.variadic) {
67
- if (!base.variadic.equals(element)) {
68
- // e.g. [...string[], ...number[]]
69
- throwParseError(multipleVariadicMesage);
70
- }
71
- // e.g. [...string[], ...string[]]
72
- // do nothing, second spread doesn't change the type
73
- }
74
- else {
75
- // e.g. [string, ...number[]]
76
- base.variadic = element.internal;
77
- }
78
- return base;
79
- }
80
- };
81
- const appendSpreadBranch = (base, branch) => {
82
- const spread = branch.firstReferenceOfKind("sequence");
83
- if (!spread) {
84
- // the only array with no sequence reference is unknown[]
85
- return appendElement(base, "variadic", $ark.intrinsic.unknown);
86
- }
87
- spread.prefix?.forEach(node => appendElement(base, "required", node));
88
- spread.optionals?.forEach(node => appendElement(base, "optional", node));
89
- if (spread.variadic)
90
- appendElement(base, "variadic", spread.variadic);
91
- spread.postfix?.forEach(node => appendElement(base, "required", node));
92
- return base;
93
- };
94
- const maybeParseTupleExpression = (def, ctx) => {
95
- const tupleExpressionResult = isIndexZeroExpression(def) ? prefixParsers[def[0]](def, ctx)
96
- : isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
97
- : undefined;
98
- return tupleExpressionResult;
99
- };
100
- export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
101
- export const multipleVariadicMesage = "A tuple may have at most one variadic element";
102
- export const requiredPostOptionalMessage = "A required element may not follow an optional element";
103
- export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
104
- export const spreadOptionalMessage = "A spread element cannot be optional";
105
- export const parseKeyOfTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[1], ctx).keyof();
106
- const parseBranchTuple = (def, ctx) => {
107
- if (def[2] === undefined)
108
- return throwParseError(writeMissingRightOperandMessage(def[1], ""));
109
- const l = ctx.$.parseOwnDefinitionFormat(def[0], ctx);
110
- const r = ctx.$.parseOwnDefinitionFormat(def[2], ctx);
111
- if (def[1] === "|")
112
- return ctx.$.node("union", { branches: [l, r] });
113
- const result = intersectNodesRoot(l, r, ctx.$);
114
- if (result instanceof Disjoint)
115
- return result.throw();
116
- return result;
117
- };
118
- const parseArrayTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).array();
119
- const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
120
- export const parseMorphTuple = (def, ctx) => {
121
- if (typeof def[2] !== "function") {
122
- return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
123
- }
124
- return ctx.$.parseOwnDefinitionFormat(def[0], ctx).pipe(def[2]);
125
- };
126
- export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
127
- export const parseNarrowTuple = (def, ctx) => {
128
- if (typeof def[2] !== "function") {
129
- return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
130
- }
131
- return ctx.$.parseOwnDefinitionFormat(def[0], ctx).constrain("predicate", def[2]);
132
- };
133
- const parseAttributeTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).configureShallowDescendants(def[2]);
134
- const parseDefaultTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).default(def[2]);
135
- const parseOptionalTuple = (def, ctx) => ctx.$.parseOwnDefinitionFormat(def[0], ctx).optional();
136
- const indexOneParsers = {
137
- "[]": parseArrayTuple,
138
- "?": parseOptionalTuple,
139
- "|": parseBranchTuple,
140
- "&": parseBranchTuple,
141
- ":": parseNarrowTuple,
142
- "=>": parseMorphTuple,
143
- "@": parseAttributeTuple,
144
- "=": parseDefaultTuple
145
- };
146
- const prefixParsers = {
147
- keyof: parseKeyOfTuple,
148
- instanceof: (def, ctx) => {
149
- if (typeof def[1] !== "function") {
150
- return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
151
- }
152
- const branches = def
153
- .slice(1)
154
- .map(ctor => typeof ctor === "function" ?
155
- ctx.$.node("proto", { proto: ctor })
156
- : throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
157
- return branches.length === 1 ?
158
- branches[0]
159
- : ctx.$.node("union", { branches });
160
- },
161
- "===": (def, ctx) => ctx.$.units(def.slice(1))
162
- };
163
- const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
164
- export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;