Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/common/src/pipes/slice_pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class SlicePipe implements PipeTransform {
* - **if positive**: return all items before `end` index of the list or string.
* - **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: 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;

Expand Down
9 changes: 8 additions & 1 deletion packages/common/test/pipes/slice_pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,23 @@ import {expect} from '@angular/platform-browser/testing/src/matchers';
describe('supports', () => {
it('should support strings', () => { expect(() => pipe.transform(str, 0)).not.toThrow(); });
it('should support lists', () => { expect(() => pipe.transform(list, 0)).not.toThrow(); });
it('should support readonly lists',
() => { expect(() => pipe.transform(list as ReadonlyArray<number>, 0)).not.toThrow(); });

it('should not support other objects',
() => { expect(() => pipe.transform({}, 0)).toThrow(); });
// this would not compile
// so we cast as `any` to check that it throws for unsupported objects
() => { expect(() => pipe.transform({} as any, 0)).toThrow(); });
});

describe('transform', () => {

it('should return null if the value is null',
() => { 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 all items after START index when START is positive and END is omitted',
() => {
expect(pipe.transform(list, 3)).toEqual([4, 5]);
Expand Down
5 changes: 4 additions & 1 deletion tools/public_api_guard/common/common.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,10 @@ export interface PopStateEvent {
export declare function registerLocaleData(data: any, localeId?: string | any, extraData?: any): void;

export declare class SlicePipe implements PipeTransform {
transform(value: any, start: number, end?: number): any;
transform<T>(value: ReadonlyArray<T>, start: number, end?: number): Array<T>;
transform(value: string, start: number, end?: number): string;
transform(value: null, start: number, end?: number): null;
transform(value: undefined, start: number, end?: number): undefined;
}

export declare type Time = {
Expand Down