arktype 0.1.2 → 0.1.4

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 (72) hide show
  1. package/dist/cjs/main.js +135 -20
  2. package/dist/cjs/nodes/branches.js +7 -7
  3. package/dist/cjs/nodes/compose.js +3 -2
  4. package/dist/cjs/nodes/discriminate.js +5 -4
  5. package/dist/cjs/nodes/functors.js +3 -3
  6. package/dist/cjs/nodes/node.js +1 -1
  7. package/dist/cjs/nodes/predicate.js +19 -14
  8. package/dist/cjs/nodes/rules/collapsibleSet.js +3 -4
  9. package/dist/cjs/nodes/rules/objectKind.js +78 -0
  10. package/dist/cjs/nodes/rules/range.js +2 -2
  11. package/dist/cjs/nodes/rules/rules.js +5 -5
  12. package/dist/cjs/parse/definition.js +26 -6
  13. package/dist/cjs/parse/string/ast.js +1 -1
  14. package/dist/cjs/parse/string/shift/operator/bounds.js +1 -1
  15. package/dist/cjs/parse/tuple/keyof.js +6 -7
  16. package/dist/cjs/parse/tuple/morph.js +2 -2
  17. package/dist/cjs/parse/tuple/tuple.js +1 -1
  18. package/dist/cjs/traverse/problems.js +18 -32
  19. package/dist/cjs/traverse/traverse.js +12 -12
  20. package/dist/cjs/utils/domains.js +10 -47
  21. package/dist/cjs/utils/generics.js +1 -3
  22. package/dist/cjs/utils/objectKinds.js +64 -0
  23. package/dist/cjs/utils/size.js +16 -0
  24. package/dist/mjs/main.js +134 -19
  25. package/dist/mjs/nodes/branches.js +7 -7
  26. package/dist/mjs/nodes/compose.js +3 -2
  27. package/dist/mjs/nodes/discriminate.js +6 -5
  28. package/dist/mjs/nodes/functors.js +3 -3
  29. package/dist/mjs/nodes/node.js +1 -1
  30. package/dist/mjs/nodes/predicate.js +20 -15
  31. package/dist/mjs/nodes/rules/collapsibleSet.js +3 -4
  32. package/dist/mjs/nodes/rules/{subdomain.js → objectKind.js} +10 -10
  33. package/dist/mjs/nodes/rules/range.js +1 -1
  34. package/dist/mjs/nodes/rules/rules.js +5 -5
  35. package/dist/mjs/parse/definition.js +27 -7
  36. package/dist/mjs/parse/string/ast.js +1 -1
  37. package/dist/mjs/parse/string/shift/operator/bounds.js +1 -1
  38. package/dist/mjs/parse/tuple/keyof.js +6 -7
  39. package/dist/mjs/parse/tuple/morph.js +2 -2
  40. package/dist/mjs/parse/tuple/tuple.js +1 -1
  41. package/dist/mjs/traverse/problems.js +18 -31
  42. package/dist/mjs/traverse/traverse.js +12 -12
  43. package/dist/mjs/utils/domains.js +9 -42
  44. package/dist/mjs/utils/generics.js +0 -1
  45. package/dist/mjs/utils/objectKinds.js +48 -0
  46. package/dist/mjs/utils/size.js +2 -0
  47. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  48. package/dist/types/main.d.ts +82 -15
  49. package/dist/types/nodes/branches.d.ts +4 -4
  50. package/dist/types/nodes/compose.d.ts +12 -5
  51. package/dist/types/nodes/discriminate.d.ts +3 -2
  52. package/dist/types/nodes/infer.d.ts +11 -10
  53. package/dist/types/nodes/predicate.d.ts +2 -2
  54. package/dist/types/nodes/rules/objectKind.d.ts +15 -0
  55. package/dist/types/nodes/rules/range.d.ts +1 -1
  56. package/dist/types/nodes/rules/rules.d.ts +4 -4
  57. package/dist/types/parse/definition.d.ts +6 -5
  58. package/dist/types/parse/record.d.ts +2 -2
  59. package/dist/types/parse/string/ast.d.ts +7 -5
  60. package/dist/types/parse/string/reduce/shared.d.ts +1 -1
  61. package/dist/types/parse/string/shift/operator/bounds.d.ts +1 -1
  62. package/dist/types/parse/tuple/keyof.d.ts +8 -0
  63. package/dist/types/parse/tuple/tuple.d.ts +6 -12
  64. package/dist/types/traverse/problems.d.ts +25 -41
  65. package/dist/types/traverse/traverse.d.ts +24 -4
  66. package/dist/types/utils/domains.d.ts +11 -37
  67. package/dist/types/utils/generics.d.ts +2 -2
  68. package/dist/types/utils/objectKinds.d.ts +83 -0
  69. package/dist/types/utils/size.d.ts +3 -0
  70. package/package.json +4 -4
  71. package/dist/cjs/nodes/rules/subdomain.js +0 -78
  72. package/dist/types/nodes/rules/subdomain.d.ts +0 -15
