-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter-token.ts
56 lines (48 loc) · 1.23 KB
/
filter-token.ts
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
/**
* @typedef {import("../types.js").AttrValueFilter} AttrValueFilter
*/
import type {
AttrName,
AttrValue,
AttrValueFilterFallback,
} from '../types.js';
import {
filterFallback
} from './filter-fallback.js';
import {
FilterError
} from '../error.js';
import {
convertNumberToString
} from '../util/convert-number-to-string.js';
/**
* Filters a value that is a string, number, boolean, or BigInt for a token list.
*
* This filter is designed to replicate most of the behaviour of
* {@link https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList DOMTokenList}.
*
* @type {AttrValueFilter}
* @throws {FilterError}
*/
export function filterToken(
value: unknown,
name?: AttrName,
fallback: AttrValueFilterFallback = false
): AttrValue {
switch (typeof value) {
case 'string': {
return value;
}
case 'bigint':
case 'boolean': {
return value.toString();
}
case 'number': {
if (!Number.isFinite(value)) {
throw FilterError.create('{attr} is not finite', value, name);
}
return convertNumberToString(value);
}
}
return filterFallback(value, name, fallback);
}