Skip to content

Commit 0b50258

Browse files
piloopinvsavkin
authored andcommitted
feat(pipes): add limitTo pipe
1 parent 600d53c commit 0b50258

File tree

4 files changed

+157
-2
lines changed

4 files changed

+157
-2
lines changed

modules/angular2/pipes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export {ObservablePipe} from './src/change_detection/pipes/observable_pipe';
1212
export {JsonPipe} from './src/change_detection/pipes/json_pipe';
1313
export {IterableChanges} from './src/change_detection/pipes/iterable_changes';
1414
export {KeyValueChanges} from './src/change_detection/pipes/keyvalue_changes';
15+
export {LimitToPipe} from './src/change_detection/pipes/limit_to_pipe';

modules/angular2/src/change_detection/change_detection.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {PromisePipeFactory} from './pipes/promise_pipe';
1010
import {UpperCaseFactory} from './pipes/uppercase_pipe';
1111
import {LowerCaseFactory} from './pipes/lowercase_pipe';
1212
import {JsonPipe} from './pipes/json_pipe';
13+
import {LimitToPipeFactory} from './pipes/limit_to_pipe';
1314
import {NullPipeFactory} from './pipes/null_pipe';
1415
import {ChangeDetection, ProtoChangeDetector, ChangeDetectorDefinition} from './interfaces';
1516
import {Inject, Injectable, OpaqueToken, Optional} from 'angular2/di';
@@ -64,16 +65,25 @@ export const lowercase: List<PipeFactory> =
6465
*
6566
* @exportedAs angular2/pipes
6667
*/
67-
export const json: List<PipeFactory | Pipe> =
68+
export const json: List<PipeFactory> =
6869
CONST_EXPR([CONST_EXPR(new JsonPipe()), CONST_EXPR(new NullPipeFactory())]);
6970

71+
/**
72+
* LimitTo text transform.
73+
*
74+
* @exportedAs angular2/pipes
75+
*/
76+
export const limitTo: List<PipeFactory> =
77+
CONST_EXPR([CONST_EXPR(new LimitToPipeFactory()), CONST_EXPR(new NullPipeFactory())]);
78+
7079
export const defaultPipes = CONST_EXPR({
7180
"iterableDiff": iterableDiff,
7281
"keyValDiff": keyValDiff,
7382
"async": async,
7483
"uppercase": uppercase,
7584
"lowercase": lowercase,
76-
"json": json
85+
"json": json,
86+
"limitTo": limitTo
7787
});
7888

7989
/**
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {
2+
isBlank,
3+
isString,
4+
isArray,
5+
StringWrapper,
6+
BaseException,
7+
CONST
8+
} from 'angular2/src/facade/lang';
9+
import {ListWrapper} from 'angular2/src/facade/collection';
10+
import {Math} from 'angular2/src/facade/math';
11+
import {WrappedValue, Pipe, PipeFactory} from './pipe';
12+
import {ChangeDetectorRef} from '../change_detector_ref';
13+
14+
/**
15+
* Creates a new List or String containing only a prefix/suffix of the
16+
* elements.
17+
*
18+
* The number of elements to return is specified by the `limitTo` parameter.
19+
*
20+
* # Usage
21+
*
22+
* expression | limitTo:number
23+
*
24+
* Where the input expression is a [List] or [String], and `limitTo` is:
25+
*
26+
* - **a positive integer**: return _number_ items from the beginning of the list or string
27+
* expression.
28+
* - **a negative integer**: return _number_ items from the end of the list or string expression.
29+
* - **`|limitTo|` greater than the size of the expression**: return the entire expression.
30+
*
31+
* When operating on a [List], the returned list is always a copy even when all
32+
* the elements are being returned.
33+
*
34+
* # Examples
35+
*
36+
* ## List Example
37+
*
38+
* Assuming `var collection = ['a', 'b', 'c']`, this `ng-for` directive:
39+
*
40+
* <li *ng-for="var i in collection | limitTo:2">{{i}}</li>
41+
*
42+
* produces the following:
43+
*
44+
* <li>a</li>
45+
* <li>b</li>
46+
*
47+
* ## String Examples
48+
*
49+
* {{ 'abcdefghij' | limitTo: 4 }} // output is 'abcd'
50+
* {{ 'abcdefghij' | limitTo: -4 }} // output is 'ghij'
51+
* {{ 'abcdefghij' | limitTo: -100 }} // output is 'abcdefghij'
52+
*
53+
* @exportedAs angular2/pipes
54+
*/
55+
export class LimitToPipe implements Pipe {
56+
static supportsObj(obj): boolean { return isString(obj) || isArray(obj); }
57+
58+
supports(obj): boolean { return LimitToPipe.supportsObj(obj); }
59+
60+
transform(value, args: List<any> = null): any {
61+
if (isBlank(args) || args.length == 0) {
62+
throw new BaseException('limitTo pipe requires one argument');
63+
}
64+
var limit: int = args[0];
65+
var left = 0, right = Math.min(limit, value.length);
66+
if (limit < 0) {
67+
left = Math.max(0, value.length + limit);
68+
right = value.length;
69+
}
70+
if (isString(value)) {
71+
return StringWrapper.substring(value, left, right);
72+
}
73+
return ListWrapper.slice(value, left, right);
74+
}
75+
76+
onDestroy(): void {}
77+
}
78+
79+
/**
80+
* @exportedAs angular2/pipes
81+
*/
82+
@CONST()
83+
export class LimitToPipeFactory implements PipeFactory {
84+
supports(obj): boolean { return LimitToPipe.supportsObj(obj); }
85+
86+
create(cdRef: ChangeDetectorRef): Pipe { return new LimitToPipe(); }
87+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach} from 'angular2/test_lib';
2+
3+
import {LimitToPipe} from 'angular2/src/change_detection/pipes/limit_to_pipe';
4+
5+
export function main() {
6+
describe("LimitToPipe", () => {
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 LimitToPipe();
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 the first X items when X is positive', () => {
30+
expect(pipe.transform(list, [3])).toEqual([1, 2, 3]);
31+
expect(pipe.transform(str, [3])).toEqual('tuv');
32+
});
33+
34+
it('should return the last X items when X is negative', () => {
35+
expect(pipe.transform(list, [-3])).toEqual([3, 4, 5]);
36+
expect(pipe.transform(str, [-3])).toEqual('xyz');
37+
});
38+
39+
it('should return a copy of input array if X is exceeds array length', () => {
40+
expect(pipe.transform(list, [20])).toEqual(list);
41+
expect(pipe.transform(list, [-20])).toEqual(list);
42+
});
43+
44+
it('should return the entire string if X exceeds input length', () => {
45+
expect(pipe.transform(str, [20])).toEqual(str);
46+
expect(pipe.transform(str, [-20])).toEqual(str);
47+
});
48+
49+
it('should not modify the input list', () => {
50+
expect(pipe.transform(list, [3])).toEqual([1, 2, 3]);
51+
expect(list).toEqual([1, 2, 3, 4, 5]);
52+
});
53+
54+
});
55+
56+
});
57+
}

0 commit comments

Comments
 (0)