MCPcopy Create free account
hub / github.com/denoland/std / assertArrayIncludes

Function assertArrayIncludes

assert/array_includes.ts:40–76  ·  view source on GitHub ↗
(
  actual: ArrayLikeArg<T>,
  expected: ArrayLikeArg<T>,
  msg?: string,
)

Source from the content-addressed store, hash-verified

38 * @throws {AssertionError} If any value in `expected` is not found in `actual`.
39 */
40export function assertArrayIncludes<T>(
41 actual: ArrayLikeArg<T>,
42 expected: ArrayLikeArg<T>,
43 msg?: string,
44): void {
45 const missing: unknown[] = [];
46 const expectedLen = expected.length;
47 const actualLen = actual.length;
48 for (let i = 0; i < expectedLen; i++) {
49 const item = expected[i];
50 let found: boolean;
51 if (isPrimitive(item)) {
52 // Fast path
53 found = Array.prototype.includes.call(actual, item);
54 } else {
55 found = false;
56 for (let j = 0; j < actualLen; j++) {
57 if (equal(item, actual[j])) {
58 found = true;
59 break;
60 }
61 }
62 }
63 if (!found) {
64 missing.push(item);
65 }
66 }
67 if (missing.length === 0) {
68 return;
69 }
70
71 const msgSuffix = msg ? `: ${msg}` : ".";
72 msg = `Expected actual: "${format(actual)}" to include: "${
73 format(expected)
74 }"${msgSuffix}\nmissing: ${format(missing)}`;
75 throw new AssertionError(msg);
76}

Callers 4

fnFunction · 0.90
assertWalkPathsFunction · 0.90
assertWalkSyncPathsFunction · 0.90

Calls 4

equalFunction · 0.90
formatFunction · 0.90
isPrimitiveFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected