@orkestrel/contract 0.0.3 → 0.0.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.
@@ -1,4 +1,4 @@
1
- import { AnyConstructor, FromGuards, Guard, GuardsShape, GuardType, IntersectionFromGuards, OptionalFromGuards, TupleFromGuards } from './types.js';
1
+ import { FromGuards, Guard, GuardsShape, GuardType, IntersectionFromGuards, OptionalFromGuards, TupleFromGuards } from './types.js';
2
2
  /**
3
3
  * Build a guard that accepts arrays whose every element satisfies `elementGuard`.
4
4
  *
@@ -50,7 +50,7 @@ export declare function literalOf<const Literals extends ReadonlyArray<string |
50
50
  * isDateValue({}) // false
51
51
  * ```
52
52
  */
53
- export declare function instanceOf<C>(ctor: C): Guard<InstanceType<C & AnyConstructor<object>>>;
53
+ export declare function instanceOf<C extends abstract new (...args: never) => object>(ctor: C): Guard<InstanceType<C>>;
54
54
  /**
55
55
  * Build a guard from a native `enum` or any object whose values are strings or
56
56
  * numbers.
@@ -63,7 +63,14 @@ function isUndefined(value) {
63
63
  function isDefined(value) {
64
64
  return value !== null && value !== void 0;
65
65
  }
66
- /** Determine whether a value is a string. */
66
+ /** Determine whether a value is a string.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * isString('hi') // true
71
+ * isString(42) // false
72
+ * ```
73
+ */
67
74
  function isString(value) {
68
75
  return typeof value === "string";
69
76
  }
@@ -72,11 +79,26 @@ function isString(value) {
72
79
  *
73
80
  * @remarks
74
81
  * Includes `NaN` and `±Infinity` — use {@link isFiniteNumber} to exclude them.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * isNumber(42) // true
86
+ * isNumber(Number.NaN) // true — NaN is still a number
87
+ * isNumber('42') // false
88
+ * ```
75
89
  */
76
90
  function isNumber(value) {
77
91
  return typeof value === "number";
78
92
  }
79
- /** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`). */
93
+ /** Determine whether a value is a finite number (excludes `NaN` and `±Infinity`).
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * isFiniteNumber(42) // true
98
+ * isFiniteNumber(Number.NaN) // false
99
+ * isFiniteNumber(Infinity) // false
100
+ * ```
101
+ */
80
102
  function isFiniteNumber(value) {
81
103
  return typeof value === "number" && Number.isFinite(value);
82
104
  }
