* @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 *
()
| 20679 | </example> |
| 20680 | */ |
| 20681 | function limitToFilter() { |
| 20682 | return function (input, limit, begin) { |
| 20683 | if (Math.abs(Number(limit)) === Infinity) { |
| 20684 | limit = Number(limit); |
| 20685 | } else { |
| 20686 | limit = toInt(limit); |
| 20687 | } |
| 20688 | if (isNaN(limit)) return input; |
| 20689 | |
| 20690 | if (isNumber(input)) input = input.toString(); |
| 20691 | if (!isArray(input) && !isString(input)) return input; |
| 20692 | |
| 20693 | begin = (!begin || isNaN(begin)) ? 0 : toInt(begin); |
| 20694 | begin = (begin < 0) ? Math.max(0, input.length + begin) : begin; |
| 20695 | |
| 20696 | if (limit >= 0) { |
| 20697 | return input.slice(begin, begin + limit); |
| 20698 | } else { |
| 20699 | if (begin === 0) { |
| 20700 | return input.slice(limit, input.length); |
| 20701 | } else { |
| 20702 | return input.slice(Math.max(0, begin + limit), begin); |
| 20703 | } |
| 20704 | } |
| 20705 | }; |
| 20706 | } |
| 20707 | |
| 20708 | /** |
| 20709 | * @ngdoc filter |