coercion.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. export { c as coerceBooleanProperty } from './boolean-property-DaaVhX5A.mjs';
  2. export { _ as _isNumberValue, a as coerceElement, c as coerceNumberProperty } from './element-x4z00URv.mjs';
  3. export { c as coerceArray } from './array-I1yfCXUO.mjs';
  4. export { c as coerceCssPixelValue } from './css-pixel-value-C_HEqLhI.mjs';
  5. import '@angular/core';
  6. /**
  7. * Coerces a value to an array of trimmed non-empty strings.
  8. * Any input that is not an array, `null` or `undefined` will be turned into a string
  9. * via `toString()` and subsequently split with the given separator.
  10. * `null` and `undefined` will result in an empty array.
  11. * This results in the following outcomes:
  12. * - `null` -> `[]`
  13. * - `[null]` -> `["null"]`
  14. * - `["a", "b ", " "]` -> `["a", "b"]`
  15. * - `[1, [2, 3]]` -> `["1", "2,3"]`
  16. * - `[{ a: 0 }]` -> `["[object Object]"]`
  17. * - `{ a: 0 }` -> `["[object", "Object]"]`
  18. *
  19. * Useful for defining CSS classes or table columns.
  20. * @param value the value to coerce into an array of strings
  21. * @param separator split-separator if value isn't an array
  22. */
  23. function coerceStringArray(value, separator = /\s+/) {
  24. const result = [];
  25. if (value != null) {
  26. const sourceValues = Array.isArray(value) ? value : `${value}`.split(separator);
  27. for (const sourceValue of sourceValues) {
  28. const trimmedString = `${sourceValue}`.trim();
  29. if (trimmedString) {
  30. result.push(trimmedString);
  31. }
  32. }
  33. }
  34. return result;
  35. }
  36. export { coerceStringArray };
  37. //# sourceMappingURL=coercion.mjs.map