arktype 0.3.0 → 0.5.0

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 (126) hide show
  1. package/dist/api.cjs +9 -0
  2. package/dist/api.d.ts +10 -0
  3. package/dist/api.mjs +1 -0
  4. package/dist/cjs/nodes/branch.js +15 -6
  5. package/dist/cjs/nodes/compose.js +1 -1
  6. package/dist/cjs/nodes/discriminate.js +89 -21
  7. package/dist/cjs/nodes/node.js +11 -11
  8. package/dist/cjs/nodes/predicate.js +1 -1
  9. package/dist/cjs/nodes/rules/class.js +2 -2
  10. package/dist/cjs/nodes/rules/divisor.js +1 -1
  11. package/dist/cjs/nodes/rules/props.js +1 -2
  12. package/dist/cjs/nodes/rules/range.js +2 -2
  13. package/dist/cjs/nodes/rules/regex.js +1 -1
  14. package/dist/cjs/nodes/rules/rules.js +4 -0
  15. package/dist/cjs/parse/ast/ast.js +4 -0
  16. package/dist/cjs/parse/ast/bound.js +9 -0
  17. package/dist/cjs/parse/{tuple/utils.js → ast/distributableFunction.js} +1 -1
  18. package/dist/cjs/parse/ast/divisor.js +9 -0
  19. package/dist/cjs/parse/{string/ast.js → ast/intersection.js} +2 -8
  20. package/dist/cjs/parse/ast/keyof.js +109 -0
  21. package/dist/cjs/parse/{tuple → ast}/morph.js +1 -1
  22. package/dist/cjs/parse/{tuple → ast}/narrow.js +2 -2
  23. package/dist/cjs/parse/{tuple → ast}/tuple.js +11 -10
  24. package/dist/cjs/parse/ast/union.js +9 -0
  25. package/dist/cjs/parse/ast/utils.js +4 -0
  26. package/dist/cjs/parse/definition.js +12 -2
  27. package/dist/cjs/parse/string/reduce/dynamic.js +7 -7
  28. package/dist/cjs/parse/string/shift/operator/bounds.js +4 -4
  29. package/dist/cjs/parse/string/shift/operator/divisor.js +3 -3
  30. package/dist/cjs/parse/string/string.js +1 -1
  31. package/dist/cjs/scopes/expressions.js +29 -0
  32. package/dist/cjs/scopes/scope.js +59 -6
  33. package/dist/cjs/scopes/validation.js +16 -1
  34. package/dist/cjs/traverse/problems.js +78 -46
  35. package/dist/cjs/traverse/traverse.js +62 -58
  36. package/dist/cjs/utils/data.js +39 -0
  37. package/dist/cjs/utils/freeze.js +2 -3
  38. package/dist/cjs/utils/generics.js +20 -2
  39. package/dist/cjs/utils/numericLiterals.js +2 -14
  40. package/dist/mjs/nodes/branch.js +14 -5
  41. package/dist/mjs/nodes/compose.js +2 -2
  42. package/dist/mjs/nodes/discriminate.js +89 -21
  43. package/dist/mjs/nodes/node.js +8 -8
  44. package/dist/mjs/nodes/predicate.js +2 -2
  45. package/dist/mjs/nodes/rules/class.js +2 -2
  46. package/dist/mjs/nodes/rules/divisor.js +1 -1
  47. package/dist/mjs/nodes/rules/props.js +1 -2
  48. package/dist/mjs/nodes/rules/range.js +2 -2
  49. package/dist/mjs/nodes/rules/regex.js +1 -1
  50. package/dist/mjs/nodes/rules/rules.js +4 -0
  51. package/dist/mjs/parse/ast/ast.js +1 -0
  52. package/dist/mjs/parse/ast/bound.js +1 -0
  53. package/dist/mjs/parse/{tuple/utils.js → ast/distributableFunction.js} +2 -2
  54. package/dist/mjs/parse/ast/divisor.js +1 -0
  55. package/dist/mjs/parse/{string/ast.js → ast/intersection.js} +2 -5
  56. package/dist/mjs/parse/ast/keyof.js +101 -0
  57. package/dist/mjs/parse/{tuple → ast}/morph.js +1 -1
  58. package/dist/mjs/parse/ast/narrow.js +7 -0
  59. package/dist/mjs/parse/{tuple → ast}/tuple.js +12 -11
  60. package/dist/mjs/parse/ast/union.js +1 -0
  61. package/dist/mjs/parse/ast/utils.js +1 -0
  62. package/dist/mjs/parse/definition.js +12 -2
  63. package/dist/mjs/parse/string/reduce/dynamic.js +8 -8
  64. package/dist/mjs/parse/string/shift/operator/bounds.js +4 -4
  65. package/dist/mjs/parse/string/shift/operator/divisor.js +3 -3
  66. package/dist/mjs/parse/string/string.js +2 -2
  67. package/dist/mjs/scopes/expressions.js +9 -0
  68. package/dist/mjs/scopes/scope.js +58 -5
  69. package/dist/mjs/scopes/validation.js +16 -1
  70. package/dist/mjs/traverse/problems.js +76 -44
  71. package/dist/mjs/traverse/traverse.js +37 -34
  72. package/dist/mjs/utils/data.js +24 -0
  73. package/dist/mjs/utils/freeze.js +2 -3
  74. package/dist/mjs/utils/generics.js +18 -1
  75. package/dist/mjs/utils/numericLiterals.js +1 -1
  76. package/dist/tsconfig.temp.tsbuildinfo +1 -1
  77. package/dist/types/nodes/branch.d.ts +3 -12
  78. package/dist/types/nodes/discriminate.d.ts +6 -8
  79. package/dist/types/nodes/infer.d.ts +5 -5
  80. package/dist/types/nodes/node.d.ts +4 -13
  81. package/dist/types/nodes/predicate.d.ts +2 -29
  82. package/dist/types/nodes/rules/range.d.ts +2 -2
  83. package/dist/types/nodes/rules/rules.d.ts +1 -1
  84. package/dist/types/parse/ast/ast.d.ts +24 -0
  85. package/dist/types/parse/ast/bound.d.ts +10 -0
  86. package/dist/types/parse/{tuple/utils.d.ts → ast/distributableFunction.d.ts} +2 -3
  87. package/dist/types/parse/ast/divisor.d.ts +8 -0
  88. package/dist/types/parse/ast/intersection.d.ts +17 -0
  89. package/dist/types/parse/{tuple → ast}/keyof.d.ts +4 -4
  90. package/dist/types/parse/ast/morph.d.ts +18 -0
  91. package/dist/types/parse/ast/narrow.d.ts +21 -0
  92. package/dist/types/parse/ast/tuple.d.ts +40 -0
  93. package/dist/types/parse/ast/union.d.ts +12 -0
  94. package/dist/types/parse/ast/utils.d.ts +7 -0
  95. package/dist/types/parse/definition.d.ts +9 -7
  96. package/dist/types/parse/record.d.ts +2 -2
  97. package/dist/types/parse/string/reduce/shared.d.ts +5 -4
  98. package/dist/types/parse/string/reduce/static.d.ts +11 -6
  99. package/dist/types/parse/string/shift/operand/unenclosed.d.ts +1 -1
  100. package/dist/types/parse/string/shift/operator/bounds.d.ts +3 -2
  101. package/dist/types/parse/string/shift/operator/divisor.d.ts +5 -1
  102. package/dist/types/parse/string/shift/scanner.d.ts +1 -1
  103. package/dist/types/parse/string/string.d.ts +1 -2
  104. package/dist/types/scopes/expressions.d.ts +48 -0
  105. package/dist/types/scopes/scope.d.ts +17 -7
  106. package/dist/types/scopes/standard.d.ts +15 -0
  107. package/dist/types/scopes/type.d.ts +9 -5
  108. package/dist/types/scopes/validation.d.ts +4 -0
  109. package/dist/types/traverse/problems.d.ts +17 -24
  110. package/dist/types/traverse/traverse.d.ts +67 -37
  111. package/dist/types/utils/data.d.ts +13 -0
  112. package/dist/types/utils/domains.d.ts +1 -1
  113. package/dist/types/utils/generics.d.ts +14 -10
  114. package/dist/types/utils/numericLiterals.d.ts +16 -0
  115. package/dist/types/utils/paths.d.ts +2 -0
  116. package/package.json +1 -1
  117. package/dist/cjs/parse/tuple/keyof.js +0 -64
  118. package/dist/cjs/utils/size.js +0 -16
  119. package/dist/mjs/parse/tuple/keyof.js +0 -56
  120. package/dist/mjs/parse/tuple/narrow.js +0 -7
  121. package/dist/mjs/utils/size.js +0 -2
  122. package/dist/types/parse/string/ast.d.ts +0 -46
  123. package/dist/types/parse/tuple/morph.d.ts +0 -15
  124. package/dist/types/parse/tuple/narrow.d.ts +0 -13
  125. package/dist/types/parse/tuple/tuple.d.ts +0 -31
  126. package/dist/types/utils/size.d.ts +0 -3
