Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
feat(limitTo): ignore limit when invalid
Browse files Browse the repository at this point in the history
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 a3c3bf3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
17 changes: 7 additions & 10 deletions src/ng/filter/limitTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 changes: 22 additions & 12 deletions test/ng/filter/limitToSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});


Expand Down

0 comments on commit a3c3bf3

Please sign in to comment.