arktype 2.0.0-dev.12-cjs → 2.0.0-dev.13

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.
@@ -1,12 +1,8 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writeInvalidConstructorMessage = exports.parseNarrowTuple = exports.writeMalformedFunctionalExpressionMessage = exports.parseMorphTuple = exports.parseKeyOfTuple = exports.spreadOptionalMessage = exports.optionalPostVariadicMessage = exports.requiredPostOptionalMessage = exports.multipleVariadicMesage = exports.writeNonArraySpreadMessage = exports.parseTupleLiteral = exports.parseTuple = void 0;
4
- const schema_1 = require("@arktype/schema");
5
- const util_1 = require("@arktype/util");
6
- const unenclosed_js_1 = require("./string/shift/operand/unenclosed.js");
7
- const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? (0, exports.parseTupleLiteral)(def, ctx);
8
- exports.parseTuple = parseTuple;
9
- const parseTupleLiteral = (def, ctx) => {
1
+ import { jsObjects, makeRootAndArrayPropertiesMutable, tsKeywords } from "@arktype/schema";
2
+ import { append, objectKindOrDomainOf, throwParseError } from "@arktype/util";
3
+ import { writeMissingRightOperandMessage } from "./string/shift/operand/unenclosed.js";
4
+ export const parseTuple = (def, ctx) => maybeParseTupleExpression(def, ctx) ?? parseTupleLiteral(def, ctx);
5
+ export const parseTupleLiteral = (def, ctx) => {
10
6
  let sequences = [{}];
11
7
  let i = 0;
12
8
  while (i < def.length) {
@@ -20,19 +16,19 @@ const parseTupleLiteral = (def, ctx) => {
20
16
  i++;
21
17
  if (def[i] === "?") {
22
18
  if (spread)
23
- return (0, util_1.throwParseError)(exports.spreadOptionalMessage);
19
+ return throwParseError(spreadOptionalMessage);
24
20
  optional = true;
25
21
  i++;
26
22
  }
27
23
  if (spread) {
28
- if (!element.extends(schema_1.jsObjects.Array))
29
- return (0, util_1.throwParseError)((0, exports.writeNonArraySpreadMessage)(element.expression));
24
+ if (!element.extends(jsObjects.Array))
25
+ return throwParseError(writeNonArraySpreadMessage(element.expression));
30
26
  // a spread must be distributed over branches e.g.:
31
27
  // def: [string, ...(number[] | [true, false])]
32
28
  // nodes: [string, ...number[]] | [string, true, false]
33
29
  sequences = sequences.flatMap(base =>
34
30
  // since appendElement mutates base, we have to shallow-ish clone it for each branch
35
- element.branches.map(branch => appendSpreadBranch((0, schema_1.makeRootAndArrayPropertiesMutable)(base), branch)));
31
+ element.branches.map(branch => appendSpreadBranch(makeRootAndArrayPropertiesMutable(base), branch)));
36
32
  }
37
33
  else {
38
34
  sequences = sequences.map(base => appendElement(base, optional ? "optional" : "required", element));
@@ -43,37 +39,36 @@ const parseTupleLiteral = (def, ctx) => {
43
39
  sequence
44
40
  })));
45
41
  };
