1 | import { ElementRef } from '@angular/core';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * @license
|
---|
5 | * Copyright Google LLC All Rights Reserved.
|
---|
6 | *
|
---|
7 | * Use of this source code is governed by an MIT-style license that can be
|
---|
8 | * found in the LICENSE file at https://angular.io/license
|
---|
9 | */
|
---|
10 | /** Coerces a data-bound value (typically a string) to a boolean. */
|
---|
11 | function coerceBooleanProperty(value) {
|
---|
12 | return value != null && `${value}` !== 'false';
|
---|
13 | }
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * @license
|
---|
17 | * Copyright Google LLC All Rights Reserved.
|
---|
18 | *
|
---|
19 | * Use of this source code is governed by an MIT-style license that can be
|
---|
20 | * found in the LICENSE file at https://angular.io/license
|
---|
21 | */
|
---|
22 | function coerceNumberProperty(value, fallbackValue = 0) {
|
---|
23 | return _isNumberValue(value) ? Number(value) : fallbackValue;
|
---|
24 | }
|
---|
25 | /**
|
---|
26 | * Whether the provided value is considered a number.
|
---|
27 | * @docs-private
|
---|
28 | */
|
---|
29 | function _isNumberValue(value) {
|
---|
30 | // parseFloat(value) handles most of the cases we're interested in (it treats null, empty string,
|
---|
31 | // and other non-number values as NaN, where Number just uses 0) but it considers the string
|
---|
32 | // '123hello' to be a valid number. Therefore we also check if Number(value) is NaN.
|
---|
33 | return !isNaN(parseFloat(value)) && !isNaN(Number(value));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @license
|
---|
38 | * Copyright Google LLC All Rights Reserved.
|
---|
39 | *
|
---|
40 | * Use of this source code is governed by an MIT-style license that can be
|
---|
41 | * found in the LICENSE file at https://angular.io/license
|
---|
42 | */
|
---|
43 | function coerceArray(value) {
|
---|
44 | return Array.isArray(value) ? value : [value];
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * @license
|
---|
49 | * Copyright Google LLC All Rights Reserved.
|
---|
50 | *
|
---|
51 | * Use of this source code is governed by an MIT-style license that can be
|
---|
52 | * found in the LICENSE file at https://angular.io/license
|
---|
53 | */
|
---|
54 | /** Coerces a value to a CSS pixel value. */
|
---|
55 | function coerceCssPixelValue(value) {
|
---|
56 | if (value == null) {
|
---|
57 | return '';
|
---|
58 | }
|
---|
59 | return typeof value === 'string' ? value : `${value}px`;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * @license
|
---|
64 | * Copyright Google LLC All Rights Reserved.
|
---|
65 | *
|
---|
66 | * Use of this source code is governed by an MIT-style license that can be
|
---|
67 | * found in the LICENSE file at https://angular.io/license
|
---|
68 | */
|
---|
69 | /**
|
---|
70 | * Coerces an ElementRef or an Element into an element.
|
---|
71 | * Useful for APIs that can accept either a ref or the native element itself.
|
---|
72 | */
|
---|
73 | function coerceElement(elementOrRef) {
|
---|
74 | return elementOrRef instanceof ElementRef ? elementOrRef.nativeElement : elementOrRef;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * @license
|
---|
79 | * Copyright Google LLC All Rights Reserved.
|
---|
80 | *
|
---|
81 | * Use of this source code is governed by an MIT-style license that can be
|
---|
82 | * found in the LICENSE file at https://angular.io/license
|
---|
83 | */
|
---|
84 | /**
|
---|
85 | * Coerces a value to an array of trimmed non-empty strings.
|
---|
86 | * Any input that is not an array, `null` or `undefined` will be turned into a string
|
---|
87 | * via `toString()` and subsequently split with the given separator.
|
---|
88 | * `null` and `undefined` will result in an empty array.
|
---|
89 | * This results in the following outcomes:
|
---|
90 | * - `null` -> `[]`
|
---|
91 | * - `[null]` -> `["null"]`
|
---|
92 | * - `["a", "b ", " "]` -> `["a", "b"]`
|
---|
93 | * - `[1, [2, 3]]` -> `["1", "2,3"]`
|
---|
94 | * - `[{ a: 0 }]` -> `["[object Object]"]`
|
---|
95 | * - `{ a: 0 }` -> `["[object", "Object]"]`
|
---|
96 | *
|
---|
97 | * Useful for defining CSS classes or table columns.
|
---|
98 | * @param value the value to coerce into an array of strings
|
---|
99 | * @param separator split-separator if value isn't an array
|
---|
100 | */
|
---|
101 | function coerceStringArray(value, separator = /\s+/) {
|
---|
102 | const result = [];
|
---|
103 | if (value != null) {
|
---|
104 | const sourceValues = Array.isArray(value) ? value : `${value}`.split(separator);
|
---|
105 | for (const sourceValue of sourceValues) {
|
---|
106 | const trimmedString = `${sourceValue}`.trim();
|
---|
107 | if (trimmedString) {
|
---|
108 | result.push(trimmedString);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return result;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * @license
|
---|
117 | * Copyright Google LLC All Rights Reserved.
|
---|
118 | *
|
---|
119 | * Use of this source code is governed by an MIT-style license that can be
|
---|
120 | * found in the LICENSE file at https://angular.io/license
|
---|
121 | */
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * @license
|
---|
125 | * Copyright Google LLC All Rights Reserved.
|
---|
126 | *
|
---|
127 | * Use of this source code is governed by an MIT-style license that can be
|
---|
128 | * found in the LICENSE file at https://angular.io/license
|
---|
129 | */
|
---|
130 |
|
---|
131 | export { _isNumberValue, coerceArray, coerceBooleanProperty, coerceCssPixelValue, coerceElement, coerceNumberProperty, coerceStringArray };
|
---|
132 | //# sourceMappingURL=coercion.js.map
|
---|