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:
Andrea Canciani 2020-03-27 11:51:17 +01:00 committed by Alex Rickabaugh
parent 4dfe0fa068
commit 4744c229db
3 changed files with 11 additions and 8 deletions

View File

@ -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 = {

View File

@ -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);

View File

@ -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',