coercion.mjs 2.6 KB

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