-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer-equal.assert.ts
More file actions
34 lines (32 loc) · 1.04 KB
/
Copy pathbuffer-equal.assert.ts
File metadata and controls
34 lines (32 loc) · 1.04 KB
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
import { AssertionError } from "../../assertion-error.js";
import { desc } from "../../describe/describe.js";
import { bufferEqualTo } from "./buffer-equal.match.js";
import type { TypedArray } from "../type-typed-array/type-typed-array.type.js";
/**
* Assert that two buffers (TypedArrays) are equal, comparing element by element.
* Supports all TypedArray types: Uint8Array, Int8Array, Uint16Array, Int16Array,
* Uint32Array, Int32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array.
* @example
* ```ts
* import { assertBufferEqual } from "@kensio/smartass";
*
* const value: unknown = Buffer.from("ok");
*
* assertBufferEqual(value, Buffer.from("ok"));
*
* // value is now narrowed to Buffer
* ```
*/
export function assertBufferEqual<T extends TypedArray>(
actual: unknown,
expected: T,
message?: string,
): asserts actual is T {
if (!bufferEqualTo(expected).isMatch(actual)) {
throw new AssertionError(
message ?? `Expected ${desc(actual)} to equal ${desc(expected)}.`,
actual,
expected,
);
}
}