diff --git a/src/ng/filter/limitTo.js b/src/ng/filter/limitTo.js index bb1756f2c86b..ee253eb3565b 100644 --- a/src/ng/filter/limitTo.js +++ b/src/ng/filter/limitTo.js @@ -7,14 +7,16 @@ * * @description * Creates a new array or string containing only a specified number of elements. The elements - * are taken from either the beginning or the end of the source array or string, as specified by - * the value and sign (positive or negative) of `limit`. + * are taken from either the beginning or the end or specified beginning index of the source array + * or string, as specified by the value and sign (positive or negative) of `limit`. * * @param {Array|string} input Source array or string to be limited. - * @param {string|number} limit The length of the returned array or string. If the `limit` number + * @param {string|number} limit The length of the returned array or string. If the `limit` number * is positive, `limit` number of items from the beginning of the source array/string are copied. - * If the number is negative, `limit` number of items from the end of the source array/string + * If the number is negative, `limit` number of items from the end of the source array/string * are copied. The `limit` will be trimmed if it exceeds `array.length` + * @param {string|number} begin Index at which to begin limitation. As a negative index, `begin` + * indicates an offset from the end of `input`. Defaults to `0`. * @returns {Array|string} A new sub-array or substring of length `limit` or less if input array * had less than `limit` elements. * @@ -70,15 +72,25 @@ */ function limitToFilter(){ - return function(input, limit) { + return function(input, limit, begin) { if (!isArray(input) && !isString(input)) return input; - + limit = int(limit); + begin = (!begin || isNaN(begin)) ? 0 : int(begin); + begin = (begin < 0 && begin >= -input.length) ? input.length + begin : begin; if (isString(input)) { //NaN check on limit if (limit) { - return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length); + if (limit >= 0) { + return input.slice(begin, begin + limit); + } else { + if (begin === 0) { + return input.slice(limit, input.length); + } else { + return input.slice(Math.max(0, begin + limit), begin); + } + } } else { return ""; } @@ -94,15 +106,22 @@ function limitToFilter(){ limit = -input.length; if (limit > 0) { - i = 0; - n = limit; + i = begin; + n = begin + limit; } else { - i = input.length + limit; - n = input.length; + if (begin) { + i = limit + begin; + n = begin; + } else { + i = input.length + limit; + n = input.length; + } } for (; i