Skip to content

Commit d890c4f

Browse files
itslennyvsavkin
authored andcommitted
refactor(pipes): remove LimitTo pipe in favor of slice pipe
1 parent c2043ec commit d890c4f

File tree

9 files changed

+22
-144
lines changed

9 files changed

+22
-144
lines changed

modules/angular2/src/core/facade/collection.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ class ListWrapper {
182182
}
183183

184184
static List slice(List l, [int from = 0, int to]) {
185+
from = _startOffset(l, from);
186+
to = _endOffset(l, to);
185187
//in JS if from > to an empty array is returned
186188
if(to != null && from > to) {
187189
return [];
188190
}
189-
return l.sublist(_startOffset(l, from), _endOffset(l, to));
191+
return l.sublist(from, to);
190192
}
191193

192194
static List splice(List l, int from, int length) {
@@ -213,7 +215,7 @@ class ListWrapper {
213215
// the end of the list
214216
static int _startOffset(List l, int start) {
215217
int len = l.length;
216-
return start = start < 0 ? max(len + start, 0) : min(start, len);
218+
return start < 0 ? max(len + start, 0) : min(start, len);
217219
}
218220

219221
// JS splice, slice, fill functions can take end < 0 which indicates a position relative to

modules/angular2/src/core/facade/lang.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,13 @@ class StringWrapper {
9595
}
9696

9797
static String slice(String s, [int start = 0, int end]) {
98+
start = _startOffset(s, start);
99+
end = _endOffset(s, end);
98100
//in JS if start > end an empty string is returned
99101
if(end != null && start > end) {
100102
return "";
101103
}
102-
return s.substring(_startOffset(s, start), _endOffset(s, end));
104+
return s.substring(start, end);
103105
}
104106

105107
static String substring(String s, int start, [int end]) {
@@ -120,7 +122,7 @@ class StringWrapper {
120122
// the end of the string
121123
static int _startOffset(String s, int start) {
122124
int len = s.length;
123-
return start = start < 0 ? math.max(len + start, 0) : math.min(start, len);
125+
return start < 0 ? math.max(len + start, 0) : math.min(start, len);
124126
}
125127

126128
// JS slice function can take end < 0 which indicates a position relative to

modules/angular2/src/core/pipes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export {AsyncPipe} from './pipes/async_pipe';
88
export {DatePipe} from './pipes/date_pipe';
99
export {DEFAULT_PIPES, DEFAULT_PIPES_TOKEN} from './pipes/default_pipes';
1010
export {JsonPipe} from './pipes/json_pipe';
11-
export {LimitToPipe} from './pipes/limit_to_pipe';
11+
export {SlicePipe} from './pipes/slice_pipe';
1212
export {LowerCasePipe} from './pipes/lowercase_pipe';
1313
export {NumberPipe, DecimalPipe, PercentPipe, CurrencyPipe} from './pipes/number_pipe';
1414
export {UpperCasePipe} from './pipes/uppercase_pipe';

modules/angular2/src/core/pipes/default_pipes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {AsyncPipe} from './async_pipe';
22
import {UpperCasePipe} from './uppercase_pipe';
33
import {LowerCasePipe} from './lowercase_pipe';
44
import {JsonPipe} from './json_pipe';
5-
import {LimitToPipe} from './limit_to_pipe';
5+
import {SlicePipe} from './slice_pipe';
66
import {DatePipe} from './date_pipe';
77
import {DecimalPipe, PercentPipe, CurrencyPipe} from './number_pipe';
88

@@ -14,7 +14,7 @@ const DEFAULT_PIPES_LIST = CONST_EXPR([
1414
UpperCasePipe,
1515
LowerCasePipe,
1616
JsonPipe,
17-
LimitToPipe,
17+
SlicePipe,
1818
DecimalPipe,
1919
PercentPipe,
2020
CurrencyPipe,

modules/angular2/src/core/pipes/limit_to_pipe.ts

Lines changed: 0 additions & 73 deletions
This file was deleted.

modules/angular2/src/core/pipes/slice_pipe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {
33
isString,
44
isArray,
55
StringWrapper,
6-
BaseException,
76
CONST
87
} from 'angular2/src/core/facade/lang';
8+
import {BaseException} from 'angular2/src/core/facade/exceptions';
99
import {ListWrapper} from 'angular2/src/core/facade/collection';
1010
import {Injectable} from 'angular2/di';
1111

@@ -76,7 +76,7 @@ import {Pipe} from '../metadata';
7676
@Pipe({name: 'slice'})
7777
@Injectable()
7878
export class SlicePipe implements PipeTransform {
79-
transform(value: any, args: List<any> = null): any {
79+
transform(value: any, args: any[] = null): any {
8080
if (isBlank(args) || args.length == 0) {
8181
throw new BaseException('Slice pipe requires one argument');
8282
}
@@ -85,7 +85,7 @@ export class SlicePipe implements PipeTransform {
8585
}
8686
if (isBlank(value)) return value;
8787
var start: number = args[0];
88-
var end: number = args.length > 1 ? args[1] : value.length;
88+
var end: number = args.length > 1 ? args[1] : null;
8989
if (isString(value)) {
9090
return StringWrapper.slice(value, start, end);
9191
}

modules/angular2/test/core/facade/collection_spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ export function main() {
6363
it('should support negative end',
6464
() => { expect(ListWrapper.slice(l, -3, -1)).toEqual([2, 3]); });
6565

66-
it('should return empty list if start is greater than end',
67-
() => { expect(ListWrapper.slice(l, 4, 2)).toEqual([]); });
66+
it('should return empty list if start is greater than end', () => {
67+
expect(ListWrapper.slice(l, 4, 2)).toEqual([]);
68+
expect(ListWrapper.slice(l, -2, -4)).toEqual([]);
69+
});
6870
});
6971

7072
describe('indexOf', () => {

modules/angular2/test/core/facade/lang_spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ export function main() {
7676
it('should support negative end',
7777
() => { expect(StringWrapper.slice(s, -3, -1)).toEqual("hi"); });
7878

79-
it('should return empty string if start is greater than end',
80-
() => { expect(StringWrapper.slice(s, 4, 2)).toEqual(""); });
79+
it('should return empty string if start is greater than end', () => {
80+
expect(StringWrapper.slice(s, 4, 2)).toEqual("");
81+
expect(StringWrapper.slice(s, -2, -4)).toEqual("");
82+
});
8183
});
8284

8385
});

modules/angular2/test/core/pipes/limit_to_pipe_spec.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)