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

Commit c115fa9

Browse files
TEHEKIgorMinar
authored andcommitted
fix($limitTo): properly handle excessive limits
`angular.Array.limitTo`'s result should not exceed original input array size Closes #571
1 parent b7a7fc7 commit c115fa9

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/apis.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,9 @@ var angularArray = {
697697
* @param {string|Number} limit The length of the returned array. If the `limit` number is
698698
* positive, `limit` number of items from the beginning of the source array are copied.
699699
* If the number is negative, `limit` number of items from the end of the source array are
700-
* copied.
701-
* @returns {Array} A new sub-array of length `limit`.
700+
* copied. The `limit` will be trimmed if it exceeds `array.length`
701+
* @returns {Array} A new sub-array of length `limit` or less if input array had less than `limit`
702+
* elements.
702703
*
703704
* @example
704705
<doc:example>
@@ -718,6 +719,11 @@ var angularArray = {
718719
input('limit').enter(-3);
719720
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[7,8,9]');
720721
});
722+
723+
it('should not exceed the maximum size of input array', function() {
724+
input('limit').enter(100);
725+
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3,4,5,6,7,8,9]');
726+
});
721727
</doc:scenario>
722728
</doc:example>
723729
*/
@@ -726,6 +732,16 @@ var angularArray = {
726732
var out = [],
727733
i, n;
728734

735+
// check that array is iterable
736+
if (!array || !(array instanceof Array))
737+
return out;
738+
739+
// if abs(limit) exceeds maximum length, trim it
740+
if (limit > array.length)
741+
limit = array.length;
742+
else if (limit < -array.length)
743+
limit = -array.length;
744+
729745
if (limit > 0) {
730746
i = 0;
731747
n = limit;

test/ApiSpecs.js

+20
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ describe('api', function(){
175175
expect(angular.Array.limitTo(items, null)).toEqual([]);
176176
expect(angular.Array.limitTo(items, undefined)).toEqual([]);
177177
});
178+
179+
180+
it('should return an empty array when input is not Array type', function() {
181+
expect(angular.Array.limitTo('bogus', 1)).toEqual([]);
182+
expect(angular.Array.limitTo(null, 1)).toEqual([]);
183+
expect(angular.Array.limitTo(undefined, 1)).toEqual([]);
184+
expect(angular.Array.limitTo(null, 1)).toEqual([]);
185+
expect(angular.Array.limitTo(undefined, 1)).toEqual([]);
186+
expect(angular.Array.limitTo({}, 1)).toEqual([]);
187+
});
188+
189+
190+
it('should return a copy of input array if X is exceeds array length', function () {
191+
expect(angular.Array.limitTo(items, 19)).toEqual(items);
192+
expect(angular.Array.limitTo(items, '9')).toEqual(items);
193+
expect(angular.Array.limitTo(items, -9)).toEqual(items);
194+
expect(angular.Array.limitTo(items, '-9')).toEqual(items);
195+
196+
expect(angular.Array.limitTo(items, 9)).not.toBe(items);
197+
});
178198
});
179199

180200

0 commit comments

Comments
 (0)