@@ -1,6 +1,47 @@
1
+ function _checkPrivateRedeclaration(obj, privateCollection) {
2
+ if (privateCollection.has(obj)) {
3
+ throw new TypeError("Cannot initialize the same private elements twice on an object");
4
+ }
5
+ }
6
+ function _classApplyDescriptorGet(receiver, descriptor) {
7
+ if (descriptor.get) {
8
+ return descriptor.get.call(receiver);
9
+ }
10
+ return descriptor.value;
11
+ }
12
+ function _classApplyDescriptorSet(receiver, descriptor, value) {
13
+ if (descriptor.set) {
14
+ descriptor.set.call(receiver, value);
15
+ } else {
16
+ if (!descriptor.writable) {
17
+ throw new TypeError("attempted to set read only private field");
18
+ }
19
+ descriptor.value = value;
20
+ }
21
+ }
22
+ function _classExtractFieldDescriptor(receiver, privateMap, action) {
23
+ if (!privateMap.has(receiver)) {
24
+ throw new TypeError("attempted to " + action + " private field on non-instance");
25
+ }
26
+ return privateMap.get(receiver);
27
+ }
28
+ function _classPrivateFieldGet(receiver, privateMap) {
29
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get");
30
+ return _classApplyDescriptorGet(receiver, descriptor);
31
+ }
32
+ function _classPrivateFieldInit(obj, privateMap, value) {
33
+ _checkPrivateRedeclaration(obj, privateMap);
34
+ privateMap.set(obj, value);
35
+ }
36
+ function _classPrivateFieldSet(receiver, privateMap, value) {
37
+ var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set");
38
+ _classApplyDescriptorSet(receiver, descriptor, value);
39
+ return value;
40
+ }
1
41
  var _definitions_code, _definitions_code1;
