deno.land / std@0.201.0 / assert / assert_array_includes.ts

assert_array_includes.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { equal } from "./equal.ts";import { format } from "./_format.ts";import { AssertionError } from "./assertion_error.ts";
/** * Make an assertion that `actual` includes the `expected` values. * If not then an error will be thrown. * * Type parameter can be specified to ensure values under comparison have the same type. * * @example * ```ts * import { assertArrayIncludes } from "https://deno.land/std@$STD_VERSION/assert/assert_array_includes.ts"; * * assertArrayIncludes<number>([1, 2], [2]) * ``` */export function assertArrayIncludes<T>( actual: ArrayLike<T>, expected: ArrayLike<T>, msg?: string,) { const missing: unknown[] = []; for (let i = 0; i < expected.length; i++) { let found = false; for (let j = 0; j < actual.length; j++) { if (equal(expected[i], actual[j])) { found = true; break; } } if (!found) { missing.push(expected[i]); } } if (missing.length === 0) { return; }
const msgSuffix = msg ? `: ${msg}` : "."; msg = `Expected actual: "${format(actual)}" to include: "${ format(expected) }"${msgSuffix}\nmissing: ${format(missing)}`; throw new AssertionError(msg);}
std

Version Info

Tagged at
2 years ago