-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter-stringable.ts
More file actions
43 lines (37 loc) · 904 Bytes
/
Copy pathfilter-stringable.ts
File metadata and controls
43 lines (37 loc) · 904 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
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @typedef {import("../types.js").AttrValueFilter} AttrValueFilter
*/
import type {
AttrName,
AttrValue,
AttrValueFilterFallback,
} from '../types.js';
import {
filterFallback
} from './filter-fallback.js';
import {
isStringable
} from '../util/is-stringable.js';
/**
* Filters a value into its string representation or into a JSON string.
*
* @type {AttrValueFilter}
*/
export function filterStringable(
value: unknown,
name?: AttrName,
fallback: AttrValueFilterFallback = false
): AttrValue {
if (value != null) {
if (!Array.isArray(value)) {
if (value instanceof Date) {
return value.toISOString();
}
if (isStringable(value)) {
return value.toString();
}
}
return JSON.stringify(value);
}
return filterFallback(value, name, fallback);
}