refactor(pipes): remove LimitTo pipe in favor of slice pipe
This commit is contained in:
@ -2,7 +2,7 @@ import {AsyncPipe} from './async_pipe';
|
||||
import {UpperCasePipe} from './uppercase_pipe';
|
||||
import {LowerCasePipe} from './lowercase_pipe';
|
||||
import {JsonPipe} from './json_pipe';
|
||||
import {LimitToPipe} from './limit_to_pipe';
|
||||
import {SlicePipe} from './slice_pipe';
|
||||
import {DatePipe} from './date_pipe';
|
||||
import {DecimalPipe, PercentPipe, CurrencyPipe} from './number_pipe';
|
||||
|
||||
@ -14,7 +14,7 @@ const DEFAULT_PIPES_LIST = CONST_EXPR([
|
||||
UpperCasePipe,
|
||||
LowerCasePipe,
|
||||
JsonPipe,
|
||||
LimitToPipe,
|
||||
SlicePipe,
|
||||
DecimalPipe,
|
||||
PercentPipe,
|
||||
CurrencyPipe,
|
||||
|
@ -1,73 +0,0 @@
|
||||
import {isBlank, isString, isArray, StringWrapper, CONST} from 'angular2/src/core/facade/lang';
|
||||
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
||||
import {ListWrapper} from 'angular2/src/core/facade/collection';
|
||||
import {Math} from 'angular2/src/core/facade/math';
|
||||
import {PipeTransform, WrappedValue} from 'angular2/src/core/change_detection';
|
||||
import {Injectable} from 'angular2/src/core/di';
|
||||
import {Pipe} from 'angular2/src/core/metadata';
|
||||
import {InvalidPipeArgumentException} from './invalid_pipe_argument_exception';
|
||||
|
||||
/**
|
||||
* Creates a new Array or String containing only a prefix/suffix of the
|
||||
* elements.
|
||||
*
|
||||
* The number of elements to return is specified by the `limitTo` parameter.
|
||||
*
|
||||
* # Usage
|
||||
*
|
||||
* expression | limitTo:number
|
||||
*
|
||||
* Where the input expression is a [Array] or [String], and `limitTo` is:
|
||||
*
|
||||
* - **a positive integer**: return _number_ items from the beginning of the list or string
|
||||
* expression.
|
||||
* - **a negative integer**: return _number_ items from the end of the list or string expression.
|
||||
* - **`|limitTo|` greater than the size of the expression**: return the entire expression.
|
||||
*
|
||||
* When operating on a [Array], the returned list is always a copy even when all
|
||||
* the elements are being returned.
|
||||
*
|
||||
* # Examples
|
||||
*
|
||||
* ## Array Example
|
||||
*
|
||||
* Assuming `var collection = ['a', 'b', 'c']`, this `ng-for` directive:
|
||||
*
|
||||
* <li *ng-for="var i in collection | limitTo:2">{{i}}</li>
|
||||
*
|
||||
* produces the following:
|
||||
*
|
||||
* <li>a</li>
|
||||
* <li>b</li>
|
||||
*
|
||||
* ## String Examples
|
||||
*
|
||||
* {{ 'abcdefghij' | limitTo: 4 }} // output is 'abcd'
|
||||
* {{ 'abcdefghij' | limitTo: -4 }} // output is 'ghij'
|
||||
* {{ 'abcdefghij' | limitTo: -100 }} // output is 'abcdefghij'
|
||||
*/
|
||||
@Pipe({name: 'limitTo'})
|
||||
@Injectable()
|
||||
export class LimitToPipe implements PipeTransform {
|
||||
supports(obj: any): boolean { return isString(obj) || isArray(obj); }
|
||||
|
||||
transform(value: any, args: any[] = null): any {
|
||||
if (isBlank(args) || args.length == 0) {
|
||||
throw new BaseException('limitTo pipe requires one argument');
|
||||
}
|
||||
if (!this.supports(value)) {
|
||||
throw new InvalidPipeArgumentException(LimitToPipe, value);
|
||||
}
|
||||
if (isBlank(value)) return value;
|
||||
var limit: number = args[0];
|
||||
var left = 0, right = Math.min(limit, value.length);
|
||||
if (limit < 0) {
|
||||
left = Math.max(0, value.length + limit);
|
||||
right = value.length;
|
||||
}
|
||||
if (isString(value)) {
|
||||
return StringWrapper.substring(value, left, right);
|
||||
}
|
||||
return ListWrapper.slice(value, left, right);
|
||||
}
|
||||
}
|
@ -3,9 +3,9 @@ import {
|
||||
isString,
|
||||
isArray,
|
||||
StringWrapper,
|
||||
BaseException,
|
||||
CONST
|
||||
} from 'angular2/src/core/facade/lang';
|
||||
import {BaseException} from 'angular2/src/core/facade/exceptions';
|
||||
import {ListWrapper} from 'angular2/src/core/facade/collection';
|
||||
import {Injectable} from 'angular2/di';
|
||||
|
||||
@ -76,7 +76,7 @@ import {Pipe} from '../metadata';
|
||||
@Pipe({name: 'slice'})
|
||||
@Injectable()
|
||||
export class SlicePipe implements PipeTransform {
|
||||
transform(value: any, args: List<any> = null): any {
|
||||
transform(value: any, args: any[] = null): any {
|
||||
if (isBlank(args) || args.length == 0) {
|
||||
throw new BaseException('Slice pipe requires one argument');
|
||||
}
|
||||
@ -85,7 +85,7 @@ export class SlicePipe implements PipeTransform {
|
||||
}
|
||||
if (isBlank(value)) return value;
|
||||
var start: number = args[0];
|
||||
var end: number = args.length > 1 ? args[1] : value.length;
|
||||
var end: number = args.length > 1 ? args[1] : null;
|
||||
if (isString(value)) {
|
||||
return StringWrapper.slice(value, start, end);
|
||||
}
|
||||
|
Reference in New Issue
Block a user