-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-length.assert.ts
More file actions
41 lines (39 loc) · 1.22 KB
/
array-length.assert.ts
File metadata and controls
41 lines (39 loc) · 1.22 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
35
36
37
38
39
40
41
import { assertNonNullable } from "../non-nullable/non-nullable.assert.js";
import { AssertionError } from "../../assertion-error.js";
import { desc, repr } from "../../describe/describe.js";
/**
* Assert that an array has exactly the expected length, with type narrowing.
* The type narrowing indicates:
* - An empty array for 0
* - An exact number of elements up to 5
* - At least 5 elements for >5
*/
export function assertArrayLength<T, const N extends number>(
value: readonly T[] | undefined | null,
expectedLength: N,
message?: string,
): asserts value is N extends 0
? readonly []
: N extends 1
? readonly [T]
: N extends 2
? readonly [T, T]
: N extends 3
? readonly [T, T, T]
: N extends 4
? readonly [T, T, T, T]
: N extends 5
? readonly [T, T, T, T, T]
: readonly [T, T, T, T, T, ...T[]] & {
length: N;
} {
assertNonNullable(value, message);
if (value.length !== expectedLength) {
throw new AssertionError(
message ??
`Expected ${desc(value)} to have length ${repr(expectedLength)}, but it had length ${repr(value.length)}.`,
value,
{ length: expectedLength },
);
}
}