75 lines
2.6 KiB
JavaScript
75 lines
2.6 KiB
JavaScript
|
import { ElementRef } from '@angular/core';
|
||
|
|
||
|
/** Coerces a data-bound value (typically a string) to a boolean. */
|
||
|
function coerceBooleanProperty(value) {
|
||
|
return value != null && `${value}` !== 'false';
|
||
|
}
|
||
|
|
||
|
function coerceNumberProperty(value, fallbackValue = 0) {
|
||
|
return _isNumberValue(value) ? Number(value) : fallbackValue;
|
||
|
}
|
||
|
/**
|
||
|
* Whether the provided value is considered a number.
|
||
|
* @docs-private
|
||
|
*/
|
||
|
function _isNumberValue(value) {
|
||
|
// parseFloat(value) handles most of the cases we're interested in (it treats null, empty string,
|
||
|
// and other non-number values as NaN, where Number just uses 0) but it considers the string
|
||
|
// '123hello' to be a valid number. Therefore we also check if Number(value) is NaN.
|
||
|
return !isNaN(parseFloat(value)) && !isNaN(Number(value));
|
||
|
}
|
||
|
|
||
|
function coerceArray(value) {
|
||
|
return Array.isArray(value) ? value : [value];
|
||
|
}
|
||
|
|
||
|
/** Coerces a value to a CSS pixel value. */
|
||
|
function coerceCssPixelValue(value) {
|
||
|
if (value == null) {
|
||
|
return '';
|
||
|
}
|
||
|
return typeof value === 'string' ? value : `${value}px`;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Coerces an ElementRef or an Element into an element.
|
||
|
* Useful for APIs that can accept either a ref or the native element itself.
|
||
|
*/
|
||
|
function coerceElement(elementOrRef) {
|
||
|
return elementOrRef instanceof ElementRef ? elementOrRef.nativeElement : elementOrRef;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Coerces a value to an array of trimmed non-empty strings.
|
||
|
* Any input that is not an array, `null` or `undefined` will be turned into a string
|
||
|
* via `toString()` and subsequently split with the given separator.
|
||
|
* `null` and `undefined` will result in an empty array.
|
||
|
* This results in the following outcomes:
|
||
|
* - `null` -> `[]`
|
||
|
* - `[null]` -> `["null"]`
|
||
|
* - `["a", "b ", " "]` -> `["a", "b"]`
|
||
|
* - `[1, [2, 3]]` -> `["1", "2,3"]`
|
||
|
* - `[{ a: 0 }]` -> `["[object Object]"]`
|
||
|
* - `{ a: 0 }` -> `["[object", "Object]"]`
|
||
|
*
|
||
|
* Useful for defining CSS classes or table columns.
|
||
|
* @param value the value to coerce into an array of strings
|
||
|
* @param separator split-separator if value isn't an array
|
||
|
*/
|
||
|
function coerceStringArray(value, separator = /\s+/) {
|
||
|
const result = [];
|
||
|
if (value != null) {
|
||
|
const sourceValues = Array.isArray(value) ? value : `${value}`.split(separator);
|
||
|
for (const sourceValue of sourceValues) {
|
||
|
const trimmedString = `${sourceValue}`.trim();
|
||
|
if (trimmedString) {
|
||
|
result.push(trimmedString);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
export { _isNumberValue, coerceArray, coerceBooleanProperty, coerceCssPixelValue, coerceElement, coerceNumberProperty, coerceStringArray };
|
||
|
//# sourceMappingURL=coercion.mjs.map
|