kanagata 0.3.2 → 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.
- package/README.md +17 -5
- package/dist/cli.js +18 -3
- package/dist/client.d.ts +63 -35
- package/dist/emit.d.ts +18 -4
- package/dist/emit.js +171 -28
- package/dist/index.d.ts +6 -1
- package/dist/index.js +8 -3
- package/dist/operation.d.ts +17 -2
- package/dist/operation.js +214 -71
- package/dist/pointer.d.ts +51 -8
- package/dist/pointer.js +136 -5
- package/dist/schema.d.ts +11 -3
- package/dist/schema.js +1528 -138
- package/dist/template.d.ts +12 -0
- package/dist/template.js +349 -0
- package/dist/ty.d.ts +28 -4
- package/dist/ty.js +367 -60
- package/package.json +1 -1
- package/src/client.ts +82 -54
package/dist/ty.js
CHANGED
|
@@ -33,7 +33,10 @@ export const prop = (key, ty) => ({
|
|
|
33
33
|
ty,
|
|
34
34
|
optional: false,
|
|
35
35
|
});
|
|
36
|
-
/**
|
|
36
|
+
/**
|
|
37
|
+
* Structural equality. References to two views of a schema differ only where `views` says
|
|
38
|
+
* the schema's views differ: always (`true`), never (`false`), or for the schemas in it.
|
|
39
|
+
*/
|
|
37
40
|
export function equal(a, b, views = true) {
|
|
38
41
|
if (a === b)
|
|
39
42
|
return true;
|
|
@@ -51,11 +54,13 @@ export function equal(a, b, views = true) {
|
|
|
51
54
|
});
|
|
52
55
|
switch (a.kind) {
|
|
53
56
|
case "lit":
|
|
54
|
-
|
|
57
|
+
case "template":
|
|
58
|
+
return b.kind === a.kind && a.text === b.text;
|
|
55
59
|
case "ref":
|
|
56
60
|
return (b.kind === "ref" &&
|
|
57
61
|
a.schema === b.schema &&
|
|
58
|
-
(
|
|
62
|
+
(a.view === b.view ||
|
|
63
|
+
(typeof views === "boolean" ? !views : !views.has(a.schema))));
|
|
59
64
|
case "array":
|
|
60
65
|
return b.kind === "array" && eq(a.elem, b.elem);
|
|
61
66
|
case "tuple":
|
|
@@ -67,8 +72,15 @@ export function equal(a, b, views = true) {
|
|
|
67
72
|
: b.rest !== undefined && eq(a.rest, b.rest)));
|
|
68
73
|
case "object":
|
|
69
74
|
return b.kind === "object" && props(a.props, b.props);
|
|
70
|
-
case "map":
|
|
71
|
-
|
|
75
|
+
case "map": {
|
|
76
|
+
if (b.kind !== "map" || !props(a.props, b.props))
|
|
77
|
+
return false;
|
|
78
|
+
const keyed = (m) => [
|
|
79
|
+
...(m.keyed ?? []).flatMap((k) => [k.key, k.ty]),
|
|
80
|
+
...(m.index === undefined ? [] : [STRING, m.index]),
|
|
81
|
+
];
|
|
82
|
+
return list(keyed(a), keyed(b));
|
|
83
|
+
}
|
|
72
84
|
case "union":
|
|
73
85
|
case "inter":
|
|
74
86
|
return b.kind === a.kind && list(a.items, b.items);
|
|
@@ -100,7 +112,10 @@ export function refs(t, out = new Set()) {
|
|
|
100
112
|
break;
|
|
101
113
|
case "map":
|
|
102
114
|
props(t.props);
|
|
103
|
-
|
|
115
|
+
for (const k of t.keyed ?? [])
|
|
116
|
+
refs(k.ty, out);
|
|
117
|
+
if (t.index !== undefined)
|
|
118
|
+
refs(t.index, out);
|
|
104
119
|
break;
|
|
105
120
|
case "union":
|
|
106
121
|
case "inter":
|
|
@@ -145,35 +160,76 @@ export function admitsAny(t, resolve) {
|
|
|
145
160
|
* admits any value (`any`): such a type admits `undefined` in TypeScript, which would
|
|
146
161
|
* stay in the type another member gives (`{ a?: string } & { a: unknown }` reads `a`
|
|
147
162
|
* as `string | undefined`), while JSON has no `undefined`. `unknown` becomes `{} | null`.
|
|
163
|
+
* A type with no such key is kept as it is.
|
|
148
164
|
*/
|
|
149
165
|
export function presentKeys(t, any) {
|
|
150
|
-
|
|
166
|
+
// Each part gone through; unchanged, the type is itself, and what is known of it stays.
|
|
167
|
+
let changed = false;
|
|
168
|
+
// The items of a long tuple are one type, gone through once.
|
|
169
|
+
let last;
|
|
170
|
+
let lastOut = t;
|
|
171
|
+
const go = (u) => {
|
|
172
|
+
if (u !== last) {
|
|
173
|
+
last = u;
|
|
174
|
+
lastOut = presentKeys(u, any);
|
|
175
|
+
}
|
|
176
|
+
if (lastOut !== u)
|
|
177
|
+
changed = true;
|
|
178
|
+
return lastOut;
|
|
179
|
+
};
|
|
151
180
|
const props = (ps) => ps.map((p) => {
|
|
152
181
|
const ty = go(p.ty);
|
|
153
|
-
|
|
182
|
+
if (!p.present || !any(ty))
|
|
183
|
+
return ty === p.ty ? p : { ...p, ty };
|
|
184
|
+
changed = true;
|
|
185
|
+
return { ...p, ty: inter([ty, ANY_JSON]) };
|
|
154
186
|
});
|
|
187
|
+
let out = t;
|
|
155
188
|
switch (t.kind) {
|
|
156
|
-
case "array":
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
189
|
+
case "array": {
|
|
190
|
+
const elem = go(t.elem);
|
|
191
|
+
if (changed)
|
|
192
|
+
out = { kind: "array", elem };
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
case "tuple": {
|
|
196
|
+
const items = t.items.map(go);
|
|
197
|
+
const rest = t.rest === undefined ? undefined : go(t.rest);
|
|
198
|
+
if (changed)
|
|
199
|
+
out =
|
|
200
|
+
rest === undefined
|
|
201
|
+
? { kind: "tuple", items, min: t.min }
|
|
202
|
+
: { kind: "tuple", items, min: t.min, rest };
|
|
203
|
+
break;
|
|
204
|
+
}
|
|
205
|
+
case "object": {
|
|
206
|
+
const ps = props(t.props);
|
|
207
|
+
if (changed)
|
|
208
|
+
out = { kind: "object", props: ps };
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "map": {
|
|
212
|
+
const ps = props(t.props);
|
|
213
|
+
const keyed = t.keyed?.map((k) => ({ key: k.key, ty: go(k.ty) }));
|
|
214
|
+
const index = t.index === undefined ? undefined : go(t.index);
|
|
215
|
+
if (changed)
|
|
216
|
+
out = {
|
|
217
|
+
kind: "map",
|
|
218
|
+
props: ps,
|
|
219
|
+
...(keyed === undefined ? {} : { keyed }),
|
|
220
|
+
...(index === undefined ? {} : { index }),
|
|
166
221
|
};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
case "map":
|
|
170
|
-
return { kind: "map", props: props(t.props), index: go(t.index) };
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
171
224
|
case "union":
|
|
172
|
-
case "inter":
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
225
|
+
case "inter": {
|
|
226
|
+
const items = t.items.map(go);
|
|
227
|
+
if (changed)
|
|
228
|
+
out = { kind: t.kind, items };
|
|
229
|
+
break;
|
|
230
|
+
}
|
|
176
231
|
}
|
|
232
|
+
return out;
|
|
177
233
|
}
|
|
178
234
|
/**
|
|
179
235
|
* Collects distinct members: literals by their text through a Set (enums can have
|
|
@@ -211,11 +267,15 @@ export function union(items) {
|
|
|
211
267
|
let out = members.out;
|
|
212
268
|
if (out.some((t) => t.kind === "unknown"))
|
|
213
269
|
return UNKNOWN;
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
270
|
+
// A literal is no kind type; a union of thousands of them has none to read.
|
|
271
|
+
const kinds = out.flatMap((t) => t.kind === "lit" ? [] : (kindsOf(t) ?? []));
|
|
272
|
+
// `number` admits every whole number; a primitive admits its literals, `string` its templates.
|
|
273
|
+
if (kinds.length > 0)
|
|
274
|
+
out = out.filter((t) => t.kind === "integer"
|
|
275
|
+
? !kinds.includes("number")
|
|
276
|
+
: t.kind === "template"
|
|
277
|
+
? !kinds.includes("string")
|
|
278
|
+
: t.kind !== "lit" || !admits(kinds, t));
|
|
219
279
|
if (out.length === 0)
|
|
220
280
|
return NEVER;
|
|
221
281
|
const [only] = out;
|
|
@@ -238,40 +298,173 @@ export function inter(items) {
|
|
|
238
298
|
};
|
|
239
299
|
items.forEach(push);
|
|
240
300
|
const out = members.out;
|
|
241
|
-
|
|
301
|
+
// One type meets nothing: it is as it was built.
|
|
302
|
+
if (out.length === 1)
|
|
303
|
+
return out[0];
|
|
304
|
+
const arrays = out.filter(isArrayTy);
|
|
305
|
+
if (arrays.length > 1)
|
|
306
|
+
return inter([...out.filter((t) => !isArrayTy(t)), meetArrays(arrays)]);
|
|
307
|
+
// A union with arrays meets the other arrays member by member, so that their items
|
|
308
|
+
// read as both say: `(string | unknown[]) & (string | string[])` is `string | string[]`.
|
|
309
|
+
const arrayish = out.filter(hasArray);
|
|
310
|
+
if (arrayish.length > 1 && arrayish.some((t) => t.kind === "union")) {
|
|
311
|
+
const combos = arrayish.reduce((acc, t) => acc.flatMap((c) => (t.kind === "union" ? t.items : [t]).map((m) => [...c, m])), [[]]);
|
|
312
|
+
return inter([
|
|
313
|
+
...out.filter((t) => !arrayish.includes(t)),
|
|
314
|
+
union(combos.map((c) => inter(c))),
|
|
315
|
+
]);
|
|
316
|
+
}
|
|
317
|
+
const merged = mergeObjects(out);
|
|
318
|
+
if (merged !== out)
|
|
319
|
+
return inter(merged);
|
|
320
|
+
// A union of object types meets the object type next to it member by member, each
|
|
321
|
+
// one object type: `{ a?: A } & ({ b: B } | { c: C })` is `{ a?: A; b: B } | { a?: A; c: C }`.
|
|
322
|
+
const objects = out.find((t) => t.kind === "object");
|
|
323
|
+
const alternatives = out.find((t) => t.kind === "union" && t.items.every((m) => m.kind === "object"));
|
|
324
|
+
if (objects !== undefined && alternatives !== undefined) {
|
|
325
|
+
const rest = out.filter((t) => t !== alternatives && t !== objects);
|
|
326
|
+
return inter([
|
|
327
|
+
...rest,
|
|
328
|
+
union(alternatives.items.map((m) => inter([objects, m]))),
|
|
329
|
+
]);
|
|
330
|
+
}
|
|
331
|
+
const narrowed = prune(narrow(out));
|
|
242
332
|
// Narrowing can make members equal (`"a" & ("a" | 1) & string`); normalize again.
|
|
243
333
|
// Each round drops a member or shrinks a set of values, so this ends.
|
|
244
334
|
if (narrowed !== out)
|
|
245
335
|
return inter(narrowed);
|
|
246
336
|
if (out.some((t) => t.kind === "never"))
|
|
247
337
|
return NEVER;
|
|
338
|
+
// Members of kinds no value has both of meet nowhere: `null & string[]`.
|
|
339
|
+
const kinds = out.map(kindSet).filter((k) => k !== undefined);
|
|
340
|
+
if (kinds.some((a, i) => kinds
|
|
341
|
+
.slice(i + 1)
|
|
342
|
+
.some((b) => !a.some((x) => b.some((y) => meets(x, y))))))
|
|
343
|
+
return NEVER;
|
|
248
344
|
if (out.length === 0)
|
|
249
345
|
return UNKNOWN;
|
|
250
|
-
|
|
251
|
-
return out.length === 1 && only !== undefined
|
|
252
|
-
? only
|
|
253
|
-
: { kind: "inter", items: out };
|
|
346
|
+
return { kind: "inter", items: out };
|
|
254
347
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
348
|
+
const isArrayTy = (t) => t.kind === "array" || t.kind === "tuple";
|
|
349
|
+
/** An array or tuple, or a union with one. */
|
|
350
|
+
const hasArray = (t) => isArrayTy(t) || (t.kind === "union" && t.items.some(isArrayTy));
|
|
351
|
+
/**
|
|
352
|
+
* Object types are one object type: each property's types met, required where one
|
|
353
|
+
* requires it (so that the items of arrays read as both say: TypeScript reads an item
|
|
354
|
+
* of `A[] & B[]` as `A`). A required property that admits nothing admits no object.
|
|
355
|
+
* Returns `items` itself when there is one object type at most.
|
|
356
|
+
*/
|
|
357
|
+
function mergeObjects(items) {
|
|
358
|
+
const objects = items.filter((t) => t.kind === "object");
|
|
359
|
+
if (objects.length < 2)
|
|
360
|
+
return items;
|
|
361
|
+
const props = [];
|
|
362
|
+
for (const o of objects)
|
|
363
|
+
for (const p of o.props) {
|
|
364
|
+
const i = props.findIndex((q) => q.key === p.key);
|
|
365
|
+
const q = props[i];
|
|
366
|
+
if (q === undefined) {
|
|
367
|
+
props.push(p);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const met = {
|
|
371
|
+
...q,
|
|
372
|
+
ty: inter([q.ty, p.ty]),
|
|
373
|
+
optional: q.optional && p.optional,
|
|
374
|
+
};
|
|
375
|
+
if (p.present)
|
|
376
|
+
met.present = true;
|
|
377
|
+
if (met.doc === undefined && p.doc !== undefined)
|
|
378
|
+
met.doc = p.doc;
|
|
379
|
+
props[i] = met;
|
|
380
|
+
}
|
|
381
|
+
if (props.some((p) => !p.optional && p.ty.kind === "never"))
|
|
382
|
+
return [NEVER];
|
|
383
|
+
return [
|
|
384
|
+
...items.filter((t) => t.kind !== "object"),
|
|
385
|
+
{ kind: "object", props },
|
|
386
|
+
];
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Arrays and tuples meet item by item: `string[]` and `[unknown, unknown, ...unknown[]]`
|
|
390
|
+
* are `[string, string, ...string[]]`. TypeScript reads an item of their intersection
|
|
391
|
+
* as the tuple's alone (`unknown`). A tuple without `rest` closes the others at its
|
|
392
|
+
* length, and requiring more items than that admits nothing.
|
|
393
|
+
*/
|
|
394
|
+
function meetArrays(arrays) {
|
|
395
|
+
const rows = arrays.map((t) => t.kind === "tuple" ? t : { items: [], min: 0, rest: t.elem });
|
|
396
|
+
const min = Math.max(...rows.map((r) => r.min));
|
|
397
|
+
const closed = rows.filter((r) => r.rest === undefined);
|
|
398
|
+
const length = Math.min(Math.max(...rows.map((r) => r.items.length)), ...closed.map((r) => r.items.length));
|
|
399
|
+
if (min > length)
|
|
400
|
+
return NEVER;
|
|
401
|
+
// Below `length`, a closed tuple has the item and an open one its item or `rest`.
|
|
402
|
+
const items = Array.from({ length }, (_, i) => inter(rows.map((r) => r.items[i] ?? r.rest)));
|
|
403
|
+
if (items.slice(0, min).some((t) => t.kind === "never"))
|
|
404
|
+
return NEVER;
|
|
405
|
+
if (closed.length > 0)
|
|
406
|
+
return { kind: "tuple", items, min };
|
|
407
|
+
const rest = inter(rows.map((r) => r.rest));
|
|
408
|
+
return length === 0
|
|
409
|
+
? { kind: "array", elem: rest }
|
|
410
|
+
: { kind: "tuple", items, min, rest };
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* `f`, remembered for each union and intersection: a type is never changed once built,
|
|
414
|
+
* and the normalization asks again for the same ones, unions of thousands of literals
|
|
415
|
+
* among them. Other types cost less to read again than to look up.
|
|
416
|
+
*/
|
|
417
|
+
function remembered(f) {
|
|
418
|
+
const known = new WeakMap();
|
|
419
|
+
return (t) => {
|
|
420
|
+
if (t.kind !== "union" && t.kind !== "inter")
|
|
421
|
+
return f(t);
|
|
422
|
+
if (known.has(t))
|
|
423
|
+
return known.get(t);
|
|
424
|
+
const r = f(t);
|
|
425
|
+
known.set(t, r);
|
|
426
|
+
return r;
|
|
269
427
|
};
|
|
270
|
-
const kinds = (t.kind === "union" ? t.items : [t]).map(one);
|
|
271
|
-
return kinds.every((k) => k !== undefined)
|
|
272
|
-
? kinds
|
|
273
|
-
: undefined;
|
|
274
428
|
}
|
|
429
|
+
/** The kinds of value an "any value of these kinds" type admits: `string`, `string | null`, `Record`, `unknown[]`. */
|
|
430
|
+
const kindsOf = remembered((t) => kindsBy(t, (m) => {
|
|
431
|
+
if (m.kind === "string" ||
|
|
432
|
+
m.kind === "number" ||
|
|
433
|
+
m.kind === "integer" ||
|
|
434
|
+
m.kind === "boolean" ||
|
|
435
|
+
m.kind === "null")
|
|
436
|
+
return m.kind;
|
|
437
|
+
if (m.kind === "record")
|
|
438
|
+
return "object";
|
|
439
|
+
if (m.kind === "array" && m.elem.kind === "unknown")
|
|
440
|
+
return "array";
|
|
441
|
+
return undefined;
|
|
442
|
+
}));
|
|
443
|
+
/** The kind `one` gives each member of a union (or the type), if it gives every one a kind. */
|
|
444
|
+
function kindsBy(t, one) {
|
|
445
|
+
if (t.kind !== "union") {
|
|
446
|
+
const k = one(t);
|
|
447
|
+
return k === undefined ? undefined : ONE_KIND[k];
|
|
448
|
+
}
|
|
449
|
+
const kinds = [];
|
|
450
|
+
for (const m of t.items) {
|
|
451
|
+
const k = one(m);
|
|
452
|
+
if (k === undefined)
|
|
453
|
+
return undefined;
|
|
454
|
+
kinds.push(k);
|
|
455
|
+
}
|
|
456
|
+
return kinds;
|
|
457
|
+
}
|
|
458
|
+
/** Each kind alone, one list for all that ask (none changes it). */
|
|
459
|
+
const ONE_KIND = {
|
|
460
|
+
string: ["string"],
|
|
461
|
+
number: ["number"],
|
|
462
|
+
integer: ["integer"],
|
|
463
|
+
boolean: ["boolean"],
|
|
464
|
+
null: ["null"],
|
|
465
|
+
object: ["object"],
|
|
466
|
+
array: ["array"],
|
|
467
|
+
};
|
|
275
468
|
/**
|
|
276
469
|
* The kind of value a type that lists values admits: a literal by its notation, any
|
|
277
470
|
* tuple as an array, any object or map type as an object (not only those from
|
|
@@ -296,19 +489,26 @@ function valueKind(t) {
|
|
|
296
489
|
* within `number`). Sets of literals meet each other (numbers by value). Then the
|
|
297
490
|
* sets of values keep what K allows, and the kind types go; with no values, one
|
|
298
491
|
* type says what K is. So `type` next to `enum` gives `"a"`, not `string & "a"`,
|
|
299
|
-
* and contradictions (`number & string`, `"a" & "b"`) are `never`.
|
|
492
|
+
* and contradictions (`number & string`, `"a" & "b"`) are `never`. A template
|
|
493
|
+
* (from `pattern`) is some strings: it meets kinds as `string` does and stands for them,
|
|
494
|
+
* and the values next to it keep the strings it admits.
|
|
300
495
|
* Returns `items` itself when nothing changes.
|
|
301
496
|
*/
|
|
302
497
|
function narrow(items) {
|
|
303
|
-
const
|
|
498
|
+
const strings = items.map(isStrings);
|
|
499
|
+
const templates = items.filter((_, i) => strings[i]);
|
|
500
|
+
const kindSets = items.map((t, i) => (strings[i] ? undefined : kindsOf(t)));
|
|
304
501
|
const present = kindSets.filter((k) => k !== undefined);
|
|
502
|
+
if (templates.length > 0)
|
|
503
|
+
present.push(["string"]);
|
|
305
504
|
const kinds = present.length === 0 ? undefined : present.reduce(meet);
|
|
306
505
|
const valueSets = items.map((t, i) => kindSets[i] === undefined ? valuesOf(t) : undefined);
|
|
307
506
|
const literalSets = valueSets
|
|
308
507
|
.filter(isLiterals)
|
|
309
508
|
.map((v) => new Set(v.map(valueKey)));
|
|
310
509
|
const keep = (m) => (kinds === undefined || admits(kinds, m)) &&
|
|
311
|
-
(!isLiteral(m) || literalSets.every((s) => s.has(valueKey(m))))
|
|
510
|
+
(!isLiteral(m) || literalSets.every((s) => s.has(valueKey(m)))) &&
|
|
511
|
+
templates.every((t) => admitsString(t, m));
|
|
312
512
|
const hasValues = valueSets.some((v) => v !== undefined);
|
|
313
513
|
let changed = false;
|
|
314
514
|
let literalsSeen = false;
|
|
@@ -316,7 +516,17 @@ function narrow(items) {
|
|
|
316
516
|
const out = [];
|
|
317
517
|
items.forEach((t, i) => {
|
|
318
518
|
const values = valueSets[i];
|
|
319
|
-
if (
|
|
519
|
+
if (strings[i]) {
|
|
520
|
+
// Values say more; kinds without strings admit none of it.
|
|
521
|
+
if (hasValues || kinds?.length === 0) {
|
|
522
|
+
changed = true;
|
|
523
|
+
if (!hasValues)
|
|
524
|
+
out.push(NEVER);
|
|
525
|
+
}
|
|
526
|
+
else
|
|
527
|
+
out.push(t);
|
|
528
|
+
}
|
|
529
|
+
else if (values !== undefined) {
|
|
320
530
|
if (isLiterals(values)) {
|
|
321
531
|
// The first set of literals, narrowed by the others, stands for them all.
|
|
322
532
|
if (literalsSeen) {
|
|
@@ -334,7 +544,7 @@ function narrow(items) {
|
|
|
334
544
|
}
|
|
335
545
|
}
|
|
336
546
|
else if (kindSets[i] !== undefined) {
|
|
337
|
-
if (hasValues || kindsSeen) {
|
|
547
|
+
if (hasValues || kindsSeen || templates.length > 0) {
|
|
338
548
|
changed = true;
|
|
339
549
|
return;
|
|
340
550
|
}
|
|
@@ -351,6 +561,87 @@ function narrow(items) {
|
|
|
351
561
|
});
|
|
352
562
|
return changed ? out : items;
|
|
353
563
|
}
|
|
564
|
+
/** Whether a type admits some strings only, a template among them: `` `a${string}` ``, `` `a${string}` | "b" ``. */
|
|
565
|
+
function isStrings(t) {
|
|
566
|
+
const members = t.kind === "union" ? t.items : [t];
|
|
567
|
+
return (members.some((m) => m.kind === "template") &&
|
|
568
|
+
members.every((m) => m.kind === "template" || valueKind(m) === "string"));
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Whether a type of some strings admits a string literal: a template by its pattern (the
|
|
572
|
+
* templates values meet are from `pattern`), a literal by its text.
|
|
573
|
+
*/
|
|
574
|
+
function admitsString(t, m) {
|
|
575
|
+
const text = m.text;
|
|
576
|
+
const s = JSON.parse(text);
|
|
577
|
+
return (t.kind === "union" ? t.items : [t]).some((x) => x.kind === "template"
|
|
578
|
+
? new RegExp(x.pattern, "u").test(s)
|
|
579
|
+
: x.text === text);
|
|
580
|
+
}
|
|
581
|
+
/** The kinds of value a type admits, where its form says so: `{ a: A }` objects, `string | A[]` strings and arrays. */
|
|
582
|
+
const kindSet = remembered((t) => {
|
|
583
|
+
if (t.kind === "inter") {
|
|
584
|
+
// The kinds every member it says them of allows.
|
|
585
|
+
const sets = t.items.map(kindSet).filter((s) => s !== undefined);
|
|
586
|
+
return sets.length === 0
|
|
587
|
+
? undefined
|
|
588
|
+
: sets.reduce((a, b) => a.filter((k) => b.some((o) => meets(k, o))));
|
|
589
|
+
}
|
|
590
|
+
if (t.kind !== "union")
|
|
591
|
+
return memberKinds(t);
|
|
592
|
+
const kinds = [];
|
|
593
|
+
for (const m of t.items) {
|
|
594
|
+
const k = memberKinds(m);
|
|
595
|
+
if (k === undefined)
|
|
596
|
+
return undefined;
|
|
597
|
+
kinds.push(...k);
|
|
598
|
+
}
|
|
599
|
+
return kinds;
|
|
600
|
+
});
|
|
601
|
+
/** `kindSet` of a type that is no union. */
|
|
602
|
+
function memberKinds(m) {
|
|
603
|
+
if (m.kind === "inter")
|
|
604
|
+
return kindSet(m);
|
|
605
|
+
const k = kindsOf(m)?.[0] ??
|
|
606
|
+
valueKind(m) ??
|
|
607
|
+
(m.kind === "null"
|
|
608
|
+
? "null"
|
|
609
|
+
: m.kind === "array"
|
|
610
|
+
? "array"
|
|
611
|
+
: m.kind === "template"
|
|
612
|
+
? "string"
|
|
613
|
+
: undefined);
|
|
614
|
+
return k === undefined ? undefined : ONE_KIND[k];
|
|
615
|
+
}
|
|
616
|
+
const meets = (a, b) => a === b ||
|
|
617
|
+
(a === "integer" && b === "number") ||
|
|
618
|
+
(a === "number" && b === "integer");
|
|
619
|
+
/**
|
|
620
|
+
* Drops the members of a union another member of the intersection rules out by kind:
|
|
621
|
+
* `{ a?: string } & (Cat | string)` is `{ a?: string } & Cat`. Returns `items` itself
|
|
622
|
+
* when nothing changes.
|
|
623
|
+
*/
|
|
624
|
+
function prune(items) {
|
|
625
|
+
if (!items.some((t) => t.kind === "union"))
|
|
626
|
+
return items;
|
|
627
|
+
const sets = items.map(kindSet);
|
|
628
|
+
let changed = false;
|
|
629
|
+
const out = items.map((t, i) => {
|
|
630
|
+
if (t.kind !== "union")
|
|
631
|
+
return t;
|
|
632
|
+
const others = sets.filter((s, j) => j !== i && s !== undefined);
|
|
633
|
+
const kept = t.items.filter((m) => {
|
|
634
|
+
const own = kindSet(m);
|
|
635
|
+
return (own === undefined ||
|
|
636
|
+
others.every((s) => own.some((k) => s.some((o) => meets(k, o)))));
|
|
637
|
+
});
|
|
638
|
+
if (kept.length === t.items.length)
|
|
639
|
+
return t;
|
|
640
|
+
changed = true;
|
|
641
|
+
return union(kept);
|
|
642
|
+
});
|
|
643
|
+
return changed ? out : items;
|
|
644
|
+
}
|
|
354
645
|
/** The members of a type that lists values (literals, null, exact objects and tuples), if it is one. */
|
|
355
646
|
function valuesOf(t) {
|
|
356
647
|
const members = t.kind === "union" ? t.items : [t];
|
|
@@ -388,7 +679,17 @@ function valueKey(m) {
|
|
|
388
679
|
return "null";
|
|
389
680
|
if (valueKind(m) !== "number")
|
|
390
681
|
return m.text;
|
|
391
|
-
|
|
682
|
+
// A number's key is remembered: a range is thousands of them, met again and again.
|
|
683
|
+
let key = numberKeys.get(m);
|
|
684
|
+
if (key === undefined) {
|
|
685
|
+
key = numberKey(m.text);
|
|
686
|
+
numberKeys.set(m, key);
|
|
687
|
+
}
|
|
688
|
+
return key;
|
|
689
|
+
}
|
|
690
|
+
const numberKeys = new WeakMap();
|
|
691
|
+
function numberKey(text) {
|
|
692
|
+
const [, sign, int, frac = "", exp = "0"] = /^(-?)(\d+)(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/.exec(text);
|
|
392
693
|
let digits = `${int}${frac}`.replace(/^0+/, "");
|
|
393
694
|
let e = Number(exp) - frac.length;
|
|
394
695
|
if (digits === "")
|
|
@@ -399,6 +700,12 @@ function valueKey(m) {
|
|
|
399
700
|
}
|
|
400
701
|
return `#${sign}${digits}e${e}`;
|
|
401
702
|
}
|
|
703
|
+
/** Whether two types of JSON values are the same value: literals by value (`1` and `1.0`), others by structure. */
|
|
704
|
+
export function equalValues(a, b) {
|
|
705
|
+
return a.kind === "lit" && b.kind === "lit"
|
|
706
|
+
? valueKey(a) === valueKey(b)
|
|
707
|
+
: equal(a, b);
|
|
708
|
+
}
|
|
402
709
|
/** Whether a value (`"a"`, `1.5`, `null`, an exact object) is of one of these kinds. */
|
|
403
710
|
function admits(kinds, m) {
|
|
404
711
|
const kind = m.kind === "null" ? "null" : valueKind(m);
|