@augment-vir/assert 31.71.0 → 31.71.2

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,7 +1,17 @@
1
- import { type AnyObject, type MaybePromise, type NarrowToActual, type NarrowToExpected, type Values } from '@augment-vir/core';
1
+ import { type AnyObject, type ArrayElement, type MaybePromise, type NarrowToActual, type NarrowToExpected, type Values } from '@augment-vir/core';
2
2
  import { type EmptyObject } from 'type-fest';
3
3
  import { type WaitUntilOptions } from '../guard-types/wait-until-function.js';
4
- export declare function isIn<Parent extends object | string>(this: void, child: unknown, parent: Parent): child is Values<Parent>;
4
+ /**
5
+ * Extracts the child value type from a parent passed to `isIn`/`isNotIn` style guards. Uses
6
+ * `ArrayElement` for arrays and tuples so that array-only properties like `length` are not leaked
7
+ * as candidate values, and `Values` for everything else.
8
+ *
9
+ * @category Assert : Util
10
+ * @category Package : @augment-vir/assert
11
+ * @package [`@augment-vir/assert`](https://www.npmjs.com/package/@augment-vir/assert)
12
+ */
13
+ export type ChildOf<Parent> = Parent extends ReadonlyArray<unknown> ? ArrayElement<Parent> : Values<Parent>;
14
+ export declare function isIn<Parent extends object | string>(this: void, child: unknown, parent: Parent): child is ChildOf<Parent>;
5
15
  /**
6
16
  * All types that can be checked for emptiness. The empty variants of these types are represented in
7
17
  * {@link Empty}.
@@ -165,7 +175,7 @@ export declare const valueGuards: {
165
175
  * @see
166
176
  * - {@link assert.isNotIn} : the opposite assertion.
167
177
  */
168
- isIn<Parent extends object | string>(this: void, child: unknown, parent: Parent, failureMessage?: string | undefined): asserts child is Values<Parent>;
178
+ isIn<Parent extends object | string>(this: void, child: unknown, parent: Parent, failureMessage?: string | undefined): asserts child is ChildOf<Parent>;
169
179
  /**
170
180
  * Asserts that child value is _not_ contained within a parent object, array, or string through
171
181
  * reference equality.
@@ -191,7 +201,7 @@ export declare const valueGuards: {
191
201
  * @see
192
202
  * - {@link assert.isIn} : the opposite assertion.
193
203
  */
194
- isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent, failureMessage?: string | undefined): asserts child is Exclude<Child, Values<Parent>>;
204
+ isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent, failureMessage?: string | undefined): asserts child is Exclude<Child, ChildOf<Parent>>;
195
205
  /**
196
206
  * Asserts that a value is empty. Supports strings, Maps, Sets, objects, and arrays.
197
207
  *
@@ -377,7 +387,7 @@ export declare const valueGuards: {
377
387
  * @see
378
388
  * - {@link check.isNotIn} : the opposite check.
379
389
  */
380
- isIn<Parent extends object | string>(this: void, child: unknown, parent: Parent): child is Values<Parent>;
390
+ isIn<Parent extends object | string>(this: void, child: unknown, parent: Parent): child is ChildOf<Parent>;
381
391
  /**
382
392
  * Checks that child value is _not_ contained within a parent object, array, or string
383
393
  * through reference equality.
@@ -402,7 +412,7 @@ export declare const valueGuards: {
402
412
  * @see
403
413
  * - {@link check.isIn} : the opposite check.
404
414
  */
405
- isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent): child is Exclude<Child, Values<Parent>>;
415
+ isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent): child is Exclude<Child, ChildOf<Parent>>;
406
416
  /**
407
417
  * Checks that a value is empty. Supports strings, Maps, Sets, objects, and arrays.
408
418
  *
@@ -598,7 +608,7 @@ export declare const valueGuards: {
598
608
  * @see
599
609
  * - {@link assertWrap.isNotIn} : the opposite assertion.
600
610
  */
601
- isIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent, failureMessage?: string | undefined): Extract<Child, Values<Parent>>;
611
+ isIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent, failureMessage?: string | undefined): Extract<Child, ChildOf<Parent>>;
602
612
  /**
603
613
  * Asserts that child value is _not_ contained within a parent object, array, or string
604
614
  * through reference equality. Returns the child value if the assertion passes.
@@ -625,7 +635,7 @@ export declare const valueGuards: {
625
635
  * @see
626
636
  * - {@link assertWrap.isIn} : the opposite assertion.
627
637
  */
628
- isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent, failureMessage?: string | undefined): Exclude<Child, Values<Parent>>;
638
+ isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent, failureMessage?: string | undefined): Exclude<Child, ChildOf<Parent>>;
629
639
  /**
630
640
  * Asserts that a value is empty. Supports strings, Maps, Sets, objects, and arrays. Returns
631
641
  * the value if the assertion passes.
@@ -815,7 +825,7 @@ export declare const valueGuards: {
815
825
  * @see
816
826
  * - {@link checkWrap.isNotIn} : the opposite check.
817
827
  */
818
- isIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent): Extract<Child, Values<Parent>> | undefined;
828
+ isIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent): Extract<Child, ChildOf<Parent>> | undefined;
819
829
  /**
820
830
  * Checks that child value is _not_ contained within a parent object, array, or string
821
831
  * through reference equality.
@@ -840,7 +850,7 @@ export declare const valueGuards: {
840
850
  * @see
841
851
  * - {@link checkWrap.isIn} : the opposite check.
842
852
  */
