Skip to content

Commit 2508768

Browse files
committed
Fixing angular#571
`angular.Array.limitTo`'s result cannot exceed original input array size
1 parent bf5e5f7 commit 2508768

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/apis.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -697,8 +697,8 @@ 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` elements.
702702
*
703703
* @example
704704
<doc:example>
@@ -714,10 +714,17 @@ var angularArray = {
714714
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3]');
715715
});
716716
717-
it('should update the output when -3 is entered', function() {
717+
it('should update the output when -3 is entered', function() {
718718
input('limit').enter(-3);
719719
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[7,8,9]');
720720
});
721+
722+
it('should not exceed the maximum size of input array', function() {
723+
input('limit').enter(100);
724+
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3,4,5,6,7,8,9]');
725+
});
726+
727+
721728
</doc:scenario>
722729
</doc:example>
723730
*/
@@ -726,6 +733,16 @@ var angularArray = {
726733
var out = [],
727734
i, n;
728735

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

test/ApiSpecs.js

+19
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ 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+
it('should return a copy of input array if X is exceeds array length', function () {
190+
expect(angular.Array.limitTo(items, 19)).toEqual(items);
191+
expect(angular.Array.limitTo(items, '9')).toEqual(items);
192+
expect(angular.Array.limitTo(items, -9)).toEqual(items);
193+
expect(angular.Array.limitTo(items, '-9')).toEqual(items);
194+
195+
expect(angular.Array.limitTo(items, 9)).not.toBe(items);
196+
});
178197
});
179198

180199

0 commit comments

Comments
 (0)