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

Commit 81be47f

Browse files
Wesley Chowesleycho
Wesley Cho
authored andcommitted
fix(isArrayLike): Correct logic in isArrayLike to screen out objects
- Fix ngRepeat check via isArrayLike check to prevent objects from accidentally passing as array-like - Refactor according to suggestion
1 parent 814c984 commit 81be47f

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

src/Angular.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ function isArrayLike(obj) {
201201
return true;
202202
}
203203

204-
return isString(obj) || isArray(obj) || length === 0 ||
204+
return isString(obj) || isArray(obj) || (length === 0 && Object.keys(obj).length <= 1) ||
205205
typeof length === 'number' && length > 0 && (length - 1) in obj;
206206
}
207207

src/ng/directive/ngRepeat.js

-2
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
344344
}
345345
collectionKeys.sort();
346346
}
347-
348347
collectionLength = collectionKeys.length;
349348
nextBlockOrder = new Array(collectionLength);
350349

@@ -438,4 +437,3 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
438437
}
439438
};
440439
}];
441-

test/ng/directive/ngRepeatSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,16 @@ describe('ngRepeat', function() {
580580
expect(element.text()).toEqual('frodo:f:0|misko:m:1|shyam:s:2|');
581581
});
582582

583+
it('should expose iterator offset as $index when iterating over objects with length key value 0', function() {
584+
element = $compile(
585+
'<ul>' +
586+
'<li ng-repeat="(key, val) in items">{{key}}:{{val}}:{{$index}}|</li>' +
587+
'</ul>')(scope);
588+
scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f', 'length':0};
589+
scope.$digest();
590+
expect(element.text()).toEqual('frodo:f:0|length:0:1|misko:m:2|shyam:s:3|');
591+
});
592+
583593

584594
it('should expose iterator position as $first, $middle and $last when iterating over arrays',
585595
function() {

0 commit comments

Comments
 (0)