From 983de2e3fed4c1a53a3b3251c5fd9d5394cb1dba Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Sat, 22 Nov 2014 13:21:46 -0800 Subject: [PATCH] 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 --- src/Angular.js | 2 +- src/ng/directive/ngRepeat.js | 2 -- test/ng/directive/ngRepeatSpec.js | 10 ++++++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index fc0b378369eb..e04cb6548766 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -201,7 +201,7 @@ function isArrayLike(obj) { return true; } - return isString(obj) || isArray(obj) || length === 0 || + return isString(obj) || isArray(obj) || (length === 0 && Object.keys(obj).length <= 1) || typeof length === 'number' && length > 0 && (length - 1) in obj; } diff --git a/src/ng/directive/ngRepeat.js b/src/ng/directive/ngRepeat.js index 69192cc4a3fb..af99d881f6e1 100644 --- a/src/ng/directive/ngRepeat.js +++ b/src/ng/directive/ngRepeat.js @@ -344,7 +344,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { } collectionKeys.sort(); } - collectionLength = collectionKeys.length; nextBlockOrder = new Array(collectionLength); @@ -438,4 +437,3 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { } }; }]; - diff --git a/test/ng/directive/ngRepeatSpec.js b/test/ng/directive/ngRepeatSpec.js index 00e36be69a7f..589719cbd2bb 100644 --- a/test/ng/directive/ngRepeatSpec.js +++ b/test/ng/directive/ngRepeatSpec.js @@ -580,6 +580,16 @@ describe('ngRepeat', function() { expect(element.text()).toEqual('frodo:f:0|misko:m:1|shyam:s:2|'); }); + it('should expose iterator offset as $index when iterating over objects with length key value 0', function() { + element = $compile( + '')(scope); + scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f', 'length':0}; + scope.$digest(); + expect(element.text()).toEqual('frodo:f:0|length:0:1|misko:m:2|shyam:s:3|'); + }); + it('should expose iterator position as $first, $middle and $last when iterating over arrays', function() {