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

Commit a3c3bf3

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
1 parent 2caec44 commit a3c3bf3

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

src/ng/filter/limitTo.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
* @param {string|number} limit The length of the returned array or string. If the `limit` number
1616
* is positive, `limit` number of items from the beginning of the source array/string are copied.
1717
* If the number is negative, `limit` number of items from the end of the source array/string
18-
* are copied. The `limit` will be trimmed if it exceeds `array.length`
18+
* are copied. The `limit` will be trimmed if it exceeds `array.length`. If `limit` is undefined,
19+
* the input will be returned unchanged.
1920
* @returns {Array|string} A new sub-array or substring of length `limit` or less if input array
2021
* had less than `limit` elements.
2122
*
@@ -88,20 +89,16 @@
8889
*/
8990
function limitToFilter() {
9091
return function(input, limit) {
91-
if (isNumber(input)) input = input.toString();
92-
if (!isArray(input) && !isString(input)) return input;
93-
9492
if (Math.abs(Number(limit)) === Infinity) {
9593
limit = Number(limit);
9694
} else {
9795
limit = int(limit);
9896
}
97+
if (isNaN(limit)) return input;
9998

100-
//NaN check on limit
101-
if (limit) {
102-
return limit > 0 ? input.slice(0, limit) : input.slice(limit);
103-
} else {
104-
return isString(input) ? "" : [];
105-
}
99+
if (isNumber(input)) input = input.toString();
100+
if (!isArray(input) && !isString(input)) return input;
101+
102+
return limit >= 0 ? input.slice(0, limit) : input.slice(limit);
106103
};
107104
}

test/ng/filter/limitToSpec.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,30 @@ describe('Filter: limitTo', function() {
3434
});
3535

3636

37-
it('should return an empty array when X cannot be parsed', function() {
38-
expect(limitTo(items, 'bogus')).toEqual([]);
39-
expect(limitTo(items, 'null')).toEqual([]);
40-
expect(limitTo(items, 'undefined')).toEqual([]);
41-
expect(limitTo(items, null)).toEqual([]);
42-
expect(limitTo(items, undefined)).toEqual([]);
37+
it('should return an empty array when X = 0', function() {
38+
expect(limitTo(items, 0)).toEqual([]);
39+
expect(limitTo(items, '0')).toEqual([]);
4340
});
4441

45-
it('should return an empty string when X cannot be parsed', function() {
46-
expect(limitTo(str, 'bogus')).toEqual("");
47-
expect(limitTo(str, 'null')).toEqual("");
48-
expect(limitTo(str, 'undefined')).toEqual("");
49-
expect(limitTo(str, null)).toEqual("");
50-
expect(limitTo(str, undefined)).toEqual("");
42+
it('should return entire array when X cannot be parsed', function() {
43+
expect(limitTo(items, 'bogus')).toEqual(items);
44+
expect(limitTo(items, 'null')).toEqual(items);
45+
expect(limitTo(items, 'undefined')).toEqual(items);
46+
expect(limitTo(items, null)).toEqual(items);
47+
expect(limitTo(items, undefined)).toEqual(items);
48+
});
49+
50+
it('should return an empty string when X = 0', function() {
51+
expect(limitTo(str, 0)).toEqual("");
52+
expect(limitTo(str, '0')).toEqual("");
53+
});
54+
55+
it('should return entire string when X cannot be parsed', function() {
56+
expect(limitTo(str, 'bogus')).toEqual(str);
57+
expect(limitTo(str, 'null')).toEqual(str);
58+
expect(limitTo(str, 'undefined')).toEqual(str);
59+
expect(limitTo(str, null)).toEqual(str);
60+
expect(limitTo(str, undefined)).toEqual(str);
5161
});
5262

5363

0 commit comments

Comments
 (0)