2
42
  import { Scanner } from "../parse/string/shift/scanner.js";
3
- import { domainDescriptions, domainOf } from "../utils/domains.js";
43
+ import { DataWrapper } from "../utils/data.js";
44
+ import { domainDescriptions } from "../utils/domains.js";
4
45
  import { isWellFormedInteger } from "../utils/numericLiterals.js";
5
46
  import { objectKindDescriptions } from "../utils/objectKinds.js";
6
47
  import { Path } from "../utils/paths.js";
@@ -35,21 +76,29 @@ export class Problem {
35
76
  }
36
77
  }
37
78
  }
79
+ var _state = /*#__PURE__*/ new WeakMap();
38
80
  class ProblemArray extends Array {
39
- add(code, data, source, opts) {
81
+ add(code, source, opts) {
40
82
  // copy the path to avoid future mutations affecting it
41
- const path = opts?.path ?? Path.from(this.state.path);
42
- const problem = new Problem(code, path, data, source, this.state.getConfigForProblemCode(code));
43
- const pathKey = `${path}`;
83
+ const path = opts?.path ?? Path.from(_classPrivateFieldGet(this, _state).path);
84
+ const data = // we have to check for the presence of the key explicitly since the
85
+ // data could be undefined or null
86
+ opts && "data" in opts ? opts.data : _classPrivateFieldGet(this, _state).data;
87
+ const problem = new Problem(code, path, data, source, _classPrivateFieldGet(this, _state).getConfigForProblemCode(code));
88
+ this.addProblem(problem);
89
+ return problem;
90
+ }
91
+ addProblem(problem) {
92
+ const pathKey = `${problem.path}`;
44
93
  const existing = this.byPath[pathKey];
45
94
  if (existing) {
46
95
  if (existing.parts) {
47
96
  existing.parts.push(problem);
48
97
  } else {
49
- const problemIntersection = new Problem("multi", existing.path, data, [
98
+ const problemIntersection = new Problem("multi", existing.path, existing.data, [
50
99
  existing,
51
100
  problem
52
- ], this.state.getConfigForProblemCode("multi"));
101
+ ], _classPrivateFieldGet(this, _state).getConfigForProblemCode("multi"));
53
102
  const existingIndex = this.indexOf(existing);
54
103
  // If existing is found (which it always should be unless this was externally mutated),
55
104
  // replace it with the new problem intersection. In case it isn't for whatever reason,
@@ -61,7 +110,7 @@ class ProblemArray extends Array {
61
110
  this.byPath[pathKey] = problem;
62
111
  this.push(problem);
63
112
  }
64
- return false;
113
+ this.count++;
65
114
  }
66
115
  get summary() {
67
116
  return this.join("\n");
@@ -74,22 +123,26 @@ class ProblemArray extends Array {
74
123
  }
75
124
  constructor(state){
76
125
  super();
77
- this.state = state;
78
126
  this.byPath = {};
127
+ this.count = 0;
128
+ _classPrivateFieldInit(this, _state, {
129
+ writable: true,
130
+ value: void 0
131
+ });
132
+ _classPrivateFieldSet(this, _state, state);
79
133
  }
80
134
  }
81
135
  export const Problems = ProblemArray;
82
136
  const capitalize = (s)=>s[0].toUpperCase() + s.slice(1);
83
- export const describeDomains = (domains)=>{
84
- if (domains.length === 1) {
85
- return domainDescriptions[domains[0]];
86
- }
87
- if (domains.length === 0) {
137
+ export const domainsToDescriptions = (domains)=>domains.map((objectKind)=>domainDescriptions[objectKind]);
138
+ export const objectKindsToDescriptions = (kinds)=>kinds.map((objectKind)=>objectKindDescriptions[objectKind]);
139
+ export const describeBranches = (descriptions)=>{
140
+ if (descriptions.length === 0) {
88
141
  return "never";
89
142
  }
90
- return describeBranches(domains.map((objectKind)=>domainDescriptions[objectKind]));
91
- };
92
- export const describeBranches = (descriptions)=>{
143
+ if (descriptions.length === 1) {
144
+ return descriptions[0];
145
+ }
93
146
  let description = "";
94
147
  for(let i = 0; i < descriptions.length - 1; i++){
95
148
  description += descriptions[i];
@@ -100,26 +153,6 @@ export const describeBranches = (descriptions)=>{
100
153
  description += ` or ${descriptions[descriptions.length - 1]}`;
101
154
  return description;
102
155
  };
103
- export class DataWrapper {
104
- toString() {
105
- return stringify(this.value);
106
- }
107
- get domain() {
108
- return domainOf(this.value);
109
- }
110
- get size() {
111
- return typeof this.value === "string" || Array.isArray(this.value) ? this.value.length : typeof this.value === "number" ? this.value : 0;
112
- }
113
- get units() {
114
- return typeof this.value === "string" ? "characters" : Array.isArray(this.value) ? "items" : "";
115
- }
116
- get className() {
117
- return Object(this.value).constructor.name;
118
- }
119
- constructor(value){
120
- this.value = value;
121
- }
122
- }
123
156
  const writeDefaultReason = (mustBe, was)=>`must be ${mustBe}${was && ` (was ${was})`}`;
124
157
  const addDefaultContext = (reason, path)=>path.length === 0 ? capitalize(reason) : path.length === 1 && isWellFormedInteger(path[0]) ? `Item at index ${path[0]} ${reason}` : `${path} ${reason}`;
125
158
  const compileDefaultProblemWriters = (definitions)=>{
@@ -142,10 +175,6 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
142
175
  mustBe: (domain)=>domainDescriptions[domain],
143
176
  writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
144
177
  },
145
- domainBranches: {
146
- mustBe: (domains)=>describeDomains(domains),
147
- writeReason: (mustBe, data)=>writeDefaultReason(mustBe, data.domain)
148
- },
149
178
  missing: {
150
179
  mustBe: ()=>"defined",
151
180
  writeReason: (mustBe)=>writeDefaultReason(mustBe, "")
@@ -160,9 +189,6 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
160
189
  value: {
161
190
  mustBe: stringify
162
191
  },
163
- valueBranches: {
164
- mustBe: (values)=>describeBranches(values.map(stringify))
165
- },
166
192
  branches: {
167
193
  mustBe: (branchProblems)=>describeBranches(branchProblems.map((problem)=>`${problem.path} must be ${problem.parts ? describeBranches(problem.parts.map((part)=>part.mustBe)) : problem.mustBe}`)),
168
194
  writeReason: (mustBe, data)=>`${mustBe} (was ${data})`,
@@ -172,5 +198,11 @@ export const defaultProblemWriters = compileDefaultProblemWriters({
172
198
  mustBe: (problems)=>"• " + problems.map((_)=>_.mustBe).join("\n• "),
173
199
  writeReason: (mustBe, data)=>`${data} must be...\n${mustBe}`,
174
200
  addContext: (reason, path)=>path.length ? `At ${path}, ${reason}` : reason
201
+ },
202
+ mustBe: {
203
+ mustBe: (mustBe)=>mustBe
204
+ },
205
+ cases: {
206
+ mustBe: (cases)=>describeBranches(cases)
175
207
  }
176
208
  });
@@ -45,12 +45,11 @@ import { checkBound } from "../nodes/rules/range.js";
45
45
  import { checkRegex } from "../nodes/rules/regex.js";
46
46
  import { precedenceMap } from "../nodes/rules/rules.js";
47
47
  import { domainOf, hasDomain } from "../utils/domains.js";
48
- import { hasKey, keysOf } from "../utils/generics.js";
48
+ import { throwInternalError } from "../utils/errors.js";
49
+ import { hasKey, objectKeysOf } from "../utils/generics.js";
49
50
  import { getPath, Path } from "../utils/paths.js";
50
- import { deserializePrimitive } from "../utils/serialize.js";
51
- import { defaultProblemWriters, Problems } from "./problems.js";
51
+ import { defaultProblemWriters, domainsToDescriptions, objectKindsToDescriptions, Problem, Problems } from "./problems.js";
52
52
  var _seen = /*#__PURE__*/ new WeakMap();
53
- // TODO: include data wrapper in state
54
53
  export class TraversalState {
55
54
  getConfigForProblemCode(code) {
56
55
  if (!this.configs.length) {
@@ -70,7 +69,6 @@ export class TraversalState {
70
69
  traverseKey(key, node) {
71
70
  const lastData = this.data;
72
71
  this.data = this.data[key];
73
- // TODO: make path externally readonly
74
72
  this.path.push(key);
75
73
  const isValid = traverse(node, this);
76
74
  this.path.pop();
@@ -85,14 +83,15 @@ export class TraversalState {
85
83
  const data = this.data;
86
84
  const isObject = hasDomain(data, "object");
87
85
  if (isObject) {
88
- if (hasKey(_classPrivateFieldGet(this, _seen), id)) {
89
- if (_classPrivateFieldGet(this, _seen)[id].includes(data)) {
86
+ const seenByCurrentType = _classPrivateFieldGet(this, _seen)[id];
87
+ if (seenByCurrentType) {
88
+ if (seenByCurrentType.includes(data)) {
90
89
  // if data has already been checked by this alias as part of
91
90
  // a resolution higher up on the call stack, it must be valid
92
91
  // or we wouldn't be here
93
92
  return true;
94
93
  }
95
- _classPrivateFieldGet(this, _seen)[id].push(data);
94
+ seenByCurrentType.push(data);
96
95
  } else {
97
96
  _classPrivateFieldGet(this, _seen)[id] = [
98
97
  data
@@ -126,7 +125,7 @@ export class TraversalState {
126
125
  this.path = lastPath;
127
126
  this.problems = lastProblems;
128
127
  this.failFast = lastFailFast;
129
- return hasValidBranch || this.problems.add("branches", this.data, branchProblems);
128
+ return hasValidBranch || !this.problems.add("branches", branchProblems);
130
129
  }
131
130
  constructor(data, type){
132
131
  _classPrivateFieldInit(this, _seen, {
@@ -144,22 +143,11 @@ export class TraversalState {
144
143
  ] : [];
145
144
  }
146
145
  }
147
- export const traverse = (node, state)=>typeof node === "string" ? domainOf(state.data) === node || state.problems.add("domain", state.data, node) : checkEntries(node, state);
146
+ export const traverse = (node, state)=>typeof node === "string" ? domainOf(state.data) === node || !state.problems.add("domain", node) : checkEntries(node, state);
148
147
  export const checkEntries = (entries, state)=>{
149
148
  let isValid = true;
150
149
  for(let i = 0; i < entries.length; i++){
151
150
  const [k, v] = entries[i];
152
- if (k === "morph") {
153
- if (typeof v === "function") {
154
- // TODO: allow problem from morph
155
- state.data = v(state.data);
156
- } else {
157
- for (const morph of v){
158
- state.data = morph(state.data);
159
- }
160
- }
161
- return true;
162
- }
163
151
  const entryAllowsData = entryCheckers[k](v, state);
164
152
  isValid && (isValid = entryAllowsData);
165
153
  if (!isValid) {
@@ -180,19 +168,20 @@ export const checkRequiredProp = (prop, state)=>{
180
168
  if (prop[0] in state.data) {
181
169
  return state.traverseKey(prop[0], prop[1]);
182
170
  }
183
- return state.problems.add("missing", undefined, undefined, {
184
- path: state.path.concat(prop[0])
171
+ state.problems.add("missing", undefined, {
172
+ path: state.path.concat(prop[0]),
173
+ data: undefined
185
174
  });
175
+ return false;
186
176
  };
187
177
  const entryCheckers = {
188
178
  regex: checkRegex,
189
179
  divisor: checkDivisor,
190
180
  domains: (domains, state)=>{
191
181
  const entries = domains[domainOf(state.data)];
192
- return entries ? checkEntries(entries, state) : state.problems.add("domainBranches", state.data, keysOf(domains));
182
+ return entries ? checkEntries(entries, state) : !state.problems.add("cases", domainsToDescriptions(objectKeysOf(domains)));
193
183
  },
194
- // TODO: remove data from problem params
195
- domain: (domain, state)=>domainOf(state.data) === domain || state.problems.add("domain", state.data, domain),
184
+ domain: (domain, state)=>domainOf(state.data) === domain || !state.problems.add("domain", domain),
196
185
  bound: checkBound,
197
186
  optionalProp: (prop, state)=>{
198
187
  if (prop[0] in state.data) {
@@ -206,7 +195,8 @@ const entryCheckers = {
206
195
  prerequisiteProp: checkRequiredProp,
207
196
  indexProp: (node, state)=>{
208
197
  if (!Array.isArray(state.data)) {
209
- return state.problems.add("class", state.data, "Array");
198
+ state.problems.add("class", "Array");
199
+ return false;
210
200
  }
211
201
  let isValid = true;
212
202
  for(let i = 0; i < state.data.length; i++){
@@ -224,17 +214,17 @@ const entryCheckers = {
224
214
  if (hasKey(rule.cases, caseKey)) {
225
215
  return checkEntries(rule.cases[caseKey], state);
226
216
  }
227
- const caseKeys = keysOf(rule.cases);
217
+ const caseKeys = objectKeysOf(rule.cases);
228
218
  const missingCasePath = state.path.concat(rule.path);
229
- return rule.kind === "value" ? state.problems.add("valueBranches", dataAtPath, caseKeys.map((k)=>deserializePrimitive(k)), {
230
- path: missingCasePath
231
- }) : state.problems.add("domainBranches", dataAtPath, caseKeys, {
232
- path: missingCasePath
219
+ const caseDescriptions = rule.kind === "value" ? caseKeys : rule.kind === "domain" ? domainsToDescriptions(caseKeys) : rule.kind === "class" ? objectKindsToDescriptions(caseKeys) : throwInternalError(`Unexpectedly encountered rule kind '${rule.kind}' during traversal`);
220
+ state.problems.add("cases", caseDescriptions, {
221
+ path: missingCasePath,
222
+ data: dataAtPath
233
223
  });
224
+ return false;
234
225
  },
235
226
  alias: (name, state)=>state.traverseResolution(name),
236
227
  class: checkClass,
237
- // TODO: fix
238
228
  narrow: (narrow, state)=>narrow(state.data, state.problems),
239
229
  config: ({ config , node }, state)=>{
240
230
  state.configs.push(config);
@@ -242,5 +232,18 @@ const entryCheckers = {
242
232
  state.configs.pop();
243
233
  return result;
244
234
  },
245
- value: (value, state)=>state.data === value || state.problems.add("value", state.data, value)
235
+ value: (value, state)=>state.data === value || !state.problems.add("value", value),
236
+ morph: (morph, state)=>{
237
+ const out = morph(state.data, state.problems);
238
+ if (state.problems.length) {
239
+ return false;
240
+ }
241
+ if (out instanceof Problem) {
242
+ // if a problem was returned from the morph but not added, add it
243
+ state.problems.addProblem(out);
244
+ return false;
245
+ }
246
+ state.data = out;
247
+ return true;
248
+ }
246
249
  };
@@ -0,0 +1,24 @@
1
+ import { domainOf } from "./domains.js";
2
+ import { stringify } from "./serialize.js";
3
+ export const sizeOf = (data)=>typeof data === "string" || Array.isArray(data) ? data.length : typeof data === "number" ? data : 0;
4
+ export const unitsOf = (data)=>typeof data === "string" ? "characters" : Array.isArray(data) ? "items" : "";
5
+ export class DataWrapper {
6
+ toString() {
7
+ return stringify(this.value);
8
+ }
9
+ get domain() {
10
+ return domainOf(this.value);
11
+ }
12
+ get size() {
13
+ return sizeOf(this.value);
14
+ }
15
+ get units() {
16
+ return unitsOf(this.value);
17
+ }
18
+ get className() {
19
+ return Object(this.value).constructor.name;
20
+ }
21
+ constructor(value){
22
+ this.value = value;
23
+ }
24
+ }
@@ -1,8 +1,7 @@
1
1
  export const deepFreeze = (value)=>Object.isFrozen(value) ? value : Array.isArray(value) ? Object.freeze(value.map(deepFreeze)) : deepFreezeDictionary(value);
2
2
  const deepFreezeDictionary = (value)=>{
3
- const result = {};
4
3
  for(const k in value){
5
- result[k] = deepFreeze(value[k]);
4
+ deepFreeze(value[k]);
6
5
  }
7
- return Object.freeze(result);
6
+ return value;
8
7
  };
@@ -2,7 +2,24 @@ import { hasDomain } from "./domains.js";
2
2
  export const asConst = (t)=>t;
3
3
  export const isKeyOf = (k, obj)=>k in obj;
4
4
  export const entriesOf = (o)=>Object.entries(o);
5
- export const keysOf = (o)=>Object.keys(o);
5
+ export const objectKeysOf = (o)=>Object.keys(o);
6
+ /** Mimics output of TS's keyof operator at runtime */ export const prototypeKeysOf = (value)=>{
7
+ const result = [];
8
+ while(value !== Object.prototype && value !== null && value !== undefined){
9
+ for (const k of Object.getOwnPropertyNames(value)){
10
+ if (!result.includes(k)) {
11
+ result.push(k);
12
+ }
13
+ }
14
+ for (const symbol of Object.getOwnPropertySymbols(value)){
15
+ if (!result.includes(symbol)) {
16
+ result.push(symbol);
17
+ }
18
+ }
19
+ value = Object.getPrototypeOf(value);
20
+ }
21
+ return result;
22
+ };
6
23
  export const hasKey = (o, k)=>{
7
24
  const valueAtKey = o?.[k];
8
25
  return valueAtKey !== undefined && valueAtKey !== null;
@@ -12,7 +12,7 @@ import { throwParseError } from "./errors.js";
12
12
  * 2. The first digit of the value must not be 0, unless the entire integer portion is 0
13
13
  * 3. If the value includes a decimal, its last digit may not be 0
14
14
  * 4. The value may not be "-0"
15
- */ const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
15
+ */ export const wellFormedNumberMatcher = /^(?!^-0$)-?(?:0|[1-9]\d*)(?:\.\d*[1-9])?$/;
16
16
  const isWellFormedNumber = (s)=>wellFormedNumberMatcher.test(s);
17
17
  const numberLikeMatcher = /^-?\d*\.?\d*$/;
18
18
  const isNumberLike = (s)=>s.length !== 0 && numberLikeMatcher.test(s);