From a3c3bf3332e5685dc319c46faef882cb6ac246e1 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Sun, 21 Dec 2014 10:03:26 +0000 Subject: [PATCH] feat(limitTo): ignore limit when invalid BREAKING CHANGE: limitTo changed behavior when limit value is invalid. Instead of returning empty object/array it returns unchanged input. Closes #10510 --- src/ng/filter/limitTo.js | 17 +++++++---------- test/ng/filter/limitToSpec.js | 34 ++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/ng/filter/limitTo.js b/src/ng/filter/limitTo.js index f8506994..6d0618fe 100644 --- a/src/ng/filter/limitTo.js +++ b/src/ng/filter/limitTo.js @@ -15,7 +15,8 @@ * @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 - * are copied. The `limit` will be trimmed if it exceeds `array.length` + * are copied. The `limit` will be trimmed if it exceeds `array.length`. If `limit` is undefined, + * the input will be returned unchanged. * @returns {Array|string} A new sub-array or substring of length `limit` or less if input array * had less than `limit` elements. * @@ -88,20 +89,16 @@ */ function limitToFilter() { return function(input, limit) { - if (isNumber(input)) input = input.toString(); - if (!isArray(input) && !isString(input)) return input; - if (Math.abs(Number(limit)) === Infinity) { limit = Number(limit); } else { limit = int(limit); } + if (isNaN(limit)) return input; - //NaN check on limit - if (limit) { - return limit > 0 ? input.slice(0, limit) : input.slice(limit); - } else { - return isString(input) ? "" : []; - } + if (isNumber(input)) input = input.toString(); + if (!isArray(input) && !isString(input)) return input; + + return limit >= 0 ? input.slice(0, limit) : input.slice(limit); }; } diff --git a/test/ng/filter/limitToSpec.js b/test/ng/filter/limitToSpec.js index 372b398c..a15fbbc5 100644 --- a/test/ng/filter/limitToSpec.js +++ b/test/ng/filter/limitToSpec.js @@ -34,20 +34,30 @@ describe('Filter: limitTo', function() { }); - it('should return an empty array when X cannot be parsed', function() { - expect(limitTo(items, 'bogus')).toEqual([]); - expect(limitTo(items, 'null')).toEqual([]); - expect(limitTo(items, 'undefined')).toEqual([]); - expect(limitTo(items, null)).toEqual([]); - expect(limitTo(items, undefined)).toEqual([]); + it('should return an empty array when X = 0', function() { + expect(limitTo(items, 0)).toEqual([]); + expect(limitTo(items, '0')).toEqual([]); }); - it('should return an empty string when X cannot be parsed', function() { - expect(limitTo(str, 'bogus')).toEqual(""); - expect(limitTo(str, 'null')).toEqual(""); - expect(limitTo(str, 'undefined')).toEqual(""); - expect(limitTo(str, null)).toEqual(""); - expect(limitTo(str, undefined)).toEqual(""); + it('should return entire array when X cannot be parsed', function() { + expect(limitTo(items, 'bogus')).toEqual(items); + expect(limitTo(items, 'null')).toEqual(items); + expect(limitTo(items, 'undefined')).toEqual(items); + expect(limitTo(items, null)).toEqual(items); + expect(limitTo(items, undefined)).toEqual(items); + }); + + it('should return an empty string when X = 0', function() { + expect(limitTo(str, 0)).toEqual(""); + expect(limitTo(str, '0')).toEqual(""); + }); + + it('should return entire string when X cannot be parsed', function() { + expect(limitTo(str, 'bogus')).toEqual(str); + expect(limitTo(str, 'null')).toEqual(str); + expect(limitTo(str, 'undefined')).toEqual(str); + expect(limitTo(str, null)).toEqual(str); + expect(limitTo(str, undefined)).toEqual(str); });