From b4523eddf0c885728e87fc53412c441cf1eed660 Mon Sep 17 00:00:00 2001 From: Caitlin Potter Date: Sat, 20 Dec 2014 11:09:47 -0500 Subject: [PATCH] fix(ngRepeat): do not sort object keys alphabetically BREAKING CHANGE: Previously, the order of object properties was consistent and simple. Now, it's not guaranteed to be any particular order at all. The best approach is to convert Objects into Arrays by a filter such as https://github.com/petebacondarwin/angular-toArrayFilter or some other mechanism, and then sort them manually in the order you need. Closes #6210 --- src/ng/directive/ngRepeat.js | 6 ++--- test/ng/directive/ngRepeatSpec.js | 39 ++++++++++++++++--------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/ng/directive/ngRepeat.js b/src/ng/directive/ngRepeat.js index 6cac08b6fb6a..83935d6acef0 100644 --- a/src/ng/directive/ngRepeat.js +++ b/src/ng/directive/ngRepeat.js @@ -335,14 +335,13 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { trackByIdFn = trackByIdExpFn || trackByIdArrayFn; } else { trackByIdFn = trackByIdExpFn || trackByIdObjFn; - // if object, extract keys, sort them and use to determine order of iteration over obj props + // if object, extract keys, in enumeration order, unsorted collectionKeys = []; for (var itemKey in collection) { - if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) != '$') { + if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) !== '$') { collectionKeys.push(itemKey); } } - collectionKeys.sort(); } collectionLength = collectionKeys.length; @@ -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 f5cfdf6d3890..4a62d233fd35 100644 --- a/test/ng/directive/ngRepeatSpec.js +++ b/test/ng/directive/ngRepeatSpec.js @@ -163,7 +163,7 @@ describe('ngRepeat', function() { '')(scope); scope.items = {age:20, wealth:20, prodname: "Bingo", dogname: "Bingo", codename: "20"}; scope.$digest(); - expect(element.text()).toEqual('age:20|codename:20|dogname:Bingo|prodname:Bingo|wealth:20|'); + expect(element.text()).toEqual('age:20|wealth:20|prodname:Bingo|dogname:Bingo|codename:20|'); }); describe('track by', function() { @@ -587,7 +587,7 @@ describe('ngRepeat', function() { '')(scope); scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'}; scope.$digest(); - expect(element.text()).toEqual('frodo:f:0|misko:m:1|shyam:s:2|'); + expect(element.text()).toEqual('misko:m:0|shyam:s:1|frodo:f:2|'); }); @@ -656,10 +656,10 @@ describe('ngRepeat', function() { scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; scope.$digest(); expect(element.text()). - toEqual('doug:d:true-false-false|' + - 'frodo:f:false-true-false|' + - 'misko:m:false-true-false|' + - 'shyam:s:false-false-true|'); + toEqual('misko:m:true-false-false|' + + 'shyam:s:false-true-false|' + + 'doug:d:false-true-false|' + + 'frodo:f:false-false-true|'); delete scope.items.doug; delete scope.items.frodo; @@ -681,15 +681,15 @@ describe('ngRepeat', function() { scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f'}; scope.$digest(); expect(element.text()). - toBe('doug:d:true-false|' + - 'frodo:f:false-true|' + - 'misko:m:true-false|' + - 'shyam:s:false-true|'); + toBe('misko:m:true-false|' + + 'shyam:s:false-true|' + + 'doug:d:true-false|' + + 'frodo:f:false-true|'); delete scope.items.frodo; delete scope.items.shyam; scope.$digest(); - expect(element.text()).toBe('doug:d:true-false|misko:m:false-true|'); + expect(element.text()).toBe('misko:m:true-false|doug:d:false-true|'); }); @@ -700,11 +700,12 @@ describe('ngRepeat', function() { '')(scope); scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f', '$toBeFilteredOut': 'xxxx'}; scope.$digest(); + expect(element.text()). - toEqual('doug:d:true-false-false|' + - 'frodo:f:false-true-false|' + - 'misko:m:false-true-false|' + - 'shyam:s:false-false-true|'); + toEqual('misko:m:true-false-false|' + + 'shyam:s:false-true-false|' + + 'doug:d:false-true-false|' + + 'frodo:f:false-false-true|'); }); @@ -716,10 +717,10 @@ describe('ngRepeat', function() { scope.items = {'misko':'m', 'shyam':'s', 'doug':'d', 'frodo':'f', '$toBeFilteredOut': 'xxxx'}; scope.$digest(); expect(element.text()). - toEqual('doug:d:true-false|' + - 'frodo:f:false-true|' + - 'misko:m:true-false|' + - 'shyam:s:false-true|'); + toEqual('misko:m:true-false|' + + 'shyam:s:false-true|' + + 'doug:d:true-false|' + + 'frodo:f:false-true|'); });