* @ngdoc filter * @name limitTo * @kind function * * @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, string or number, as specified by * the value and sign (positive
()
| 23222 | </example> |
| 23223 | */ |
| 23224 | function limitToFilter() { |
| 23225 | return function(input, limit, begin) { |
| 23226 | if (Math.abs(Number(limit)) === Infinity) { |
| 23227 | limit = Number(limit); |
| 23228 | } else { |
| 23229 | limit = toInt(limit); |
| 23230 | } |
| 23231 | if (isNumberNaN(limit)) return input; |
| 23232 | |
| 23233 | if (isNumber(input)) input = input.toString(); |
| 23234 | if (!isArrayLike(input)) return input; |
| 23235 | |
| 23236 | begin = (!begin || isNaN(begin)) ? 0 : toInt(begin); |
| 23237 | begin = (begin < 0) ? Math.max(0, input.length + begin) : begin; |
| 23238 | |
| 23239 | if (limit >= 0) { |
| 23240 | return sliceFn(input, begin, begin + limit); |
| 23241 | } else { |
| 23242 | if (begin === 0) { |
| 23243 | return sliceFn(input, limit, input.length); |
| 23244 | } else { |
| 23245 | return sliceFn(input, Math.max(0, begin + limit), begin); |
| 23246 | } |
| 23247 | } |
| 23248 | }; |
| 23249 | } |
| 23250 | |
| 23251 | function sliceFn(input, begin, end) { |
| 23252 | if (isString(input)) return input.slice(begin, end); |
nothing calls this directly
no test coverage detected