@@ -30,7 +30,7 @@ const compileDisjointReasonsMessage = (disjoints)=>{
30
30
  }
31
31
  return message;
32
32
  };
33
- const writeImplicitNeverMessage = (path, description)=>`${path.length ? `At ${path}: ` : ""}Intersection ${description ? `${description} ` : ""}results in an unsatisfiable type`;
33
+ const writeImplicitNeverMessage = (path, operator, description)=>`${path.length ? `At ${path}: ` : ""}${operator} ${description ? `${description} ` : ""}results in an unsatisfiable type`;
34
34
  const writeUndiscriminatableMorphUnionMessage = (path)=>`${path === "/" ? "A" : `At ${path}, a`} union including one or more morphs must be discriminatable`;
35
35
  const writeIndivisibleMessage = (root)=>`Divisibility operand ${root} must be a number`;
36
36
  const writeUnboundableMessage = (root)=>`Bounded expression ${root} must be a number, string or array`;
@@ -71,7 +71,7 @@ const distributeRange = (range, s)=>{
71
71
  if (resolution.object === true) {
72
72
  return false;
73
73
  }
74
- return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>(0, _genericsTs.hasKey)(branch, "subdomain") && (branch.subdomain === "Array" || branch.subdomain[0] === "Array"));
74
+ return (0, _genericsTs.listFrom)(resolution.object).every((branch)=>(0, _genericsTs.hasKey)(branch, "objectKind") && (branch.objectKind === "Array" || branch.objectKind[0] === "Array"));
75
75
  default:
76
76
  return false;
77
77
  }
@@ -10,15 +10,14 @@ const _predicateTs = require("../../nodes/predicate.js");
10
10
  const _errorsTs = require("../../utils/errors.js");
11
11
  const _genericsTs = require("../../utils/generics.js");
12
12
  const _definitionTs = require("../definition.js");
13
+ const _astTs = require("../string/ast.js");
13
14
  const parseKeyOfTuple = (def, ctx)=>{
14
15
  const resolution = ctx.type.meta.scope.resolveNode((0, _definitionTs.parseDefinition)(def[1], ctx));
15
16
  if (!(0, _predicateTs.resolutionExtendsDomain)(resolution, "object")) {
16
- return (0, _errorsTs.throwParseError)("never");
17
+ return (0, _errorsTs.throwParseError)((0, _astTs.writeImplicitNeverMessage)(ctx.path, "keyof"));
17
18
  }
18
19
  if (resolution.object === true) {
19
- return {
20
- string: true
21
- };
20
+ return (0, _astTs.writeImplicitNeverMessage)(ctx.path, "keyof");
22
21
  }
23
22
  const branchKeys = [];
24
23
  const objectBranches = (0, _genericsTs.listFrom)(resolution.object);
@@ -26,7 +25,7 @@ const parseKeyOfTuple = (def, ctx)=>{
26
25
  for(let i = 1; i < objectBranches.length; i++){
27
26
  const keys = getPropsFromBranch(objectBranches[i]);
28
27
  if (!keys.length) {
29
- return (0, _errorsTs.throwParseError)("never");
28
+ return (0, _astTs.writeImplicitNeverMessage)(ctx.path, "keyof");
30
29
  }
31
30
  branchKeys.push(keys);
32
31
  }
@@ -39,8 +38,8 @@ const parseKeyOfTuple = (def, ctx)=>{
39
38
  });
40
39
  }
41
40
  });
