2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
import {Pipe, PipeTransform} from '@angular/core';
|
2016-05-31 15:22:59 -07:00
|
|
|
import {ListWrapper} from '../facade/collection';
|
2016-06-08 16:38:52 -07:00
|
|
|
import {StringWrapper, isArray, isBlank, isString} from '../facade/lang';
|
2016-08-25 00:50:16 -07:00
|
|
|
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
|
2015-08-30 17:04:48 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new List or String containing only a subset (slice) of the
|
|
|
|
* elements.
|
|
|
|
*
|
|
|
|
* The starting index of the subset to return is specified by the `start` parameter.
|
|
|
|
*
|
|
|
|
* The ending index of the subset to return is specified by the optional `end` parameter.
|
|
|
|
*
|
2015-11-17 09:41:31 -08:00
|
|
|
* ### Usage
|
2015-08-30 17:04:48 -07:00
|
|
|
*
|
|
|
|
* expression | slice:start[:end]
|
|
|
|
*
|
|
|
|
* All behavior is based on the expected behavior of the JavaScript API
|
|
|
|
* Array.prototype.slice() and String.prototype.slice()
|
|
|
|
*
|
|
|
|
* Where the input expression is a [List] or [String], and `start` is:
|
|
|
|
*
|
|
|
|
* - **a positive integer**: return the item at _start_ index and all items after
|
|
|
|
* in the list or string expression.
|
|
|
|
* - **a negative integer**: return the item at _start_ index from the end and all items after
|
|
|
|
* in the list or string expression.
|
|
|
|
* - **`|start|` greater than the size of the expression**: return an empty list or string.
|
|
|
|
* - **`|start|` negative greater than the size of the expression**: return entire list or
|
|
|
|
* string expression.
|
|
|
|
*
|
|
|
|
* and where `end` is:
|
|
|
|
*
|
|
|
|
* - **omitted**: return all items until the end of the input
|
|
|
|
* - **a positive integer**: return all items before _end_ index of the list or string
|
|
|
|
* expression.
|
|
|
|
* - **a negative integer**: return all items before _end_ index from the end of the list
|
|
|
|
* or string expression.
|
|
|
|
*
|
|
|
|
* When operating on a [List], the returned list is always a copy even when all
|
|
|
|
* the elements are being returned.
|
|
|
|
*
|
2016-02-18 17:14:40 +01:00
|
|
|
* When operating on a blank value, returns it.
|
|
|
|
*
|
2015-08-30 17:04:48 -07:00
|
|
|
* ## List Example
|
|
|
|
*
|
2015-11-23 16:02:19 -08:00
|
|
|
* This `ngFor` example:
|
2015-08-30 17:04:48 -07:00
|
|
|
*
|
2015-11-02 15:46:59 -08:00
|
|
|
* {@example core/pipes/ts/slice_pipe/slice_pipe_example.ts region='SlicePipe_list'}
|
2015-08-30 17:04:48 -07:00
|
|
|
*
|
|
|
|
* produces the following:
|
|
|
|
*
|
|
|
|
* <li>b</li>
|
|
|
|
* <li>c</li>
|
|
|
|
*
|
|
|
|
* ## String Examples
|
|
|
|
*
|
2015-11-02 15:46:59 -08:00
|
|
|
* {@example core/pipes/ts/slice_pipe/slice_pipe_example.ts region='SlicePipe_string'}
|
2016-05-27 11:24:05 -07:00
|
|
|
*
|
|
|
|
* @stable
|
2015-08-30 17:04:48 -07:00
|
|
|
*/
|
|
|
|
|
2015-10-27 12:43:00 -07:00
|
|
|
@Pipe({name: 'slice', pure: false})
|
2015-08-30 17:04:48 -07:00
|
|
|
export class SlicePipe implements PipeTransform {
|
2016-04-22 15:33:32 -07:00
|
|
|
transform(value: any, start: number, end: number = null): any {
|
2016-02-18 17:14:40 +01:00
|
|
|
if (isBlank(value)) return value;
|
2015-08-30 17:04:48 -07:00
|
|
|
if (!this.supports(value)) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new InvalidPipeArgumentError(SlicePipe, value);
|
2015-08-30 17:04:48 -07:00
|
|
|
}
|
|
|
|
if (isString(value)) {
|
|
|
|
return StringWrapper.slice(value, start, end);
|
|
|
|
}
|
|
|
|
return ListWrapper.slice(value, start, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
private supports(obj: any): boolean { return isString(obj) || isArray(obj); }
|
|
|
|
}
|