-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat: add coercion for string arrays #17523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
52a2892
feat: add coercion for string arrays
JanMalch cd0639f
refactor(coercion): use space as default separator for coerceStringArray
JanMalch 174aac8
feat(autocomplete): use coerceStringArray for CSS class input
JanMalch c4da94d
refactor(datepicker): use coerceStringArray for panelClass input
JanMalch 72daa7b
refactor(coercion): improve coerceStringArray implementation
JanMalch c3747bd
test(datepicker): add test for panelClass
JanMalch f7d28e2
test(coercion): add coerceStringArray test for custom delimiter
JanMalch 1f010f6
Merge branch 'master' into feat/coerce-string-array
JanMalch 93496a0
fix: add missing declarations to public_api_guard
JanMalch ffd7f0d
fix: update fork and fix declaration in public_api_guard
JanMalch e14dbe2
Merge branch 'master' into feat/coerce-string-array
JanMalch 1ba70b0
fix: fix datepicker panelClass declaration in public_api_guard
JanMalch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,41 @@ | ||
/** | ||
* @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. | ||
JanMalch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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. | ||
JanMalch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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; | ||
} | ||
JanMalch marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.