* @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
()
| 17367 | </example> |
| 17368 | */ |
| 17369 | function limitToFilter() { |
| 17370 | return function(input, limit) { |
| 17371 | if (isNumber(input)) input = input.toString(); |
| 17372 | if (!isArray(input) && !isString(input)) return input; |
| 17373 | |
| 17374 | if (Math.abs(Number(limit)) === Infinity) { |
| 17375 | limit = Number(limit); |
| 17376 | } else { |
| 17377 | limit = int(limit); |
| 17378 | } |
| 17379 | |
| 17380 | if (isString(input)) { |
| 17381 | //NaN check on limit |
| 17382 | if (limit) { |
| 17383 | return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length); |
| 17384 | } else { |
| 17385 | return ""; |
| 17386 | } |
| 17387 | } |
| 17388 | |
| 17389 | var out = [], |
| 17390 | i, n; |
| 17391 | |
| 17392 | // if abs(limit) exceeds maximum length, trim it |
| 17393 | if (limit > input.length) |
| 17394 | limit = input.length; |
| 17395 | else if (limit < -input.length) |
| 17396 | limit = -input.length; |
| 17397 | |
| 17398 | if (limit > 0) { |
| 17399 | i = 0; |
| 17400 | n = limit; |
| 17401 | } else { |
| 17402 | i = input.length + limit; |
| 17403 | n = input.length; |
| 17404 | } |
| 17405 | |
| 17406 | for (; i < n; i++) { |
| 17407 | out.push(input[i]); |
| 17408 | } |
| 17409 | |
| 17410 | return out; |
| 17411 | }; |
| 17412 | } |
| 17413 | |
| 17414 | /** |
| 17415 | * @ngdoc filter |