1 | import { DEBUG } from '@glimmer/env';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | type Coercable = string | number | boolean | null | undefined | symbol;
|
10 |
|
11 | function coerceId(id: Coercable): string | null {
|
12 | if (id === null || id === undefined || id === '') {
|
13 | return null;
|
14 | }
|
15 | if (typeof id === 'string') {
|
16 | return id;
|
17 | }
|
18 | if (typeof id === 'symbol') {
|
19 | return id.toString();
|
20 | }
|
21 | return '' + id;
|
22 | }
|
23 |
|
24 | export function ensureStringId(id: Coercable): string {
|
25 | let normalized: string | null = null;
|
26 | if (typeof id === 'string') {
|
27 | normalized = id.length > 0 ? id : null;
|
28 | } else if (typeof id === 'number' && !isNaN(id)) {
|
29 | normalized = '' + id;
|
30 | }
|
31 |
|
32 | if (DEBUG && normalized === null) {
|
33 | throw new Error(`Expected id to be a string or number, received ${String(id)}`);
|
34 | }
|
35 |
|
36 | return normalized!;
|
37 | }
|
38 |
|
39 | export default coerceId;
|