Permalink
Browse files

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
  • Loading branch information...
petebacondarwin committed Dec 21, 2014
1 parent 2caec44 commit a3c3bf3332e5685dc319c46faef882cb6ac246e1
Showing with 29 additions and 22 deletions.
  1. +7 −10 src/ng/filter/limitTo.js
  2. +22 −12 test/ng/filter/limitToSpec.js
View
@@ -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);
};
}
@@ -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);
});

0 comments on commit a3c3bf3

Please sign in to comment.