@orkestrel/contract 0.0.11 → 0.0.12
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/dist/src/core/index.cjs +101 -28
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +48 -12
- package/dist/src/core/index.d.ts +48 -12
- package/dist/src/core/index.js +100 -29
- package/dist/src/core/index.js.map +1 -1
- package/package.json +9 -8
package/dist/src/core/index.cjs
CHANGED
|
@@ -2478,6 +2478,61 @@ function readArrayEntries(value) {
|
|
|
2478
2478
|
});
|
|
2479
2479
|
}
|
|
2480
2480
|
/**
|
|
2481
|
+
* Snapshot a guard shape and its optional-key mode for a shape combinator.
|
|
2482
|
+
*
|
|
2483
|
+
* @remarks
|
|
2484
|
+
* A null-prototype record plus its own key list is used instead of a `Map`.
|
|
2485
|
+
* The declared-key population decides the guard's answer, and
|
|
2486
|
+
* `Map.prototype.has`, `Map.prototype.get`, and map iteration are three
|
|
2487
|
+
* caller-writable members on that path. An own data key read by index
|
|
2488
|
+
* dispatches through nothing.
|
|
2489
|
+
*
|
|
2490
|
+
* @param shape - The guard shape whose own string declarations to snapshot
|
|
2491
|
+
* @param optional - The optional-key list, `true` for every key, or `undefined`
|
|
2492
|
+
* @param reader - The public combinator name used in read refusals
|
|
2493
|
+
* @returns The owned guards and names plus the collected optional-key membership
|
|
2494
|
+
* @throws {ContractError} When the shape or optional-key list cannot be read
|
|
2495
|
+
*
|
|
2496
|
+
* @example
|
|
2497
|
+
* ```ts
|
|
2498
|
+
* readGuardShape({ id: isString }, undefined, 'recordOf')
|
|
2499
|
+
* ```
|
|
2500
|
+
*/
|
|
2501
|
+
function readGuardShape(shape, optional, reader) {
|
|
2502
|
+
const declared = readValue(() => {
|
|
2503
|
+
const members = INTRINSICS.members(shape);
|
|
2504
|
+
const guards = INTRINSICS.create(null);
|
|
2505
|
+
const names = [];
|
|
2506
|
+
for (let index = 0; index < members.length; index += 1) {
|
|
2507
|
+
const key = members[index];
|
|
2508
|
+
if (!isString(key)) continue;
|
|
2509
|
+
if (!INTRINSICS.own(guards, key)) names[names.length] = key;
|
|
2510
|
+
guards[key] = shape[key];
|
|
2511
|
+
}
|
|
2512
|
+
return {
|
|
2513
|
+
guards,
|
|
2514
|
+
names,
|
|
2515
|
+
vocabulary: collectMembers(names)
|
|
2516
|
+
};
|
|
2517
|
+
}, reader, { subject: "shape" });
|
|
2518
|
+
const optionalKeys = readValue(() => {
|
|
2519
|
+
if (optional === true) return collectMembers(declared.names);
|
|
2520
|
+
if (!INTRINSICS.array(optional)) return collectMembers([]);
|
|
2521
|
+
const entries = readArrayEntries(optional);
|
|
2522
|
+
if (!entries.success) throw entries.error;
|
|
2523
|
+
if (!entries.value.dense) throw new INTRINSICS.error("Optional key list must be dense");
|
|
2524
|
+
const keys = collectMembers([]);
|
|
2525
|
+
for (let index = 0; index < entries.value.entries.length; index += 1) admitMember(keys, INTRINSICS.text(entries.value.entries[index]));
|
|
2526
|
+
return keys;
|
|
2527
|
+
}, reader, { subject: "optional" });
|
|
2528
|
+
return {
|
|
2529
|
+
guards: declared.guards,
|
|
2530
|
+
names: declared.names,
|
|
2531
|
+
optional: optionalKeys,
|
|
2532
|
+
vocabulary: declared.vocabulary
|
|
2533
|
+
};
|
|
2534
|
+
}
|
|
2535
|
+
/**
|
|
2481
2536
|
* Snapshot an object's own enumerable string keys through a total boundary.
|
|
2482
2537
|
*
|
|
2483
2538
|
* @remarks
|
|
@@ -5704,32 +5759,7 @@ function mapOf(keyGuard, valueGuard) {
|
|
|
5704
5759
|
*/
|
|
5705
5760
|
function recordOf(shape, optional) {
|
|
5706
5761
|
return contain(() => {
|
|
5707
|
-
const declared =
|
|
5708
|
-
const members = INTRINSICS.members(shape);
|
|
5709
|
-
const collected = INTRINSICS.create(null);
|
|
5710
|
-
const names = [];
|
|
5711
|
-
for (let index = 0; index < members.length; index += 1) {
|
|
5712
|
-
const key = members[index];
|
|
5713
|
-
if (!isString(key)) continue;
|
|
5714
|
-
if (!INTRINSICS.own(collected, key)) names[names.length] = key;
|
|
5715
|
-
collected[key] = shape[key];
|
|
5716
|
-
}
|
|
5717
|
-
return {
|
|
5718
|
-
collected,
|
|
5719
|
-
names,
|
|
5720
|
-
vocabulary: collectMembers(names)
|
|
5721
|
-
};
|
|
5722
|
-
}, "recordOf", { subject: "shape" });
|
|
5723
|
-
const optionalKeys = readValue(() => {
|
|
5724
|
-
if (optional === true) return collectMembers(declared.names);
|
|
5725
|
-
if (!INTRINSICS.array(optional)) return collectMembers([]);
|
|
5726
|
-
const entries = readArrayEntries(optional);
|
|
5727
|
-
if (!entries.success) throw entries.error;
|
|
5728
|
-
if (!entries.value.dense) throw new INTRINSICS.error("Optional key list must be dense");
|
|
5729
|
-
const keys = collectMembers([]);
|
|
5730
|
-
for (let index = 0; index < entries.value.entries.length; index += 1) admitMember(keys, INTRINSICS.text(entries.value.entries[index]));
|
|
5731
|
-
return keys;
|
|
5732
|
-
}, "recordOf", { subject: "optional" });
|
|
5762
|
+
const declared = readGuardShape(shape, optional, "recordOf");
|
|
5733
5763
|
return (value) => holds(() => {
|
|
5734
5764
|
if (!isRecord(value)) return false;
|
|
5735
5765
|
const members = INTRINSICS.members(value);
|
|
@@ -5740,9 +5770,9 @@ function recordOf(shape, optional) {
|
|
|
5740
5770
|
for (let index = 0; index < declared.names.length; index += 1) {
|
|
5741
5771
|
const key = declared.names[index];
|
|
5742
5772
|
if (key === void 0) continue;
|
|
5743
|
-
const guard = declared.
|
|
5773
|
+
const guard = declared.guards[key];
|
|
5744
5774
|
const present = INTRINSICS.own(value, key);
|
|
5745
|
-
if (!matchesMember(
|
|
5775
|
+
if (!matchesMember(declared.optional, key) && !present) return false;
|
|
5746
5776
|
if (present) {
|
|
5747
5777
|
if (guard === void 0 || !guard(value[key])) return false;
|
|
5748
5778
|
}
|
|
@@ -5752,6 +5782,47 @@ function recordOf(shape, optional) {
|
|
|
5752
5782
|
}, "recordOf");
|
|
5753
5783
|
}
|
|
5754
5784
|
/**
|
|
5785
|
+
* Build a guard that accepts non-array objects matching an open guard shape.
|
|
5786
|
+
*
|
|
5787
|
+
* @remarks
|
|
5788
|
+
* Three calling modes mirror {@link recordOf}:
|
|
5789
|
+
* - **No `optional`** — all shape keys required; unknown members admitted.
|
|
5790
|
+
* - **`optional: K[]`** — the listed keys are optional; all others required.
|
|
5791
|
+
* - **`optional: true`** — every shape key is optional.
|
|
5792
|
+
*
|
|
5793
|
+
* Each declared member is read through `Reflect.get`, so inherited data and
|
|
5794
|
+
* prototype accessors can satisfy the shape. An optional member passes when its
|
|
5795
|
+
* read value is `undefined`; otherwise its guard must accept the value. Unknown
|
|
5796
|
+
* members are never enumerated or inspected. Member-carrying functions are
|
|
5797
|
+
* accepted. Arrays, `null`, and primitives are rejected. Hostile reads return
|
|
5798
|
+
* `false` rather than throwing.
|
|
5799
|
+
*
|
|
5800
|
+
* @example
|
|
5801
|
+
* ```ts
|
|
5802
|
+
* const isResult = objectOf({ conclusion: isBoolean })
|
|
5803
|
+
* isResult({ conclusion: true, metadata: 'retained' }) // true
|
|
5804
|
+
* isResult([]) // false
|
|
5805
|
+
* ```
|
|
5806
|
+
*/
|
|
5807
|
+
function objectOf(shape, optional) {
|
|
5808
|
+
return contain(() => {
|
|
5809
|
+
const declared = readGuardShape(shape, optional, "objectOf");
|
|
5810
|
+
return (value) => holds(() => {
|
|
5811
|
+
if (!isObject(value) && !isFunction(value) || INTRINSICS.array(value)) return false;
|
|
5812
|
+
for (let index = 0; index < declared.names.length; index += 1) {
|
|
5813
|
+
const key = declared.names[index];
|
|
5814
|
+
if (key === void 0) continue;
|
|
5815
|
+
const guard = declared.guards[key];
|
|
5816
|
+
const member = INTRINSICS.read(value, key);
|
|
5817
|
+
if (matchesMember(declared.optional, key)) {
|
|
5818
|
+
if (member !== void 0 && (guard === void 0 || !guard(member))) return false;
|
|
5819
|
+
} else if (guard === void 0 || !guard(member)) return false;
|
|
5820
|
+
}
|
|
5821
|
+
return true;
|
|
5822
|
+
});
|
|
5823
|
+
}, "objectOf");
|
|
5824
|
+
}
|
|
5825
|
+
/**
|
|
5755
5826
|
* Build a guard that accepts values that are own keys of the provided object.
|
|
5756
5827
|
*
|
|
5757
5828
|
* @remarks
|
|
@@ -10505,6 +10576,7 @@ exports.nullShape = nullShape;
|
|
|
10505
10576
|
exports.nullableOf = nullableOf;
|
|
10506
10577
|
exports.nullableShape = nullableShape;
|
|
10507
10578
|
exports.numberShape = numberShape;
|
|
10579
|
+
exports.objectOf = objectOf;
|
|
10508
10580
|
exports.objectShape = objectShape;
|
|
10509
10581
|
exports.omitOf = omitOf;
|
|
10510
10582
|
exports.omitVisited = omitVisited;
|
|
@@ -10539,6 +10611,7 @@ exports.pinMembers = pinMembers;
|
|
|
10539
10611
|
exports.preview = preview;
|
|
10540
10612
|
exports.rawShape = rawShape;
|
|
10541
10613
|
exports.readArrayEntries = readArrayEntries;
|
|
10614
|
+
exports.readGuardShape = readGuardShape;
|
|
10542
10615
|
exports.readMapEntries = readMapEntries;
|
|
10543
10616
|
exports.readOptions = readOptions;
|
|
10544
10617
|
exports.readPattern = readPattern;
|