Skip to content

Commit

Permalink
Fixing angular#571
Browse files Browse the repository at this point in the history
`angular.Array.limitTo`'s  result cannot exceed original input array size
  • Loading branch information
TEHEK committed Sep 23, 2011
1 parent bf5e5f7 commit 2508768
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,8 @@ var angularArray = {
* @param {string|Number} limit The length of the returned array. If the `limit` number is
* positive, `limit` number of items from the beginning of the source array are copied.
* If the number is negative, `limit` number of items from the end of the source array are
* copied.
* @returns {Array} A new sub-array of length `limit`.
* copied. The `limit` will be trimmed if it exceeds `array.length`
* @returns {Array} A new sub-array of length `limit` or less if input array had less than `limit` elements.
*
* @example
<doc:example>
Expand All @@ -714,10 +714,17 @@ var angularArray = {
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3]');
});
it('should update the output when -3 is entered', function() {
it('should update the output when -3 is entered', function() {

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar Sep 28, 2011

extra white space

input('limit').enter(-3);
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[7,8,9]');
});
it('should not exceed the maximum size of input array', function() {
input('limit').enter(100);
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3,4,5,6,7,8,9]');
});

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar Sep 28, 2011

please remove the two blank lines above

</doc:scenario>
</doc:example>
*/
Expand All @@ -726,6 +733,16 @@ var angularArray = {
var out = [],
i, n;

// check that array is iterable
if (!array || !(array instanceof Array))
return out;

// if abs(limit) exceeds maximum length, trim it
if (limit > array.length)
limit = array.length;
else if (limit < -array.length)
limit = -array.length;

if (limit > 0) {
i = 0;
n = limit;
Expand Down
19 changes: 19 additions & 0 deletions test/ApiSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ describe('api', function(){
expect(angular.Array.limitTo(items, null)).toEqual([]);
expect(angular.Array.limitTo(items, undefined)).toEqual([]);
});


it('should return an empty array when input is not Array type', function() {
expect(angular.Array.limitTo('bogus', 1)).toEqual([]);
expect(angular.Array.limitTo(null, 1)).toEqual([]);
expect(angular.Array.limitTo(undefined, 1)).toEqual([]);
expect(angular.Array.limitTo(null, 1)).toEqual([]);
expect(angular.Array.limitTo(undefined, 1)).toEqual([]);
expect(angular.Array.limitTo({}, 1)).toEqual([]);
});

This comment has been minimized.

Copy link
@IgorMinar

IgorMinar Sep 28, 2011

please add one more blank line. we usually put two blank lines between it or describe blocks. (some older code doesn't follow this yet).

it('should return a copy of input array if X is exceeds array length', function () {
expect(angular.Array.limitTo(items, 19)).toEqual(items);
expect(angular.Array.limitTo(items, '9')).toEqual(items);
expect(angular.Array.limitTo(items, -9)).toEqual(items);
expect(angular.Array.limitTo(items, '-9')).toEqual(items);

expect(angular.Array.limitTo(items, 9)).not.toBe(items);
});
});


Expand Down

0 comments on commit 2508768

Please sign in to comment.