fix(common): correct typing and implementation of `SlicePipe` (#37447)
Even in the overloads, state that it can accept `null` and `undefined`, in order to ensure easy composition with `async`. Additionally, change the implementation to return `null` on an `undefined` input, for consistency with other pipes. BREAKING CHANGE: The `slice` pipe now returns `null` for the `undefined` input value, which is consistent with the behavior of most pipes. If you rely on `undefined` being the result in that case, you now need to check for it explicitly. PR Close #37447
This commit is contained in:
parent
4dfe0fa068
commit
4744c229db
|
@ -381,9 +381,10 @@ export declare function registerLocaleData(data: any, localeId?: string | any, e
|
|||
|
||||
export declare class SlicePipe implements PipeTransform {
|
||||
transform<T>(value: ReadonlyArray<T>, start: number, end?: number): Array<T>;
|
||||
transform(value: null | undefined, start: number, end?: number): null;
|
||||
transform<T>(value: ReadonlyArray<T> | null | undefined, start: number, end?: number): Array<T> | null;
|
||||
transform(value: string, start: number, end?: number): string;
|
||||
transform(value: null, start: number, end?: number): null;
|
||||
transform(value: undefined, start: number, end?: number): undefined;
|
||||
transform(value: string | null | undefined, start: number, end?: number): string | null;
|
||||
}
|
||||
|
||||
export declare type Time = {
|
||||
|
|
|
@ -62,11 +62,13 @@ export class SlicePipe implements PipeTransform {
|
|||
* - **if negative**: return all items before `end` index from the end of the list or string.
|
||||
*/
|
||||
transform<T>(value: ReadonlyArray<T>, start: number, end?: number): Array<T>;
|
||||
transform(value: null|undefined, start: number, end?: number): null;
|
||||
transform<T>(value: ReadonlyArray<T>|null|undefined, start: number, end?: number): Array<T>|null;
|
||||
transform(value: string, start: number, end?: number): string;
|
||||
transform(value: null, start: number, end?: number): null;
|
||||
transform(value: undefined, start: number, end?: number): undefined;
|
||||
transform(value: any, start: number, end?: number): any {
|
||||
if (value == null) return value;
|
||||
transform(value: string|null|undefined, start: number, end?: number): string|null;
|
||||
transform<T>(value: ReadonlyArray<T>|string|null|undefined, start: number, end?: number):
|
||||
Array<T>|string|null {
|
||||
if (value == null) return null;
|
||||
|
||||
if (!this.supports(value)) {
|
||||
throw invalidPipeArgumentError(SlicePipe, value);
|
||||
|
|
|
@ -47,8 +47,8 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
|
|||
expect(pipe.transform(null, 1)).toBe(null);
|
||||
});
|
||||
|
||||
it('should return undefined if the value is undefined', () => {
|
||||
expect(pipe.transform(undefined, 1)).toBe(undefined);
|
||||
it('should return null if the value is undefined', () => {
|
||||
expect(pipe.transform(undefined, 1)).toBe(null);
|
||||
});
|
||||
|
||||
it('should return all items after START index when START is positive and END is omitted',
|
||||
|
|
Loading…
Reference in New Issue