-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring-length.assert.ts
More file actions
70 lines (65 loc) · 1.98 KB
/
Copy pathstring-length.assert.ts
File metadata and controls
70 lines (65 loc) · 1.98 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { AssertionError } from "../../assertion-error.js";
import { desc, repr } from "../../describe/describe.js";
import { stringOfLength } from "./string-length.match.js";
import type {
StringOfLength,
StringOfLengthAssertion,
} from "./string-length.type.js";
export function assertStringLength<
TActual extends string,
const N extends number,
>(
value: TActual,
expectedLength: N,
message?: string,
): asserts value is StringOfLengthAssertion<TActual, N>;
export function assertStringLength<const N extends number>(
value: unknown,
expectedLength: N,
message?: string,
): asserts value is StringOfLength<N>;
/**
* Assert that a string has exactly the expected length, with type narrowing.
*
* The type narrowing indicates:
* - An empty string for 0
* - An exact length and safe indexing of known character positions up to 10
* - For >10, an exact length and safe indexing of known character positions up
* to 10 when those positions are guaranteed to exist.
*
* Note that this models JavaScript string indexing and length (UTF-16 code units),
* not Unicode grapheme clusters.
* @example
* ```ts
* import { assertStringLength } from "@kensio/smartass";
*
* const code: string = "ABC123";
*
* assertStringLength(code, 6);
*
* // code is now narrowed to a string with exactly 6 code units
* ```
*/
export function assertStringLength(
value: unknown,
expectedLength: number,
message?: string,
): void {
const matcher = stringOfLength(expectedLength);
if (!matcher.matches(value)) {
throw new AssertionError(
message ?? buildStringLengthMessage(value, expectedLength),
value,
matcher.represent(),
);
}
}
function buildStringLengthMessage(
value: unknown,
expectedLength: number,
): string {
if (typeof value !== "string") {
return `Expected ${desc(value)} to be a string of length ${repr(expectedLength)}.`;
}
return `Expected ${desc(value)} to have length ${repr(expectedLength)}, but it had length ${repr(value.length)}.`;
}