42
- return {
41
+ return result.length ? {
43
42
  string: result
44
- };
43
+ } : (0, _astTs.writeImplicitNeverMessage)(ctx.path, "keyof");
45
44
  };
46
45
  const getPropsFromBranch = (branch)=>(0, _genericsTs.hasKey)(branch, "props") ? Object.keys(branch.props) : [];
@@ -12,8 +12,8 @@ _export(exports, {
12
12
  parseMorphTuple: ()=>parseMorphTuple,
13
13
  writeMalformedMorphExpressionMessage: ()=>writeMalformedMorphExpressionMessage
14
14
  });
15
- const _domainsTs = require("../../utils/domains.js");
16
15
  const _errorsTs = require("../../utils/errors.js");
16
+ const _objectKindsTs = require("../../utils/objectKinds.js");
17
17
  const _serializeTs = require("../../utils/serialize.js");
18
18
  const _definitionTs = require("../definition.js");
19
19
  const parseMorphTuple = (def, ctx)=>{
@@ -33,7 +33,7 @@ const parseMorphTuple = (def, ctx)=>{
33
33
  morph
34
34
  };
35
35
  } else if (typeof predicate === "object") {
36
- result[domain] = (0, _domainsTs.hasSubdomain)(predicate, "Array") ? predicate.map((branch)=>applyMorph(branch, morph)) : applyMorph(predicate, morph);
36
+ result[domain] = (0, _objectKindsTs.isArray)(predicate) ? predicate.map((branch)=>applyMorph(branch, morph)) : applyMorph(predicate, morph);
37
37
  } else {
38
38
  (0, _errorsTs.throwInternalError)(`Unexpected predicate value for domain '${domain}': ${(0, _serializeTs.stringify)(predicate)}`);
39
39
  }
@@ -36,7 +36,7 @@ const parseTuple = (def, ctx)=>{
36
36
  }
