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

Commit 7d48430

Browse files
feat(limitTo): ignore limit when invalid
BREAKING CHANGE: limitTo changed behavoir when limit value is invalid. Instead of returning empty object/array it returns unchanged input. Closes #10484
1 parent 7d70dcd commit 7d48430

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/ng/filter/limitTo.js

+6-8
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
*
@@ -97,13 +98,10 @@ function limitToFilter() {
9798
limit = int(limit);
9899
}
99100

101+
if (isNaN(limit)) return input;
102+
100103
if (isString(input)) {
101-
//NaN check on limit
102-
if (limit) {
103-
return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
104-
} else {
105-
return "";
106-
}
104+
return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
107105
}
108106

109107
var i, n;
@@ -118,7 +116,7 @@ function limitToFilter() {
118116
i = 0;
119117
n = limit;
120118
} else {
121-
// zero and NaN check on limit - return empty array
119+
// zero check on limit - return empty array
122120
if (!limit) return [];
123121

124122
i = input.length + limit;

test/ng/filter/limitToSpec.js

+20-12
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,28 @@ 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([]);
4339
});
4440

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("");
41+
it('should return entire array when X cannot be parsed', function() {
42+
expect(limitTo(items, 'bogus')).toEqual(items);
43+
expect(limitTo(items, 'null')).toEqual(items);
44+
expect(limitTo(items, 'undefined')).toEqual(items);
45+
expect(limitTo(items, null)).toEqual(items);
46+
expect(limitTo(items, undefined)).toEqual(items);
47+
});
48+
49+
it('should return an empty string when X = 0', function() {
50+
expect(limitTo(str, 0)).toEqual("");
51+
});
52+
53+
it('should return entire string when X cannot be parsed', function() {
54+
expect(limitTo(str, 'bogus')).toEqual(str);
55+
expect(limitTo(str, 'null')).toEqual(str);
56+
expect(limitTo(str, 'undefined')).toEqual(str);
57+
expect(limitTo(str, null)).toEqual(str);
58+
expect(limitTo(str, undefined)).toEqual(str);
5159
});
5260

5361

0 commit comments

Comments
 (0)