-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype-typed-array.assert.ts
More file actions
30 lines (29 loc) · 992 Bytes
/
Copy pathtype-typed-array.assert.ts
File metadata and controls
30 lines (29 loc) · 992 Bytes
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
import { AssertionError } from "../../assertion-error.js";
import { desc } from "../../describe/describe.js";
import { typeTypedArray } from "./type-typed-array.match.js";
import type { TypedArray } from "./type-typed-array.type.js";
/**
* Assert that a value is a TypedArray, with type-narrowing.
* Supports all TypedArray types: Uint8Array, Int8Array, Uint16Array,
* Int16Array, Uint32Array, Int32Array, Float32Array, Float64Array,
* BigInt64Array, BigUint64Array.
* @example
* ```ts
* import { assertTypeTypedArray } from "@kensio/smartass";
*
* const value: unknown = new Uint8Array([1, 2, 3]);
*
* assertTypeTypedArray(value);
*
* // value is now narrowed to a TypedArray
* ```
*/
export function assertTypeTypedArray(
value: unknown,
message = `Expected ${desc(value)} to be a TypedArray.`,
): asserts value is TypedArray {
const matcher = typeTypedArray();
if (!matcher.isMatch(value)) {
throw new AssertionError(message, value, matcher.represent());
}
}