37
37
  return {
38
38
  object: {
39
- subdomain: "Array",
39
+ objectKind: "Array",
40
40
  props,
41
41
  range: {
42
42
  comparator: "==",
@@ -12,9 +12,8 @@ _export(exports, {
12
12
  ArkTypeError: ()=>ArkTypeError,
13
13
  Problem: ()=>Problem,
14
14
  Problems: ()=>Problems,
15
- describeSubdomains: ()=>describeSubdomains,
15
+ describeDomains: ()=>describeDomains,
16
16
  describeBranches: ()=>describeBranches,
17
- subdomainDescriptions: ()=>subdomainDescriptions,
18
17
  DataWrapper: ()=>DataWrapper,
19
18
  defaultProblemWriters: ()=>defaultProblemWriters,
20
19
  compileProblemOptions: ()=>compileProblemOptions
@@ -22,6 +21,7 @@ _export(exports, {
22
21
  const _scannerTs = require("../parse/string/shift/scanner.js");
23
22
  const _domainsTs = require("../utils/domains.js");
24
23
  const _genericsTs = require("../utils/generics.js");
24
+ const _objectKindsTs = require("../utils/objectKinds.js");
25
25
  const _pathsTs = require("../utils/paths.js");
26
26
  const _serializeTs = require("../utils/serialize.js");
27
27
  var _definitions_code, _definitions_code1, _definitions_code2, _clausesByPath, _pathKey;
@@ -97,14 +97,14 @@ class ProblemArray extends Array {
97
97
  }
98
98
  const Problems = ProblemArray;
99
99
  const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
100
- const describeSubdomains = (subdomains)=>{
101
- if (subdomains.length === 1) {
102
- return subdomainDescriptions[subdomains[0]];
100
+ const describeDomains = (domains)=>{
101
+ if (domains.length === 1) {
102
+ return _domainsTs.domainDescriptions[domains[0]];
103
103
  }
104
- if (subdomains.length === 0) {
104
+ if (domains.length === 0) {
105
105
  return "never";
106
106
  }
107
- return describeBranches(subdomains.map((subdomain)=>subdomainDescriptions[subdomain]));
107
+ return describeBranches(domains.map((objectKind)=>_domainsTs.domainDescriptions[objectKind]));
108
108
  };
109
109
  const describeBranches = (descriptions)=>{
110
110
  let description = "";
@@ -117,23 +117,6 @@ const describeBranches = (descriptions)=>{
117
117
  description += ` or ${descriptions[descriptions.length - 1]}`;
118
118
  return description;
119
119
  };
120
- const subdomainDescriptions = {
121
- bigint: "a bigint",
122
- boolean: "boolean",
123
- null: "null",
124
- number: "a number",
125
- object: "an object",
126
- string: "a string",
127
- symbol: "a symbol",
128
- undefined: "undefined",
129
- Array: "an array",
130
- Function: "a function",
131
- Date: "a Date",
132
- RegExp: "a RegExp",
133
- Error: "an Error",
134
- Map: "a Map",
135
- Set: "a Set"
136
- };
137
120
  class DataWrapper {
138
121
  toString() {
139
122
  return (0, _serializeTs.stringify)(this.value);
@@ -141,18 +124,17 @@ class DataWrapper {
141
124
  get domain() {
142
125
  return (0, _domainsTs.domainOf)(this.value);
143
126
  }
144
- // TODO: object kind?
145
- get subdomain() {
146
- return (0, _domainsTs.subdomainOf)(this.value);
127
+ get objectKind() {
128
+ return (0, _objectKindsTs.objectKindOf)(this.value) ?? "non-object";
147
129
  }
148
130
  get size() {
149
- return (0, _domainsTs.sizeOf)(this.value);
131
+ return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
150
132
  }
151
133
  get units() {
152
- return (0, _domainsTs.unitsOf)(this.value);
134
+ return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
153
135
  }
154
136
  get className() {
155
- return (0, _domainsTs.classNameOf)(this.value);
137
+ return Object(this.value).constructor.name;
156
138
  }
157
139
  constructor(value){
158
140
  this.value = value;
@@ -179,13 +161,17 @@ const defaultProblemWriters = compileDefaultProblemWriters({
179
161
  was: (data)=>data.className
180
162
  },
181
163
  domain: {
182
- mustBe: (domain)=>subdomainDescriptions[domain],
164
+ mustBe: (domain)=>_domainsTs.domainDescriptions[domain],
183
165
  was: (data)=>data.domain
184
166
  },
185
167
  domainBranches: {
186
- mustBe: (domains)=>describeSubdomains(domains),
168
+ mustBe: (domains)=>describeDomains(domains),
187
169
  was: (data)=>data.domain
188
170
  },
171
+ objectKind: {
172
+ mustBe: (kind)=>_objectKindsTs.objectKindDescriptions[kind],
173
+ was: (data)=>data.objectKind
174
+ },
189
175
  missing: {
190
176
  mustBe: ()=>"defined",
191
177
  was: ()=>""
@@ -12,7 +12,7 @@ _export(exports, {
12
12
  TraversalState: ()=>TraversalState,
13
13
  traverse: ()=>traverse,
14
14
  checkEntries: ()=>checkEntries,
15
- checkSubdomain: ()=>checkSubdomain
15
+ checkObjectKind: ()=>checkObjectKind
16
16
  });
17
17
  const _discriminateJs = require("../nodes/discriminate.js");
18
18
  const _classJs = require("../nodes/rules/class.js");
@@ -23,6 +23,7 @@ const _rulesJs = require("../nodes/rules/rules.js");
23
23
  const _domainsJs = require("../utils/domains.js");
24
24
  const _errorsJs = require("../utils/errors.js");
25
25
  const _genericsJs = require("../utils/generics.js");
26
+ const _objectKindsJs = require("../utils/objectKinds.js");
26
27
  const _pathsJs = require("../utils/paths.js");
27
28
  const _serializeJs = require("../utils/serialize.js");
28
29
  const _problemsJs = require("./problems.js");
@@ -173,7 +174,6 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
173
174
  state.path.push(propKey);
174
175
  if (!(0, _genericsJs.hasKey)(data, propKey)) {
175
176
  if (propKind !== "optionalProps") {
176
- // TODO: update
177
177
  state.problems.add("missing", undefined, undefined);
178
178
  }
179
179
  } else {
@@ -185,18 +185,18 @@ const createPropChecker = (propKind)=>(data, props, state)=>{
185
185
  };
186
186
  const checkRequiredProps = createPropChecker("requiredProps");
187
187
  const checkOptionalProps = createPropChecker("optionalProps");
188
- const checkSubdomain = (data, rule, state)=>{
189
- const dataSubdomain = (0, _domainsJs.subdomainOf)(data);
188
+ const checkObjectKind = (data, rule, state)=>{
189
+ const dataObjectKind = (0, _objectKindsJs.objectKindOf)(data);
190
190
  if (typeof rule === "string") {
191
- if (dataSubdomain !== rule) {
192
- return state.problems.add("domain", data, rule);
191
+ if (dataObjectKind !== rule) {
192
+ return state.problems.add("objectKind", data, rule);
193
193
  }
194
194
  return;
195
195
  }
196
- if (dataSubdomain !== rule[0]) {
197
- return state.problems.add("domain", data, rule[0]);
196
+ if (dataObjectKind !== rule[0]) {
197
+ return state.problems.add("objectKind", data, rule[0]);
198
198
  }
199
- if (dataSubdomain === "Array" && typeof rule[2] === "number") {
199
+ if (dataObjectKind === "Array" && typeof rule[2] === "number") {
200
200
  const actual = data.length;
201
201
  const expected = rule[2];
202
202
  if (expected !== actual) {
@@ -207,7 +207,7 @@ const checkSubdomain = (data, rule, state)=>{
207
207
  });
208
208
  }
209
209
  }
210
- if (dataSubdomain === "Array" || dataSubdomain === "Set") {
210
+ if (dataObjectKind === "Array" || dataObjectKind === "Set") {
211
211
  let i = 0;
212
212
  for (const item of data){
213
213
  state.path.push(`${i}`);
@@ -216,7 +216,7 @@ const checkSubdomain = (data, rule, state)=>{
216
216
  i++;
217
217
  }
218
218
  } else {
219
- return (0, _errorsJs.throwInternalError)(`Unexpected subdomain entry ${(0, _serializeJs.stringify)(rule)}`);
219
+ return (0, _errorsJs.throwInternalError)(`Unexpected objectKind entry ${(0, _serializeJs.stringify)(rule)}`);
220
220
  }
221
221
  return true;
222
222
  };
@@ -236,7 +236,7 @@ const checkers = {
236
236
  state.problems.add("domain", data, domain);
237
237
  }
238
238
  },
239
- subdomain: checkSubdomain,
239
+ objectKind: checkObjectKind,
240
240
  bound: _rangeJs.checkBound,
241
241
  requiredProps: checkRequiredProps,
242
242
  optionalProps: checkOptionalProps,
@@ -11,57 +11,20 @@ function _export(target, all) {
11
11
  _export(exports, {
12
12
  hasDomain: ()=>hasDomain,
13
13
  domainOf: ()=>domainOf,
14
- subdomainOf: ()=>subdomainOf,
15
- hasSubdomain: ()=>hasSubdomain,
16
- sizeOf: ()=>sizeOf,
17
- unitsOf: ()=>unitsOf,
18
- classNameOf: ()=>classNameOf
14
+ domainDescriptions: ()=>domainDescriptions
19
15
  });
20
- const _genericsTs = require("./generics.js");
21
16
  const hasDomain = (data, domain)=>domainOf(data) === domain;
22
17
  const domainOf = (data)=>{
23
18
  const builtinType = typeof data;
24
19
  return builtinType === "object" ? data === null ? "null" : "object" : builtinType === "function" ? "object" : builtinType;
25
20
  };
26
- const objectSubdomains = {
27
- Array,
28
- Date,
29
- Error,
30
- Function,
31
- Map,
32
- RegExp,
33
- Set,
34
- object: Object
21
+ const domainDescriptions = {
22
+ bigint: "a bigint",
23
+ boolean: "boolean",
24
+ null: "null",
25
+ number: "a number",
26
+ object: "an object",
27
+ string: "a string",
28
+ symbol: "a symbol",
29
+ undefined: "undefined"
35
30
  };
36
- const subdomainOf = (data)=>{
37
- const domain = domainOf(data);
38
- if (domain !== "object") {
39
- return domain;
40
- }
41
- if (Array.isArray(data)) {
42
- return "Array";
43
- }
44
- // The raw result will be something like [object Date]
45
- const prototypeName = Object.prototype.toString.call(data).slice(8, -1);
46
- if ((0, _genericsTs.isKeyOf)(prototypeName, objectSubdomains)) {
47
- return data instanceof objectSubdomains[prototypeName] ? prototypeName : "object";
48
- }
49
- if (prototypeName.endsWith("Error")) {
50
- return data instanceof Error ? "Error" : "object";
51
- }
52
- return "object";
53
- };
54
- const hasSubdomain = (data, subdomain)=>subdomainOf(data) === subdomain;
55
- const sizeOf = (data)=>sizedSubdomains[subdomainOf(data)]?.(data) ?? 0;
56
- const sizedSubdomains = {
57
- number: (data)=>data,
58
- string: (data)=>data.length,
59
- Array: (data)=>data.length
60
- };
61
- const unitsOf = (data)=>sizeUnits[subdomainOf(data)] ?? "";
62
- const sizeUnits = {
63
- number: "",
64
- string: "characters",
65
- Array: "items"
66
- };
67
- const classNameOf = (data)=>Object(data).constructor.name;
@@ -16,8 +16,7 @@ _export(exports, {
16
16
  hasKey: ()=>hasKey,
17
17
  keyCount: ()=>keyCount,
18
18
  hasKeys: ()=>hasKeys,
19
- listFrom: ()=>listFrom,
20
- collapseIfSingleton: ()=>collapseIfSingleton
19
+ listFrom: ()=>listFrom
21
20
  });
22
21
  const _domainsTs = require("./domains.js");
23
22
  const asConst = (t)=>t;
@@ -33,4 +32,3 @@ const hasKeys = (value)=>(0, _domainsTs.hasDomain)(value, "object") ? Object.key
33
32
  const listFrom = (data)=>Array.isArray(data) ? data : [
34
33
  data
35
34
  ];
36
- const collapseIfSingleton = (array)=>array.length === 1 ? array[0] : array;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ objectKindOf: ()=>objectKindOf,
13
+ hasObjectKind: ()=>hasObjectKind,
14
+ isArray: ()=>isArray,
15
+ objectKindDescriptions: ()=>objectKindDescriptions
16
+ });
17
+ const _domainsTs = require("./domains.js");
18
+ // Built-in object constructors based on a subset of:
19
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
20
+ const defaultObjectKinds = {
21
+ Array,
22
+ Date,
23
+ Error,
24
+ Function,
25
+ Map,
26
+ RegExp,
27
+ Set,
28
+ Object,
29
+ String,
30
+ Number,
31
+ Boolean,
32
+ WeakMap,
33
+ WeakSet,
34
+ Promise
35
+ };
36
+ const objectKindOf = (data, kinds)=>{
37
+ if ((0, _domainsTs.domainOf)(data) !== "object") {
38
+ return undefined;
39
+ }
40
+ const kindSet = kinds ?? defaultObjectKinds;
41
+ let prototype = Object.getPrototypeOf(data);
42
+ while(prototype?.constructor && (!kindSet[prototype.constructor.name] || !(data instanceof kindSet[prototype.constructor.name]))){
43
+ prototype = Object.getPrototypeOf(prototype);
44
+ }
45
+ return prototype?.constructor?.name;
46
+ };
47
+ const hasObjectKind = (data, kind, kinds)=>objectKindOf(data, kinds) === kind;
48
+ const isArray = (data)=>Array.isArray(data);
49
+ const objectKindDescriptions = {
50
+ Object: "an object",
51
+ Array: "an array",
52
+ Function: "a function",
53
+ Date: "a Date",
54
+ RegExp: "a RegExp",
55
+ Error: "an Error",
56
+ Map: "a Map",
57
+ Set: "a Set",
58
+ String: "a String object",
59
+ Number: "a Number object",
60
+ Boolean: "a Boolean object",
61
+ Promise: "a Promise",
62
+ WeakMap: "a WeakMap",
63
+ WeakSet: "a WeakSet"
64
+ };
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ sizeOf: ()=>sizeOf,
13
+ unitsOf: ()=>unitsOf
14
+ });
15
+ const sizeOf = (data)=>typeof data === "string" || Array.isArray(data) ? data.length : typeof data === "number" ? data : 0;
16
+ const unitsOf = (data)=>typeof data === "string" ? "characters" : Array.isArray(data) ? "items" : "";
package/dist/mjs/main.js CHANGED
@@ -88,8 +88,8 @@ let anonymousScopeCount = 0;
88
88
  const scopeRegistry = {};
89
89
  var _resolutions = /*#__PURE__*/ new WeakMap(), _exports = /*#__PURE__*/ new WeakMap(), _anonymousTypeCount = /*#__PURE__*/ new WeakMap(), _register = /*#__PURE__*/ new WeakSet(), _cacheSpaces = /*#__PURE__*/ new WeakSet(), _initializeContext = /*#__PURE__*/ new WeakSet(), _compiled = /*#__PURE__*/ new WeakMap(), _resolveRecurse = /*#__PURE__*/ new WeakSet();
90
90
  export class Scope {
91
- createAnonymousTypeName() {
92
- return `anonymousType${++_classPrivateFieldUpdate(this, _anonymousTypeCount).value}`;
91
+ createAnonymousTypeSuffix() {
92
+ return ++_classPrivateFieldUpdate(this, _anonymousTypeCount).value;
93
93
  }
94
94
  get infer() {
95
95
  return chainableNoOpProxy;
@@ -148,6 +148,9 @@ export class Scope {
148
148
  _classPrivateFieldSet(this, _exports, new Cache());
149
149
  _classPrivateFieldSet(this, _anonymousTypeCount, 0);
150
150
  this.type = (def, opts = {})=>{
151
+ if (opts.name && this.aliases[opts.name]) {
152
+ return throwParseError(writeDuplicateAliasesMessage(opts.name));
153
+ }
151
154
  const result = initializeType(def, opts, this);
152
155
  const ctx = _classPrivateMethodGet(this, _initializeContext, initializeContext).call(this, result);
153
156
  result.node = this.resolveNode(parseDefinition(def, ctx));
@@ -170,7 +173,7 @@ export class Scope {
170
173
  }
171
174
  }
172
175
  function register(opts) {
173
- const name = opts.name ? scopeRegistry[opts.name] ? throwParseError(`A scope named '${opts.name}' already exists`) : opts.name : `anonymousScope${++anonymousScopeCount}`;
176
+ const name = opts.name ? scopeRegistry[opts.name] ? throwParseError(`A scope named '${opts.name}' already exists`) : opts.name : `scope${++anonymousScopeCount}`;
174
177
  scopeRegistry[name] = this;
175
178
  return name;
176
179
  }
@@ -178,7 +181,7 @@ function cacheSpaces(spaces, kind) {
178
181
  for (const space of spaces){
179
182
  for(const name in space){
180
183
  if (_classPrivateFieldGet(this, _resolutions).has(name) || name in this.aliases) {
181
- throwParseError(writeDuplicateAliasMessage(name));
184
+ throwParseError(writeDuplicateAliasesMessage(name));
182
185
  }
183
186
  _classPrivateFieldGet(this, _resolutions).set(name, space[name]);
184
187
  if (kind === "includes") {
@@ -222,10 +225,10 @@ function resolveRecurse(name, onUnresolvable, seen) {
222
225
  }
223
226
  // TODO: merge scope options
224
227
  const initializeType = (definition, opts, scope)=>{
225
- const name = opts.name ?? scope.createAnonymousTypeName();
228
+ const name = opts.name ?? "type";
226
229
  const meta = {
227
230
  name,
228
- id: `${scope.name}.${name}`,
231
+ id: `${scope.name}.${opts.name ? name : `type${scope.createAnonymousTypeSuffix()}`}`,
229
232
  definition,
230
233
  scope,
231
234
  problems: compileProblemOptions(opts.problems),
@@ -302,7 +305,7 @@ const always = {
302
305
  undefined: true
303
306
  };
304
307
  export const scope = (aliases, opts = {})=>new Scope(aliases, opts);
305
- const ts = scope({
308
+ const tsKeywords = scope({
306
309
  any: [
307
310
  "node",
308
311
  always
@@ -384,21 +387,130 @@ const ts = scope({
384
387
  {
385
388
  undefined: true
386
389
  }
387
- ],
388
- // TODO: Add remaining JS object types
390
+ ]
391
+ }, {
392
+ name: "ts",
393
+ standard: false
394
+ });
395
+ const tsKeywordsSpace = tsKeywords.compile();
396
+ const jsObjects = scope({
389
397
  Function: [
390
398
  "node",
391
399
  {
392
400
  object: {
393
- subdomain: "Function"
401
+ objectKind: "Function"
402
+ }
403
+ }
404
+ ],
405
+ Array: [
406
+ "node",
407
+ {
408
+ object: {
409
+ objectKind: "Array"
410
+ }
411
+ }
412
+ ],
413
+ Date: [
414
+ "node",
415
+ {
416
+ object: {
417
+ objectKind: "Date"
418
+ }
419
+ }
420
+ ],
421
+ Error: [
422
+ "node",
423
+ {
424
+ object: {
425
+ objectKind: "Error"
426
+ }
427
+ }
428
+ ],
429
+ Map: [
430
+ "node",
431
+ {
432
+ object: {
433
+ objectKind: "Map"
434
+ }
435
+ }
436
+ ],
437
+ RegExp: [
438
+ "node",
439
+ {
440
+ object: {
441
+ objectKind: "RegExp"
442
+ }
443
+ }
444
+ ],
445
+ Set: [
446
+ "node",
447
+ {
448
+ object: {
449
+ objectKind: "Set"
450
+ }
451
+ }
452
+ ],
453
+ Object: [
454
+ "node",
455
+ {
456
+ object: {
457
+ objectKind: "Object"
458
+ }
459
+ }
460
+ ],
461
+ String: [
462
+ "node",
463
+ {
464
+ object: {
465
+ objectKind: "String"
466
+ }
467
+ }
468
+ ],
469
+ Number: [
470
+ "node",
471
+ {
472
+ object: {
473
+ objectKind: "Number"
474
+ }
475
+ }
476
+ ],
477
+ Boolean: [
478
+ "node",
479
+ {
480
+ object: {
481
+ objectKind: "Boolean"
482
+ }
483
+ }
484
+ ],
485
+ WeakMap: [
486
+ "node",
487
+ {
488
+ object: {
489
+ objectKind: "WeakMap"
490
+ }
491
+ }
492
+ ],
493
+ WeakSet: [
494
+ "node",
495
+ {
496
+ object: {
497
+ objectKind: "WeakSet"
498
+ }
499
+ }
500
+ ],
501
+ Promise: [
502
+ "node",
503
+ {
504
+ object: {
505
+ objectKind: "Promise"
394
506
  }
395
507
  }
396
508
  ]
397
509
  }, {
398
- name: "ts",
510
+ name: "jsObjects",
399
511
  standard: false
400
512
  });
401
- const tsTypes = ts.compile();
513
+ const jsObjectsSpace = jsObjects.compile();
402
514
  const validation = scope({
403
515
  email: /^(.+)@(.+)\.(.+)$/,
404
516
  alphanumeric: /^[dA-Za-z]+$/,
@@ -417,26 +529,29 @@ const validation = scope({
417
529
  name: "validation",
418
530
  standard: false
419
531
  });
420
- const validationTypes = validation.compile();
532
+ const validationSpace = validation.compile();
421
533
  const standard = scope({}, {
422
534
  name: "standard",
423
535
  includes: [
424
- tsTypes,
425
- validationTypes
536
+ tsKeywordsSpace,
537
+ jsObjectsSpace,
538
+ validationSpace
426
539
  ],
427
540
  standard: false
428
541
  });
429
542
  const standardTypes = standard.compile();
430
543
  export const scopes = {
431
- ts,
544
+ tsKeywords,
545
+ jsObjects,
432
546
  validation,
433
547
  standard
434
548
  };
435
549
  export const spaces = {
436
- ts: tsTypes,
437
- validation: validationTypes,
550
+ tsKeywords: tsKeywordsSpace,
551
+ jsObjects: jsObjectsSpace,
552
+ validation: validationSpace,
438
553
  standard: standardTypes
439
554
  };
440
555
  export const type = scopes.standard.type;
441
- export const writeDuplicateAliasMessage = (name)=>`Alias '${name}' is already defined`;
556
+ export const writeDuplicateAliasesMessage = (name)=>`Alias '${name}' is already defined`;
442
557
  export const isType = (value)=>value?.infer === chainableNoOpProxy;