Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdk/coercion): add coercion for string arrays (#20652)
Co-authored-by: Jan Malchert <25508038+JanMalch@users.noreply.github.com>"
- Loading branch information
Showing
8 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {coerceStringArray} from '@angular/cdk/coercion/string-array'; | ||
|
||
describe('coerceStringArray', () => { | ||
it('should split a string', () => { | ||
expect(coerceStringArray('x y z 1')).toEqual(['x', 'y', 'z', '1']); | ||
}); | ||
|
||
it('should map values to string in an array', () => { | ||
expect(coerceStringArray(['x', 1, true, null, undefined, ['arr', 'ay'], { data: false }])) | ||
.toEqual(['x', '1', 'true', 'null', 'undefined', 'arr,ay', '[object Object]']); | ||
}); | ||
|
||
it('should work with a custom delimiter', () => { | ||
expect(coerceStringArray('1::2::3::4', '::')).toEqual(['1', '2', '3', '4']); | ||
}); | ||
|
||
it('should trim values and remove empty values', () => { | ||
expect(coerceStringArray(', x, ,, ', ',')).toEqual(['x']); | ||
}); | ||
|
||
it('should map non-string values to string', () => { | ||
expect(coerceStringArray(0)).toEqual(['0']); | ||
}); | ||
|
||
it('should return an empty array for null', () => { | ||
expect(coerceStringArray(null)).toEqual([]); | ||
}); | ||
|
||
it('should return an empty array for undefined', () => { | ||
expect(coerceStringArray(undefined)).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
/** | ||
* 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 | ||
*/ | ||
export function coerceStringArray(value: any, separator: string | RegExp = /\s+/): string[] { | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters