MCPcopy Create free account
hub / github.com/bclinkinbeard/angular / limitToFilter

Function limitToFilter

test/fixtures/foo.js:15399–15441  ·  view source on GitHub ↗

* @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 or string, as specified by * the value and sign (positive or nega

()

Source from the content-addressed store, hash-verified

15397 </example>
15398 */
15399function limitToFilter(){
15400 return function(input, limit) {
15401 if (!isArray(input) && !isString(input)) return input;
15402
15403 if (Math.abs(Number(limit)) === Infinity) {
15404 limit = Number(limit);
15405 } else {
15406 limit = int(limit);
15407 }
15408
15409 if (isString(input)) {
15410 //NaN check on limit
15411 if (limit) {
15412 return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
15413 } else {
15414 return "";
15415 }
15416 }
15417
15418 var out = [],
15419 i, n;
15420
15421 // if abs(limit) exceeds maximum length, trim it
15422 if (limit > input.length)
15423 limit = input.length;
15424 else if (limit < -input.length)
15425 limit = -input.length;
15426
15427 if (limit > 0) {
15428 i = 0;
15429 n = limit;
15430 } else {
15431 i = input.length + limit;
15432 n = input.length;
15433 }
15434
15435 for (; i<n; i++) {
15436 out.push(input[i]);
15437 }
15438
15439 return out;
15440 };
15441}
15442
15443/**
15444 * @ngdoc filter

Callers

nothing calls this directly

Calls 2

isStringFunction · 0.85
intFunction · 0.85

Tested by

no test coverage detected