1 | import { ElementRef } from '@angular/core';
|
2 |
|
3 | /** Coerces a data-bound value (typically a string) to a boolean. */
|
4 | function coerceBooleanProperty(value) {
|
5 | return value != null && `${value}` !== 'false';
|
6 | }
|
7 |
|
8 | function coerceNumberProperty(value, fallbackValue = 0) {
|
9 | return _isNumberValue(value) ? Number(value) : fallbackValue;
|
10 | }
|
11 | /**
|
12 | * Whether the provided value is considered a number.
|
13 | * @docs-private
|
14 | */
|
15 | function _isNumberValue(value) {
|
16 | // parseFloat(value) handles most of the cases we're interested in (it treats null, empty string,
|
17 | // and other non-number values as NaN, where Number just uses 0) but it considers the string
|
18 | // '123hello' to be a valid number. Therefore we also check if Number(value) is NaN.
|
19 | return !isNaN(parseFloat(value)) && !isNaN(Number(value));
|
20 | }
|
21 |
|
22 | function coerceArray(value) {
|
23 | return Array.isArray(value) ? value : [value];
|
24 | }
|
25 |
|
26 | /** Coerces a value to a CSS pixel value. */
|
27 | function coerceCssPixelValue(value) {
|
28 | if (value == null) {
|
29 | return '';
|
30 | }
|
31 | return typeof value === 'string' ? value : `${value}px`;
|
32 | }
|
33 |
|
34 | /**
|
35 | * Coerces an ElementRef or an Element into an element.
|
36 | * Useful for APIs that can accept either a ref or the native element itself.
|
37 | */
|
38 | function coerceElement(elementOrRef) {
|
39 | return elementOrRef instanceof ElementRef ? elementOrRef.nativeElement : elementOrRef;
|
40 | }
|
41 |
|
42 | /**
|
43 | * Coerces a value to an array of trimmed non-empty strings.
|
44 | * Any input that is not an array, `null` or `undefined` will be turned into a string
|
45 | * via `toString()` and subsequently split with the given separator.
|
46 | * `null` and `undefined` will result in an empty array.
|
47 | * This results in the following outcomes:
|
48 | * - `null` -> `[]`
|
49 | * - `[null]` -> `["null"]`
|
50 | * - `["a", "b ", " "]` -> `["a", "b"]`
|
51 | * - `[1, [2, 3]]` -> `["1", "2,3"]`
|
52 | * - `[{ a: 0 }]` -> `["[object Object]"]`
|
53 | * - `{ a: 0 }` -> `["[object", "Object]"]`
|
54 | *
|
55 | * Useful for defining CSS classes or table columns.
|
56 | * @param value the value to coerce into an array of strings
|
57 | * @param separator split-separator if value isn't an array
|
58 | */
|
59 | function coerceStringArray(value, separator = /\s+/) {
|
60 | const result = [];
|
61 | if (value != null) {
|
62 | const sourceValues = Array.isArray(value) ? value : `${value}`.split(separator);
|
63 | for (const sourceValue of sourceValues) {
|
64 | const trimmedString = `${sourceValue}`.trim();
|
65 | if (trimmedString) {
|
66 | result.push(trimmedString);
|
67 | }
|
68 | }
|
69 | }
|
70 | return result;
|
71 | }
|
72 |
|
73 | export { _isNumberValue, coerceArray, coerceBooleanProperty, coerceCssPixelValue, coerceElement, coerceNumberProperty, coerceStringArray };
|
74 | //# sourceMappingURL=coercion.mjs.map
|