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

Commit c09dea9

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

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/ng/filter/limitTo.js

+8-3
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 left 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
*
@@ -102,7 +103,7 @@ function limitToFilter() {
102103
if (limit) {
103104
return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
104105
} else {
105-
return "";
106+
return input;
106107
}
107108
}
108109

@@ -117,9 +118,13 @@ function limitToFilter() {
117118
if (limit > 0) {
118119
i = 0;
119120
n = limit;
121+
} else if (!limit) {
122+
// NaN limit
123+
i = 0;
124+
n = input.length;
120125
} else {
121126
// zero and NaN check on limit - return empty array
122-
if (!limit) return [];
127+
if (!limit) return input.slice();
123128

124129
i = input.length + limit;
125130
n = input.length;

test/ng/filter/limitToSpec.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ 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 entire array when X cannot be parsed', function() {
38+
expect(limitTo(items, 'bogus')).toEqual(items);
39+
expect(limitTo(items, 'null')).toEqual(items);
40+
expect(limitTo(items, 'undefined')).toEqual(items);
41+
expect(limitTo(items, null)).toEqual(items);
42+
expect(limitTo(items, undefined)).toEqual(items);
4343
});
4444

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("");
45+
it('should return entire string when X cannot be parsed', function() {
46+
expect(limitTo(str, 'bogus')).toEqual(str);
47+
expect(limitTo(str, 'null')).toEqual(str);
48+
expect(limitTo(str, 'undefined')).toEqual(str);
49+
expect(limitTo(str, null)).toEqual(str);
50+
expect(limitTo(str, undefined)).toEqual(str);
5151
});
5252

5353

0 commit comments

Comments
 (0)