@@ -193,6 +215,35 @@ function isNullableNumber(value) {
193
215
  function isNullableBoolean(value) {
194
216
  return value === null || isBoolean(value);
195
217
  }
218
+ /**
219
+ * Determine whether a value is an instance of a constructor, contained against
220
+ * a throwing `instanceof` check.
221
+ *
222
+ * @remarks
223
+ * The low-level total helper every `instanceof`-based guard in this file (and
224
+ * the `instanceOf` combinator) routes through. A bare `value instanceof X` is
225
+ * NOT total (AGENTS §14): it invokes `getPrototypeOf` on `value` — which a
226
+ * revoked `Proxy` or a `getPrototypeOf`-trap `Proxy` throws from — and, when
227
+ * `X[Symbol.hasInstance]` is user-defined, can throw from arbitrary code. This
228
+ * wraps the check in {@link attempt} (see ./helpers.js) so any such throw
229
+ * yields `false` instead of escaping.
230
+ *
231
+ * @param value - The value to test
232
+ * @param ctor - The constructor to test against
233
+ * @returns `true` when `value instanceof ctor`, `false` on a non-match or a
234
+ * contained throw
235
+ *
236
+ * @example
237
+ * ```ts
238
+ * isInstance(new Date(), Date) // true
239
+ * isInstance({}, Date) // false
240
+ * ```
241
+ */
242
+ function isInstance(value, ctor) {
243
+ const target = ctor;
244
+ const outcome = attempt(() => isFunction(target) && value instanceof target);
245
+ return outcome.success && outcome.value;
246
+ }
196
247
  /** Determine whether a value is a `Date`.
197
248
  *
198
249
  * @example
@@ -202,7 +253,7 @@ function isNullableBoolean(value) {
202
253
  * ```
203
254
  */
204
255
  function isDate(value) {
205
- return value instanceof Date;
256
+ return isInstance(value, Date);
206
257
  }
207
258
  /** Determine whether a value is a `RegExp`.
208
259
  *
@@ -213,7 +264,7 @@ function isDate(value) {
213
264
  * ```
214
265
  */
215
266
  function isRegExp(value) {
216
- return value instanceof RegExp;
267
+ return isInstance(value, RegExp);
217
268
  }
218
269
  /** Determine whether a value is an `Error`.
219
270
  *
@@ -224,7 +275,7 @@ function isRegExp(value) {
224
275
  * ```
225
276
  */
226
277
  function isError(value) {
227
- return value instanceof Error;
278
+ return isInstance(value, Error);
228
279
  }
229
280
  /** Determine whether a value is a native `Promise` (use {@link isPromiseLike} for any thenable).
230
281
  *
@@ -235,7 +286,7 @@ function isError(value) {
235
286
  * ```
236
287
  */
237
288
  function isPromise(value) {
238
- return value instanceof Promise;
289
+ return isInstance(value, Promise);
239
290
  }
240
291
  /**
241
292
  * Determine whether a value is promise-like — an object exposing callable
@@ -271,7 +322,7 @@ function isPromiseLike(value) {
271
322
  * ```
272
323
  */
273
324
  function isArrayBuffer(value) {
274
- return value instanceof ArrayBuffer;
325
+ return isInstance(value, ArrayBuffer);
275
326
  }
276
327
  /**
277
328
  * Determine whether a value is a `SharedArrayBuffer`.
@@ -287,7 +338,7 @@ function isArrayBuffer(value) {
287
338
  * ```
288
339
  */
289
340
  function isSharedArrayBuffer(value) {
290
- return typeof SharedArrayBuffer !== "undefined" && value instanceof SharedArrayBuffer;
341
+ return typeof SharedArrayBuffer !== "undefined" && isInstance(value, SharedArrayBuffer);
291
342
  }
292
343
  /**
293
344
  * Determine whether a value implements the iterable protocol (`Symbol.iterator`).
@@ -356,6 +407,14 @@ function isObject(value) {
356
407
  * instance's runs through the class's own prototype. The whole body runs
357
408
  * inside `attempt` (AGENTS §14) so a revoked `Proxy` or a hostile
358
409
  * `getPrototypeOf` trap cannot escape as a thrown error.
410
+ *
411
+ * @example
412
+ * ```ts
413
+ * isRecord({ a: 1 }) // true
414
+ * isRecord(Object.create(null)) // true
415
+ * isRecord([]) // false
416
+ * isRecord(new Date()) // false
417
+ * ```
359
418
  */
360
419
  function isRecord(value) {
361
420
  const outcome = attempt(() => {
@@ -374,7 +433,7 @@ function isRecord(value) {
374
433
  * ```
375
434
  */
376
435
  function isMap(value) {
377
- return value instanceof Map;
436
+ return isInstance(value, Map);
378
437
  }
379
438
  /** Determine whether a value is a `Set`.
380
439
  *
@@ -385,7 +444,7 @@ function isMap(value) {
385
444
  * ```
386
445
  */
387
446
  function isSet(value) {
388
- return value instanceof Set;
447
+ return isInstance(value, Set);
389
448
  }
390
449
  /** Determine whether a value is a `WeakMap`.
391
450
  *
@@ -396,7 +455,7 @@ function isSet(value) {
396
455
  * ```
397
456
  */
398
457
  function isWeakMap(value) {
399
- return value instanceof WeakMap;
458
+ return isInstance(value, WeakMap);
400
459
  }
401
460
  /** Determine whether a value is a `WeakSet`.
402
461
  *
@@ -407,7 +466,7 @@ function isWeakMap(value) {
407
466
  * ```
408
467
  */
409
468
  function isWeakSet(value) {
410
- return value instanceof WeakSet;
469
+ return isInstance(value, WeakSet);
411
470
  }
412
471
  /** Determine whether a value is an array.
413
472
  *
@@ -429,7 +488,7 @@ function isArray(value) {
429
488
  * ```
430
489
  */
431
490
  function isDataView(value) {
432
- return value instanceof DataView;
491
+ return isInstance(value, DataView);
433
492
  }
434
493
  /** Determine whether a value is an `ArrayBufferView` (any typed array or `DataView`).
435
494
  *
@@ -451,7 +510,7 @@ function isArrayBufferView(value) {
451
510
  * ```
452
511
  */
453
512
  function isInt8Array(value) {
454
- return value instanceof Int8Array;
513
+ return isInstance(value, Int8Array);
455
514
  }
456
515
  /** Determine whether a value is a `Uint8Array`.
457
516
  *
@@ -462,7 +521,7 @@ function isInt8Array(value) {
462
521
  * ```
463
522
  */
464
523
  function isUint8Array(value) {
465
- return value instanceof Uint8Array;
524
+ return isInstance(value, Uint8Array);
466
525
  }
467
526
  /** Determine whether a value is a `Uint8ClampedArray`.
468
527
  *
@@ -473,7 +532,7 @@ function isUint8Array(value) {
473
532
  * ```
474
533
  */
475
534
  function isUint8ClampedArray(value) {
476
- return value instanceof Uint8ClampedArray;
535
+ return isInstance(value, Uint8ClampedArray);
477
536
  }
478
537
  /** Determine whether a value is an `Int16Array`.
479
538
  *
@@ -484,7 +543,7 @@ function isUint8ClampedArray(value) {
484
543
  * ```
485
544
  */
486
545
  function isInt16Array(value) {
487
- return value instanceof Int16Array;
546
+ return isInstance(value, Int16Array);
488
547
  }
489
548
  /** Determine whether a value is a `Uint16Array`.
490
549
  *
@@ -495,7 +554,7 @@ function isInt16Array(value) {
495
554
  * ```
496
555
  */
497
556
  function isUint16Array(value) {
498
- return value instanceof Uint16Array;
557
+ return isInstance(value, Uint16Array);
499
558
  }
500
559
  /** Determine whether a value is an `Int32Array`.
501
560
  *
@@ -506,7 +565,7 @@ function isUint16Array(value) {
506
565
  * ```
507
566
  */
508
567
  function isInt32Array(value) {
509
- return value instanceof Int32Array;
568
+ return isInstance(value, Int32Array);
510
569
  }
511
570
  /** Determine whether a value is a `Uint32Array`.
512
571
  *
@@ -517,7 +576,7 @@ function isInt32Array(value) {
517
576
  * ```
518
577
  */
519
578
  function isUint32Array(value) {
520
- return value instanceof Uint32Array;
579
+ return isInstance(value, Uint32Array);
521
580
  }
522
581
  /** Determine whether a value is a `Float32Array`.
523
582
  *
@@ -528,7 +587,7 @@ function isUint32Array(value) {
528
587
  * ```
529
588
  */
530
589
  function isFloat32Array(value) {
531
- return value instanceof Float32Array;
590
+ return isInstance(value, Float32Array);
532
591
  }
533
592
  /** Determine whether a value is a `Float64Array`.
534
593
  *
@@ -539,7 +598,7 @@ function isFloat32Array(value) {
539
598
  * ```
540
599
  */
541
600
  function isFloat64Array(value) {
542
- return value instanceof Float64Array;
601
+ return isInstance(value, Float64Array);
543
602
  }
544
603
  /**
545
604
  * Determine whether a value is a `BigInt64Array`.
@@ -555,7 +614,7 @@ function isFloat64Array(value) {
555
614
  * ```
556
615
  */
557
616
  function isBigInt64Array(value) {
558
- return typeof BigInt64Array !== "undefined" && value instanceof BigInt64Array;
617
+ return typeof BigInt64Array !== "undefined" && isInstance(value, BigInt64Array);
559
618
  }
560
619
  /**
561
620
  * Determine whether a value is a `BigUint64Array`.
@@ -571,7 +630,7 @@ function isBigInt64Array(value) {
571
630
  * ```
572
631
  */
573
632
  function isBigUint64Array(value) {
574
- return typeof BigUint64Array !== "undefined" && value instanceof BigUint64Array;
633
+ return typeof BigUint64Array !== "undefined" && isInstance(value, BigUint64Array);
575
634
  }
576
635
  /** Determine whether a value is the empty string `''`.
577
636
  *
@@ -605,7 +664,8 @@ function isEmptyArray(value) {
605
664
  */
606
665
  function isEmptyObject(value) {
607
666
  if (!isRecord(value)) return false;
608
- return Object.keys(value).length === 0 && enumerableSymbolCount(value) === 0;
667
+ const outcome = attempt(() => Object.keys(value).length === 0 && enumerableSymbolCount(value) === 0);
668
+ return outcome.success && outcome.value;
609
669
  }
610
670
  /** Determine whether a value is an empty `Map`.
611
671
  *
@@ -616,7 +676,7 @@ function isEmptyObject(value) {
616
676
  * ```
617
677
  */
618
678
  function isEmptyMap(value) {
619
- return value instanceof Map && value.size === 0;
679
+ return isMap(value) && value.size === 0;
620
680
  }
621
681
  /** Determine whether a value is an empty `Set`.
622
682
  *
@@ -627,7 +687,7 @@ function isEmptyMap(value) {
627
687
  * ```
628
688
  */
629
689
  function isEmptySet(value) {
630
- return value instanceof Set && value.size === 0;
690
+ return isSet(value) && value.size === 0;
631
691
  }
632
692
  /** Determine whether a value is a non-empty string (at least one character).
633
693
  *
@@ -661,7 +721,8 @@ function isNonEmptyArray(value) {
661
721
  */
662
722
  function isNonEmptyObject(value) {
663
723
  if (!isRecord(value)) return false;
664
- return Object.keys(value).length > 0 || enumerableSymbolCount(value) > 0;
724
+ const outcome = attempt(() => Object.keys(value).length > 0 || enumerableSymbolCount(value) > 0);
725
+ return outcome.success && outcome.value;
665
726
  }
666
727
  /** Determine whether a value is a non-empty `Map` (at least one entry).
667
728
  *
@@ -672,7 +733,7 @@ function isNonEmptyObject(value) {
672
733
  * ```
673
734
  */
674
735
  function isNonEmptyMap(value) {
675
- return value instanceof Map && value.size > 0;
736
+ return isMap(value) && value.size > 0;
676
737
  }
677
738
  /** Determine whether a value is a non-empty `Set` (at least one element).
678
739
  *
@@ -683,7 +744,7 @@ function isNonEmptyMap(value) {
683
744
  * ```
684
745
  */
685
746
  function isNonEmptySet(value) {
686
- return value instanceof Set && value.size > 0;
747
+ return isSet(value) && value.size > 0;
687
748
  }
688
749
  /** Determine whether a value is a function that declares zero parameters (`Function.length === 0`).
689
750
  *
@@ -1071,7 +1132,7 @@ function literalOf(...literals) {
1071
1132
  * ```
1072
1133
  */
1073
1134
  function instanceOf(ctor) {
1074
- return (value) => isConstructor(ctor) && isObject(value) && value instanceof ctor;
1135
+ return (value) => isConstructor(ctor) && isObject(value) && isInstance(value, ctor);
1075
1136
  }
1076
1137
  /**
1077
1138
  * Build a guard from a native `enum` or any object whose values are strings or
@@ -1986,7 +2047,11 @@ function compileGuard(shape) {
1986
2047
  return outcome.success && outcome.value;
1987
2048
  };
1988
2049
  }
1989
- case "union": return unionOf(...shape.variants.map((variant) => compileGuard(variant)));
2050
+ case "union": {
2051
+ const guards = shape.variants.map((variant) => compileGuard(variant));
2052
+ if (shape.mode === "oneOf") return (value) => guards.filter((guard) => guard(value)).length === 1;
2053
+ return unionOf(...guards);
2054
+ }
1990
2055
  case "optional": return orOf(isUndefined, compileGuard(shape.inner));
1991
2056
  case "nullable": return nullableOf(compileGuard(shape.inner));
1992
2057
  case "raw": return (_value) => true;
@@ -2093,6 +2158,11 @@ function compileParser(shape) {
2093
2158
  parse: compileParser(variant),
2094
2159
  guard: compileGuard(variant)
2095
2160
  }));
2161
+ if (shape.mode === "oneOf") return (value) => {
2162
+ const matches = variants.filter((variant) => variant.guard(value));
2163
+ const [only] = matches;
2164
+ return matches.length === 1 && only !== void 0 ? only.parse(value) : void 0;
2165
+ };
2096
2166
  return (value) => {
2097
2167
  for (const variant of variants) if (variant.guard(value)) return value;
2098
2168
  for (const variant of variants) {
@@ -2416,16 +2486,32 @@ function unionShape(...variants) {
2416
2486
  * Build a {@link UnionShape} that emits `oneOf` (exactly one match) in JSON Schema.
2417
2487
  *
2418
2488
  * @remarks
2419
- * Runtime behavior is identical to {@link unionShape} — only the emitted schema
2420
- * keyword differs (`oneOf` vs `anyOf`).
2489
+ * Unlike {@link unionShape} (`anyOf` — at least one variant matches),
2490
+ * `oneOfShape`'s compiled guard and parser enforce EXACTLY one match:
2491
+ *
2492
+ * - **Guard**: accepts the value only when exactly one variant's guard
2493
+ * accepts it. A value matching two-or-more variants — which would violate
2494
+ * the emitted `oneOf` schema — is rejected, even though it would pass
2495
+ * {@link unionShape}'s guard.
2496
+ * - **Parser**: judged on the RAW input's guard matches only, with NO
2497
+ * coercion fallback for an ambiguous input. When exactly one variant's
2498
+ * guard accepts the raw value, that variant's parser runs. Zero matches or
2499
+ * two-or-more matches both parse to `undefined` — a value ambiguous
2500
+ * between variants has no well-defined coercion target.
2501
+ *
2502
+ * Prefer {@link unionShape} when a value may legitimately satisfy more than
2503
+ * one variant (e.g. overlapping shapes) and any match is acceptable. Prefer
2504
+ * `oneOfShape` when overlap between variants indicates malformed input that
2505
+ * must be rejected.
2421
2506
  *
2422
2507
  * @param variants - The candidate shapes
2423
2508
  * @returns A union shape with `mode: 'oneOf'`
2424
2509
  *
2425
2510
  * @example
2426
2511
  * ```ts
2427
- * const id = oneOfShape(stringShape(), integerShape())
2428
- * // Infer<typeof id> = string | number
2512
+ * const id = oneOfShape(numberShape(), integerShape())
2513
+ * // 3 fails — matches both numberShape and integerShape
2514
+ * // 3.5 passes — matches numberShape only
2429
2515
  * ```
2430
2516
  */
2431
2517
  function oneOfShape(...variants) {
@@ -2557,6 +2643,7 @@ exports.isFloat32Array = isFloat32Array;
2557
2643
  exports.isFloat64Array = isFloat64Array;
2558
2644
  exports.isFunction = isFunction;
2559
2645
  exports.isGeneratorFunction = isGeneratorFunction;
2646
+ exports.isInstance = isInstance;
2560
2647
  exports.isInt16Array = isInt16Array;
2561
2648
  exports.isInt32Array = isInt32Array;
2562
2649
  exports.isInt8Array = isInt8Array;