843
- isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent): Exclude<Child, Values<Parent>> | undefined;
853
+ isNotIn<Parent extends object | string, Child>(this: void, child: Child, parent: Parent): Exclude<Child, ChildOf<Parent>> | undefined;
844
854
  /**
845
855
  * Checks that a value is empty. Supports strings, Maps, Sets, objects, and arrays.
846
856
  *
@@ -1074,7 +1084,7 @@ export declare const valueGuards: {
1074
1084
  * @see
1075
1085
  * - {@link waitUntil.isNotIn} : the opposite assertion.
1076
1086
  */
1077
- isIn: <Child, Parent>(this: void, parent: Parent, callback: () => MaybePromise<Child>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToExpected<Child, Values<Parent>>>;
1087
+ isIn: <Child, Parent>(this: void, parent: Parent, callback: () => MaybePromise<Child>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<NarrowToExpected<Child, ChildOf<Parent>>>;
1078
1088
  /**
1079
1089
  * Repeatedly calls a callback until its output is child value is _not_ contained within a
1080
1090
  * parent object, array, or string through reference equality. Once the callback output
@@ -1102,7 +1112,7 @@ export declare const valueGuards: {
1102
1112
  * @see
1103
1113
  * - {@link waitUntil.isIn} : the opposite assertion.
1104
1114
  */
1105
- isNotIn: <Child, Parent>(this: void, parent: Parent, callback: () => MaybePromise<Child>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Child, Values<Parent>>>;
1115
+ isNotIn: <Child, Parent>(this: void, parent: Parent, callback: () => MaybePromise<Child>, options?: WaitUntilOptions | undefined, failureMessage?: string | undefined) => Promise<Exclude<Child, ChildOf<Parent>>>;
1106
1116
  /**
1107
1117
  * Repeatedly calls a callback until its output is a value is empty. Supports strings, Maps,
1108
1118
  * Sets, objects, and arrays. Once the callback output passes, it is returned. If the
@@ -1,21 +1,28 @@
1
1
  import { stringify, } from '@augment-vir/core';
2
2
  import { AssertionError } from '../augments/assertion.error.js';
3
3
  import { createWaitUntil } from '../guard-types/wait-until-function.js';
4
+ /**
5
+ * Returns the own enumerable values of `parent`. For arrays, returns the elements directly so that
6
+ * array-only properties like `length` are not treated as values. Throws if `Reflect.ownKeys` fails
7
+ * on the input.
8
+ */
9
+ function getOwnValues(parent) {
10
+ if (Array.isArray(parent)) {
11
+ return parent;
12
+ }
13
+ return Reflect.ownKeys(parent).map((key) => parent[key]);
14
+ }
4
15
  function hasValue(parent, value) {
5
16
  if (typeof parent === 'string') {
6
17
  return typeof value === 'string' && parent.includes(value);
7
18
  }
8
19
  /** Wrap this in a try/catch because `Reflect.ownKeys` can fail depending on what its input is. */
9
- let hasValue = true;
10
20
  try {
11
- hasValue = Reflect.ownKeys(parent)
12
- .map((key) => parent[key])
13
- .includes(value);
21
+ return getOwnValues(parent).includes(value);
14
22
  }
15
23
  catch {
16
24
  return false;
17
25
  }
18
- return hasValue;
19
26
  }
20
27
  export function isIn(child, parent) {
21
28
  if (typeof parent === 'string') {
@@ -125,7 +132,7 @@ const assertions = {
125
132
  }
126
133
  else {
127
134
  try {
128
- const actualValues = Reflect.ownKeys(parent).map((key) => parent[key]);
135
+ const actualValues = getOwnValues(parent);
129
136
  missingValues = values.filter((value) => {
130
137
  return !actualValues.includes(value);
131
138
  });
@@ -180,7 +187,7 @@ const assertions = {
180
187
  }
181
188
  else {
182
189
  try {
183
- const actualValues = Reflect.ownKeys(parent).map((key) => parent[key]);
190
+ const actualValues = getOwnValues(parent);
184
191
  includedValues = values.filter((value) => {
185
192
  return actualValues.includes(value);
186
193
  });
@@ -715,7 +722,7 @@ export const valueGuards = {
715
722
  }
716
723
  else {
717
724
  try {
718
- const actualValues = Reflect.ownKeys(parent).map((key) => parent[key]);
725
+ const actualValues = getOwnValues(parent);
719
726
  missingValues = values.filter((value) => {
720
727
  return !actualValues.includes(value);
721
728
  });
@@ -772,7 +779,7 @@ export const valueGuards = {
772
779
  }
773
780
  else {
774
781
  try {
775
- const actualValues = Reflect.ownKeys(parent).map((key) => parent[key]);
782
+ const actualValues = getOwnValues(parent);
776
783
  includedValues = values.filter((value) => {
777
784
  return actualValues.includes(value);
778
785
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/assert",
3
- "version": "31.71.0",
3
+ "version": "31.71.2",
4
4
  "description": "A collection of assertions for test and production code alike.",
5
5
  "keywords": [
6
6
  "augment",
@@ -42,7 +42,7 @@
42
42
  "test:update": "npm test"
43
43
  },
44
44
  "dependencies": {
45
- "@augment-vir/core": "^31.71.0",
45
+ "@augment-vir/core": "^31.71.2",
46
46
  "@date-vir/duration": "^8.3.2",
47
47
  "deep-eql": "^5.0.2",
48
48
  "expect-type": "^1.3.0",