X Tutup
Skip to content

Commit c2043ec

Browse files
itslennyvsavkin
authored andcommitted
feat(pipes): add slice pipe that supports start and end indexes
1 parent 0808eea commit c2043ec

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import {
2+
isBlank,
3+
isString,
4+
isArray,
5+
StringWrapper,
6+
BaseException,
7+
CONST
8+
} from 'angular2/src/core/facade/lang';
9+
import {ListWrapper} from 'angular2/src/core/facade/collection';
10+
import {Injectable} from 'angular2/di';
11+
12+
import {PipeTransform, WrappedValue} from 'angular2/change_detection';
13+
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
14+
15+
import {Pipe} from '../metadata';
16+
17+
/**
18+
* Creates a new List or String containing only a subset (slice) of the
19+
* elements.
20+
*
21+
* The starting index of the subset to return is specified by the `start` parameter.
22+
*
23+
* The ending index of the subset to return is specified by the optional `end` parameter.
24+
*
25+
* # Usage
26+
*
27+
* expression | slice:start[:end]
28+
*
29+
* All behavior is based on the expected behavior of the JavaScript API
30+
* Array.prototype.slice() and String.prototype.slice()
31+
*
32+
* Where the input expression is a [List] or [String], and `start` is:
33+
*
34+
* - **a positive integer**: return the item at _start_ index and all items after
35+
* in the list or string expression.
36+
* - **a negative integer**: return the item at _start_ index from the end and all items after
37+
* in the list or string expression.
38+
* - **`|start|` greater than the size of the expression**: return an empty list or string.
39+
* - **`|start|` negative greater than the size of the expression**: return entire list or
40+
* string expression.
41+
*
42+
* and where `end` is:
43+
*
44+
* - **omitted**: return all items until the end of the input
45+
* - **a positive integer**: return all items before _end_ index of the list or string
46+
* expression.
47+
* - **a negative integer**: return all items before _end_ index from the end of the list
48+
* or string expression.
49+
*
50+
* When operating on a [List], the returned list is always a copy even when all
51+
* the elements are being returned.
52+
*
53+
* # Examples
54+
*
55+
* ## List Example
56+
*
57+
* Assuming `var collection = ['a', 'b', 'c', 'd']`, this `ng-for` directive:
58+
*
59+
* <li *ng-for="var i in collection | slice:1:3">{{i}}</li>
60+
*
61+
* produces the following:
62+
*
63+
* <li>b</li>
64+
* <li>c</li>
65+
*
66+
* ## String Examples
67+
*
68+
* {{ 'abcdefghij' | slice:0:4 }} // output is 'abcd'
69+
* {{ 'abcdefghij' | slice:4:0 }} // output is ''
70+
* {{ 'abcdefghij' | slice:-4 }} // output is 'ghij'
71+
* {{ 'abcdefghij' | slice:-4,-2 }} // output is 'gh'
72+
* {{ 'abcdefghij' | slice: -100 }} // output is 'abcdefghij'
73+
* {{ 'abcdefghij' | slice: 100 }} // output is ''
74+
*/
75+
76+
@Pipe({name: 'slice'})
77+
@Injectable()
78+
export class SlicePipe implements PipeTransform {
79+
transform(value: any, args: List<any> = null): any {
80+
if (isBlank(args) || args.length == 0) {
81+
throw new BaseException('Slice pipe requires one argument');
82+
}
83+
if (!this.supports(value)) {
84+
throw new InvalidPipeArgumentException(SlicePipe, value);
85+
}
86+
if (isBlank(value)) return value;
87+
var start: number = args[0];
88+
var end: number = args.length > 1 ? args[1] : value.length;
89+
if (isString(value)) {
90+
return StringWrapper.slice(value, start, end);
91+
}
92+
return ListWrapper.slice(value, start, end);
93+
}
94+
95+
private supports(obj: any): boolean { return isString(obj) || isArray(obj); }
96+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
2+
3+
import {SlicePipe} from 'angular2/pipes';
4+
5+
export function main() {
6+
describe("SlicePipe", () => {
7+
var list;
8+
var str;
9+
var pipe;
10+
11+
beforeEach(() => {
12+
list = [1, 2, 3, 4, 5];
13+
str = 'tuvwxyz';
14+
pipe = new SlicePipe();
15+
});
16+
17+
describe("supports", () => {
18+
it("should support strings", () => { expect(pipe.supports(str)).toBe(true); });
19+
it("should support lists", () => { expect(pipe.supports(list)).toBe(true); });
20+
21+
it("should not support other objects", () => {
22+
expect(pipe.supports(new Object())).toBe(false);
23+
expect(pipe.supports(null)).toBe(false);
24+
});
25+
});
26+
27+
describe("transform", () => {
28+
29+
it('should return all items after START index when START is positive and END is omitted',
30+
() => {
31+
expect(pipe.transform(list, [3])).toEqual([4, 5]);
32+
expect(pipe.transform(str, [3])).toEqual('wxyz');
33+
});
34+
35+
it('should return last START items when START is negative and END is omitted', () => {
36+
expect(pipe.transform(list, [-3])).toEqual([3, 4, 5]);
37+
expect(pipe.transform(str, [-3])).toEqual('xyz');
38+
});
39+
40+
it('should return all items between START and END index when START and END are positive',
41+
() => {
42+
expect(pipe.transform(list, [1, 3])).toEqual([2, 3]);
43+
expect(pipe.transform(str, [1, 3])).toEqual('uv');
44+
});
45+
46+
it('should return all items between START and END from the end when START and END are negative',
47+
() => {
48+
expect(pipe.transform(list, [-4, -2])).toEqual([2, 3]);
49+
expect(pipe.transform(str, [-4, -2])).toEqual('wx');
50+
});
51+
52+
it('should return an empty value if START is greater than END', () => {
53+
expect(pipe.transform(list, [4, 2])).toEqual([]);
54+
expect(pipe.transform(str, [4, 2])).toEqual('');
55+
});
56+
57+
it('should return an empty value if START greater than input length', () => {
58+
expect(pipe.transform(list, [99])).toEqual([]);
59+
expect(pipe.transform(str, [99])).toEqual('');
60+
});
61+
62+
it('should return entire input if START is negative and greater than input length', () => {
63+
expect(pipe.transform(list, [-99])).toEqual([1, 2, 3, 4, 5]);
64+
expect(pipe.transform(str, [-99])).toEqual('tuvwxyz');
65+
});
66+
67+
it('should not modify the input list', () => {
68+
expect(pipe.transform(list, [2])).toEqual([3, 4, 5]);
69+
expect(list).toEqual([1, 2, 3, 4, 5]);
70+
});
71+
72+
});
73+
74+
});
75+
}

0 commit comments

Comments
 (0)
X Tutup