Skip to main content
Home
This release is 18 versions behind 1.0.16 — the latest version of @std/assert. Jump to latest

@std/assert@0.225.3
Built and signed on GitHub Actions

Common assertion functions, especially useful for testing

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 years ago (0.225.3)
Package root>assert_array_includes.ts
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. import { equal } from "./equal.ts"; import { format } from "jsr:/@std/internal@^1.0.0/format"; import { AssertionError } from "./assertion_error.ts"; /** An array-like object (`Array`, `Uint8Array`, `NodeList`, etc.) that is not a string */ export type ArrayLikeArg<T> = ArrayLike<T> & object; /** * 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 "@std/assert/assert-array-includes"; * * assertArrayIncludes([1, 2], [2]); // Doesn't throw * assertArrayIncludes([1, 2], [3]); // Throws * ``` */ export function assertArrayIncludes<T>( actual: ArrayLikeArg<T>, expected: ArrayLikeArg<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); }