46
- exports.parseTupleLiteral = parseTupleLiteral;
47
42
  const appendElement = (base, kind, element) => {
48
43
  switch (kind) {
49
44
  case "required":
50
45
  if (base.optionals)
51
46
  // e.g. [string?, number]
52
- return (0, util_1.throwParseError)(exports.requiredPostOptionalMessage);
47
+ return throwParseError(requiredPostOptionalMessage);
53
48
  if (base.variadic) {
54
49
  // e.g. [...string[], number]
55
- base.postfix = (0, util_1.append)(base.postfix, element);
50
+ base.postfix = append(base.postfix, element);
56
51
  }
57
52
  else {
58
53
  // e.g. [string, number]
59
- base.prefix = (0, util_1.append)(base.prefix, element);
54
+ base.prefix = append(base.prefix, element);
60
55
  }
61
56
  return base;
62
57
  case "optional":
63
58
  if (base.variadic)
64
59
  // e.g. [...string[], number?]
65
- return (0, util_1.throwParseError)(exports.optionalPostVariadicMessage);
60
+ return throwParseError(optionalPostVariadicMessage);
66
61
  // e.g. [string, number?]
67
- base.optionals = (0, util_1.append)(base.optionals, element);
62
+ base.optionals = append(base.optionals, element);
68
63
  return base;
69
64
  case "variadic":
70
65
  // e.g. [...string[], number, ...string[]]
71
66
  if (base.postfix)
72
- (0, util_1.throwParseError)(exports.multipleVariadicMesage);
67
+ throwParseError(multipleVariadicMesage);
73
68
  if (base.variadic) {
74
69
  if (!base.variadic.equals(element)) {
75
70
  // e.g. [...string[], ...number[]]
76
- (0, util_1.throwParseError)(exports.multipleVariadicMesage);
71
+ throwParseError(multipleVariadicMesage);
77
72
  }
78
73
  // e.g. [...string[], ...string[]]
79
74
  // do nothing, second spread doesn't change the type
@@ -89,7 +84,7 @@ const appendSpreadBranch = (base, branch) => {
89
84
  const spread = branch.firstReferenceOfKind("sequence");
90
85
  if (!spread) {
91
86
  // the only array with no sequence reference is unknown[]
92
- return appendElement(base, "variadic", schema_1.tsKeywords.unknown);
87
+ return appendElement(base, "variadic", tsKeywords.unknown);
93
88
  }
94
89
  spread.prefix.forEach(node => appendElement(base, "required", node));
95
90
  spread.optionals.forEach(node => appendElement(base, "optional", node));
@@ -102,73 +97,56 @@ const maybeParseTupleExpression = (def, ctx) => {
102
97
  : isIndexOneExpression(def) ? indexOneParsers[def[1]](def, ctx)
103
98
  : undefined;
104
99
  return tupleExpressionResult;
105
- // TODO: remove
106
- // return tupleExpressionResult.isNever()
107
- // ? throwParseError(
108
- // writeUnsatisfiableExpressionError(
109
- // def
110
- // .map((def) =>
111
- // typeof def === "string" ? def : printable(def)
112
- // )
113
- // .join(" ")
114
- // )
115
- // )
116
- // : tupleExpressionResult
117
100
  };
118
- const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
119
- exports.writeNonArraySpreadMessage = writeNonArraySpreadMessage;
120
- exports.multipleVariadicMesage = "A tuple may have at most one variadic element";
121
- exports.requiredPostOptionalMessage = "A required element may not follow an optional element";
122
- exports.optionalPostVariadicMessage = "An optional element may not follow a variadic element";
123
- exports.spreadOptionalMessage = "A spread element cannot be optional";
124
- const parseKeyOfTuple = (def, ctx) => ctx.$.parse(def[1], ctx).keyof();
125
- exports.parseKeyOfTuple = parseKeyOfTuple;
101
+ export const writeNonArraySpreadMessage = (operand) => `Spread element must be an array (was ${operand})`;
102
+ export const multipleVariadicMesage = "A tuple may have at most one variadic element";
103
+ export const requiredPostOptionalMessage = "A required element may not follow an optional element";
104
+ export const optionalPostVariadicMessage = "An optional element may not follow a variadic element";
105
+ export const spreadOptionalMessage = "A spread element cannot be optional";
106
+ export const parseKeyOfTuple = (def, ctx) => ctx.$.parse(def[1], ctx).keyof();
126
107
  const parseBranchTuple = (def, ctx) => {
127
108
  if (def[2] === undefined)
128
- return (0, util_1.throwParseError)((0, unenclosed_js_1.writeMissingRightOperandMessage)(def[1], ""));
109
+ return throwParseError(writeMissingRightOperandMessage(def[1], ""));
129
110
  const l = ctx.$.parse(def[0], ctx);
130
111
  const r = ctx.$.parse(def[2], ctx);
131
112
  return def[1] === "&" ? l.and(r) : l.or(r);
132
113
  };
133
114
  const parseArrayTuple = (def, ctx) => ctx.$.parse(def[0], ctx).array();
134
115
  const isIndexOneExpression = (def) => indexOneParsers[def[1]] !== undefined;
135
- const parseMorphTuple = (def, ctx) => {
116
+ export const parseMorphTuple = (def, ctx) => {
136
117
  if (typeof def[2] !== "function") {
137
- return (0, util_1.throwParseError)((0, exports.writeMalformedFunctionalExpressionMessage)("=>", def[2]));
118
+ return throwParseError(writeMalformedFunctionalExpressionMessage("=>", def[2]));
138
119
  }
139
120
  // TODO: nested morphs?
140
121
  return ctx.$.parse(def[0], ctx).pipe(def[2]);
141
122
  };
142
- exports.parseMorphTuple = parseMorphTuple;
143
- const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
144
- exports.writeMalformedFunctionalExpressionMessage = writeMalformedFunctionalExpressionMessage;
145
- const parseNarrowTuple = (def, ctx) => {
123
+ export const writeMalformedFunctionalExpressionMessage = (operator, value) => `${operator === ":" ? "Narrow" : "Morph"} expression requires a function following '${operator}' (was ${typeof value})`;
124
+ export const parseNarrowTuple = (def, ctx) => {
146
125
  if (typeof def[2] !== "function") {
147
- return (0, util_1.throwParseError)((0, exports.writeMalformedFunctionalExpressionMessage)(":", def[2]));
126
+ return throwParseError(writeMalformedFunctionalExpressionMessage(":", def[2]));
148
127
  }
149
128
  return ctx.$.parse(def[0], ctx).constrain("predicate", def[2]);
150
129
  };
151
- exports.parseNarrowTuple = parseNarrowTuple;
152
130
  const parseAttributeTuple = (def, ctx) => ctx.$.parse(def[0], ctx).configureShallowDescendants(def[2]);
153
131
  const indexOneParsers = {
154
132
  "|": parseBranchTuple,
155
133
  "&": parseBranchTuple,
156
134
  "[]": parseArrayTuple,
157
- ":": exports.parseNarrowTuple,
158
- "=>": exports.parseMorphTuple,
135
+ ":": parseNarrowTuple,
136
+ "=>": parseMorphTuple,
159
137
  "@": parseAttributeTuple
160
138
  };
161
139
  const prefixParsers = {
162
- keyof: exports.parseKeyOfTuple,
140
+ keyof: parseKeyOfTuple,
163
141
  instanceof: (def, ctx) => {
164
142
  if (typeof def[1] !== "function") {
165
- return (0, util_1.throwParseError)((0, exports.writeInvalidConstructorMessage)((0, util_1.objectKindOrDomainOf)(def[1])));
143
+ return throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(def[1])));
166
144
  }
167
145
  const branches = def
168
146
  .slice(1)
169
147
  .map(ctor => typeof ctor === "function" ?
170
148
  ctx.$.node("proto", { proto: ctor })
171
- : (0, util_1.throwParseError)((0, exports.writeInvalidConstructorMessage)((0, util_1.objectKindOrDomainOf)(ctor))));
149
+ : throwParseError(writeInvalidConstructorMessage(objectKindOrDomainOf(ctor))));
172
150
  return branches.length === 1 ?
173
151
  branches[0]
174
152
  : ctx.$.node("union", { branches });
@@ -176,5 +154,4 @@ const prefixParsers = {
176
154
  "===": (def, ctx) => ctx.$.units(def.slice(1))
177
155
  };
178
156
  const isIndexZeroExpression = (def) => prefixParsers[def[0]] !== undefined;
179
- const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
180
- exports.writeInvalidConstructorMessage = writeInvalidConstructorMessage;
157
+ export const writeInvalidConstructorMessage = (actual) => `Expected a constructor following 'instanceof' operator (was ${actual})`;
package/out/scope.js CHANGED
@@ -1,38 +1,35 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseScopeKey = exports.writeShallowCycleErrorMessage = exports.RawScope = exports.scope = void 0;
4
- const schema_1 = require("@arktype/schema");
5
- const util_1 = require("@arktype/util");
6
- const generic_js_1 = require("./generic.js");
7
- const match_js_1 = require("./match.js");
8
- const definition_js_1 = require("./parser/definition.js");
9
- const generic_js_2 = require("./parser/generic.js");
10
- const dynamic_js_1 = require("./parser/string/reduce/dynamic.js");
11
- const string_js_1 = require("./parser/string/string.js");
12
- const type_js_1 = require("./type.js");
13
- exports.scope = ((def, config = {}) => new RawScope(def, config));
14
- class RawScope extends schema_1.RawRootScope {
1
+ import { RawRootScope, hasArkKind } from "@arktype/schema";
2
+ import { domainOf, hasDomain, isThunk, throwParseError } from "@arktype/util";
3
+ import { Generic } from "./generic.js";
4
+ import { createMatchParser } from "./match.js";
5
+ import { parseObject, writeBadDefinitionTypeMessage } from "./parser/definition.js";
6
+ import { parseGenericParams } from "./parser/generic.js";
7
+ import { DynamicState } from "./parser/string/reduce/dynamic.js";
8
+ import { fullStringParse } from "./parser/string/string.js";
9
+ import { RawTypeParser } from "./type.js";
10
+ export const scope = ((def, config = {}) => new RawScope(def, config));
11
+ export class RawScope extends RawRootScope {
15
12
  parseCache = {};
16
13
  constructor(def, config) {
17
14
  const aliases = {};
18
15
  for (const k in def) {
19
- const parsedKey = (0, exports.parseScopeKey)(k);
16
+ const parsedKey = parseScopeKey(k);
20
17
  aliases[parsedKey.name] =
21
18
  parsedKey.params.length ?
22
19
  // TODO: this
23
- new generic_js_1.Generic(parsedKey.params, def[k], {})
20
+ new Generic(parsedKey.params, def[k], {})
24
21
  : def[k];
25
22
  }
26
23
  super(aliases, config);
27
24
  }
28
- type = new type_js_1.RawTypeParser(this);
29
- match = (0, match_js_1.createMatchParser)(this);
25
+ type = new RawTypeParser(this);
26
+ match = createMatchParser(this);
30
27
  declare = (() => ({
31
28
  type: this.type
32
29
  })).bind(this);
33
30
  define = ((def) => def).bind(this);
34
31
  preparseRoot(def) {
35
- if ((0, util_1.isThunk)(def) && !(0, schema_1.hasArkKind)(def, "generic"))
32
+ if (isThunk(def) && !hasArkKind(def, "generic"))
36
33
  return def();
37
34
  return def;
38
35
  }
@@ -56,21 +53,19 @@ class RawScope extends schema_1.RawRootScope {
56
53
  this.parseCache[def] = this.parseString(def, ctx);
57
54
  return this.parseCache[def];
58
55
  }
59
- return (0, util_1.hasDomain)(def, "object") ?
60
- (0, definition_js_1.parseObject)(def, ctx)
61
- : (0, util_1.throwParseError)((0, definition_js_1.writeBadDefinitionTypeMessage)((0, util_1.domainOf)(def)));
56
+ return hasDomain(def, "object") ?
57
+ parseObject(def, ctx)
58
+ : throwParseError(writeBadDefinitionTypeMessage(domainOf(def)));
62
59
  }
63
60
  parseString(def, ctx) {
64
61
  return (this.maybeResolveRoot(def) ??
65
62
  ((def.endsWith("[]") &&
66
63
  this.maybeResolveRoot(def.slice(0, -2))?.array()) ||
67
- (0, string_js_1.fullStringParse)(new dynamic_js_1.DynamicState(def, ctx))));
64
+ fullStringParse(new DynamicState(def, ctx))));
68
65
  }
69
66
  }
70
- exports.RawScope = RawScope;
71
- const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
72
- exports.writeShallowCycleErrorMessage = writeShallowCycleErrorMessage;
73
- const parseScopeKey = (k) => {
67
+ export const writeShallowCycleErrorMessage = (name, seen) => `Alias '${name}' has a shallow resolution cycle: ${[...seen, name].join(":")}`;
68
+ export const parseScopeKey = (k) => {
74
69
  const firstParamIndex = k.indexOf("<");
75
70
  if (firstParamIndex === -1) {
76
71
  return {
@@ -79,11 +74,10 @@ const parseScopeKey = (k) => {
79
74
  };
80
75
  }
81
76
  if (k.at(-1) !== ">") {
82
- (0, util_1.throwParseError)(`'>' must be the last character of a generic declaration in a scope`);
77
+ throwParseError(`'>' must be the last character of a generic declaration in a scope`);
83
78
  }
84
79
  return {
85
80
  name: k.slice(0, firstParamIndex),
86
- params: (0, generic_js_2.parseGenericParams)(k.slice(firstParamIndex + 1, -1))
81
+ params: parseGenericParams(k.slice(firstParamIndex + 1, -1))
87
82
  };
88
83
  };
89
- exports.parseScopeKey = parseScopeKey;
package/out/type.js CHANGED
@@ -1,14 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Type = exports.RawTypeParser = void 0;
4
- const schema_1 = require("@arktype/schema");
5
- const util_1 = require("@arktype/util");
6
- const generic_js_1 = require("./generic.js");
7
- const generic_js_2 = require("./parser/generic.js");
1
+ import { ArkErrors, BaseRoot } from "@arktype/schema";
2
+ import { Callable } from "@arktype/util";
3
+ import { Generic } from "./generic.js";
4
+ import { parseGenericParams } from "./parser/generic.js";
8
5
  const typeParserAttachments = Object.freeze({
9
- errors: schema_1.ArkErrors
6
+ errors: ArkErrors
10
7
  });
11
- class RawTypeParser extends util_1.Callable {
8
+ export class RawTypeParser extends Callable {
12
9
  constructor($) {
13
10
  super((...args) => {
14
11
  if (args.length === 1) {
@@ -21,10 +18,10 @@ class RawTypeParser extends util_1.Callable {
21
18
  args[0].at(-1) === ">") {
22
19
  // if there are exactly two args, the first of which looks like <${string}>,
23
20
  // treat as a generic
24
- const params = (0, generic_js_2.parseGenericParams)(args[0].slice(1, -1));
21
+ const params = parseGenericParams(args[0].slice(1, -1));
25
22
  const def = args[1];
26
23
  // TODO: validateUninstantiatedGeneric, remove this cast
27
- return new generic_js_1.Generic(params, def, $);
24
+ return new Generic(params, def, $);
28
25
  }
29
26
  // otherwise, treat as a tuple expression. technically, this also allows
30
27
  // non-expression tuple definitions to be parsed, but it's not a supported
@@ -33,5 +30,4 @@ class RawTypeParser extends util_1.Callable {
33
30
  }, { bind: $, attach: typeParserAttachments });
34
31
  }
35
32
  }
36
- exports.RawTypeParser = RawTypeParser;
37
- exports.Type = schema_1.BaseRoot;
33
+ export const Type = BaseRoot;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "arktype",
3
3
  "description": "TypeScript's 1:1 validator, optimized from editor to runtime",
4
- "version": "2.0.0-dev.12-cjs",
4
+ "version": "2.0.0-dev.13",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "David Blass",
@@ -20,6 +20,9 @@
20
20
  "./config": "./out/config.js",
21
21
  "./internal/*": "./out/*"
22
22
  },
23
+ "imports": {
24
+ "#foo": "../schema/api.ts"
25
+ },
23
26
  "files": [
24
27
  "out"
25
28
  ],
@@ -28,7 +31,7 @@
28
31
  "test": "tsx ../repo/testPackage.ts"
29
32
  },
30
33
  "dependencies": {
31
- "@arktype/util": "0.0.41-cjs",
32
- "@arktype/schema": "0.1.4-cjs"
34
+ "@arktype/util": "0.0.42",
35
+ "@arktype/schema": "0.1.5"
33
36
  }
34
37
  }