[6a3a178] | 1 | /**
|
---|
| 2 | * @license
|
---|
| 3 | * Copyright Google LLC All Rights Reserved.
|
---|
| 4 | *
|
---|
| 5 | * Use of this source code is governed by an MIT-style license that can be
|
---|
| 6 | * found in the LICENSE file at https://angular.io/license
|
---|
| 7 | */
|
---|
| 8 | /**
|
---|
| 9 | * Coerces a value to an array of trimmed non-empty strings.
|
---|
| 10 | * Any input that is not an array, `null` or `undefined` will be turned into a string
|
---|
| 11 | * via `toString()` and subsequently split with the given separator.
|
---|
| 12 | * `null` and `undefined` will result in an empty array.
|
---|
| 13 | * This results in the following outcomes:
|
---|
| 14 | * - `null` -> `[]`
|
---|
| 15 | * - `[null]` -> `["null"]`
|
---|
| 16 | * - `["a", "b ", " "]` -> `["a", "b"]`
|
---|
| 17 | * - `[1, [2, 3]]` -> `["1", "2,3"]`
|
---|
| 18 | * - `[{ a: 0 }]` -> `["[object Object]"]`
|
---|
| 19 | * - `{ a: 0 }` -> `["[object", "Object]"]`
|
---|
| 20 | *
|
---|
| 21 | * Useful for defining CSS classes or table columns.
|
---|
| 22 | * @param value the value to coerce into an array of strings
|
---|
| 23 | * @param separator split-separator if value isn't an array
|
---|
| 24 | */
|
---|
| 25 | export function coerceStringArray(value, separator = /\s+/) {
|
---|
| 26 | const result = [];
|
---|
| 27 | if (value != null) {
|
---|
| 28 | const sourceValues = Array.isArray(value) ? value : `${value}`.split(separator);
|
---|
| 29 | for (const sourceValue of sourceValues) {
|
---|
| 30 | const trimmedString = `${sourceValue}`.trim();
|
---|
| 31 | if (trimmedString) {
|
---|
| 32 | result.push(trimmedString);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 | return result;
|
---|
| 37 | }
|
---|
| 38 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3RyaW5nLWFycmF5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2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