From d93e52613599ef892e33c07809790faf99914c34 Mon Sep 17 00:00:00 2001 From: Nuri Hodges Date: Fri, 24 Apr 2015 14:41:48 -0700 Subject: [PATCH 1/6] fix(orderBy): Throw exception when orderBy is given a non array-like object. #11255 --- src/ng/filter/orderBy.js | 2 +- test/ng/filter/orderBySpec.js | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index 2e9e946d0c75..7a184a503cc3 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -177,7 +177,7 @@ orderByFilter.$inject = ['$parse']; function orderByFilter($parse) { return function(array, sortPredicate, reverseOrder) { - if (!(isArrayLike(array))) return array; + if (!(isArrayLike(array))) throw minErr('filter')('notarray', 'Expected array but received: {0}', array); if (!isArray(sortPredicate)) { sortPredicate = [sortPredicate]; } if (sortPredicate.length === 0) { sortPredicate = ['+']; } diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index 4f371ba80aca..7bb626a2a162 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -8,6 +8,11 @@ describe('Filter: orderBy', function() { describe('(Arrays)', function() { + it('should throw an exception if no array-like object is provided', function() { + expect(function() { orderBy({}); }). + toThrowMinErr('filter', 'notarray', 'Expected array but received: {}'); + }); + it('should return sorted array if predicate is not provided', function() { expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]); From 6ddfc27743176a5daf557d5b5e8bbeba8bf73060 Mon Sep 17 00:00:00 2001 From: Frank Date: Wed, 7 Oct 2015 14:37:15 -0600 Subject: [PATCH 2/6] Documentation to orderby when the parameter is not an array. --- docs/content/error/orderBy/notarray.ngdoc | 53 +++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 docs/content/error/orderBy/notarray.ngdoc diff --git a/docs/content/error/orderBy/notarray.ngdoc b/docs/content/error/orderBy/notarray.ngdoc new file mode 100644 index 000000000000..e03e80f819c4 --- /dev/null +++ b/docs/content/error/orderBy/notarray.ngdoc @@ -0,0 +1,53 @@ +@ngdoc error +@name orderBy:notarray +@fullName Not an array +@description + +This error occurs when {@link ng.orderBy filter} is not used with an array: +```html + +
+ {{ key }} : {{ value }} +
+``` + +orderBy must be used with an array so a subset of items can be returned. +The array can be initialized asynchronously and therefore null or undefined won't throw this error. + +To orderBy an object by the value of its properties you can create your own array based on that object: +```js +angular.module('aModule',[]) + .controller('aController', function($scope) { + var obj = { + one: { + name: 'something 1' + }, + two: { + name: 'something 2' + }, + three: { + name: 'something 3' + }, + }; + + //if using underscore + $scope.objToArray = = _.values(obj); + + }); +``` +That can be used as: +```html + +
+ {{ key }} : {{ value }} +
+``` + +You could as well convert the object to an array using a filter such as +[toArrayFilter](https://github.com/petebacondarwin/angular-toArrayFilter): +```html + +
+ {{ item }} +
+``` From 77cefefb588ca8a615431e41236afc1e8cb09d94 Mon Sep 17 00:00:00 2001 From: Nuri Hodges Date: Wed, 14 Oct 2015 12:09:19 -0700 Subject: [PATCH 3/6] fix(orderBy): Change filter to orderBy in error thrown. --- src/ng/filter/orderBy.js | 2 +- test/ng/directive/ngOptionsSpec.js | 2 +- test/ng/filter/orderBySpec.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index 7a184a503cc3..b9fc7cc9c6f4 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -177,7 +177,7 @@ orderByFilter.$inject = ['$parse']; function orderByFilter($parse) { return function(array, sortPredicate, reverseOrder) { - if (!(isArrayLike(array))) throw minErr('filter')('notarray', 'Expected array but received: {0}', array); + if (!(isArrayLike(array))) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array); if (!isArray(sortPredicate)) { sortPredicate = [sortPredicate]; } if (sortPredicate.length === 0) { sortPredicate = ['+']; } diff --git a/test/ng/directive/ngOptionsSpec.js b/test/ng/directive/ngOptionsSpec.js index c5dac4b72914..1634811a22df 100644 --- a/test/ng/directive/ngOptionsSpec.js +++ b/test/ng/directive/ngOptionsSpec.js @@ -125,7 +125,7 @@ describe('ngOptions', function() { .directive('oCompileContents', function() { return { - link: function(scope, element) { + link: function(scope, element) { linkLog.push('linkCompileContents'); $compile(element.contents())(scope); } diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index 7bb626a2a162..fad19ec44420 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -10,7 +10,7 @@ describe('Filter: orderBy', function() { describe('(Arrays)', function() { it('should throw an exception if no array-like object is provided', function() { expect(function() { orderBy({}); }). - toThrowMinErr('filter', 'notarray', 'Expected array but received: {}'); + toThrowMinErr('orderBy', 'notarray', 'Expected array but received: {}'); }); it('should return sorted array if predicate is not provided', function() { From 0c47849a281795e0b8f47c15506679ca71d2985d Mon Sep 17 00:00:00 2001 From: Nuri Hodges Date: Thu, 15 Oct 2015 10:21:32 -0700 Subject: [PATCH 4/6] chore(orderBy): Update documentation based on code review. --- docs/content/error/orderBy/notarray.ngdoc | 31 ++++++++++------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/docs/content/error/orderBy/notarray.ngdoc b/docs/content/error/orderBy/notarray.ngdoc index e03e80f819c4..ba46631eb09a 100644 --- a/docs/content/error/orderBy/notarray.ngdoc +++ b/docs/content/error/orderBy/notarray.ngdoc @@ -3,7 +3,7 @@ @fullName Not an array @description -This error occurs when {@link ng.orderBy filter} is not used with an array: +This error occurs when {@link ng.orderBy orderBy} is not used with an array: ```html
@@ -16,29 +16,26 @@ The array can be initialized asynchronously and therefore null or undefined won' To orderBy an object by the value of its properties you can create your own array based on that object: ```js -angular.module('aModule',[]) +angular.module('aModule', []) .controller('aController', function($scope) { - var obj = { - one: { - name: 'something 1' - }, - two: { - name: 'something 2' - }, - three: { - name: 'something 3' - }, + var myObj = { + one: {id: 1, name: 'Some thing'}, + two: {id: 2, name: 'Another thing'}, + three: {id: 3, name: 'A third thing'} }; - //if using underscore - $scope.objToArray = = _.values(obj); - + $scope.arrFromMyObj = Object.keys(myObj).map(function(key) { + return myObj[key]; + }); }); ``` That can be used as: ```html - -
+ +
{{ key }} : {{ value }}
``` From 1c997ceffc398b7a221ed7d2bad7f8b0a8af3b3e Mon Sep 17 00:00:00 2001 From: Nuri Hodges Date: Fri, 23 Oct 2015 09:49:21 -0700 Subject: [PATCH 5/6] fix(orderBy): Modify docs and add test for array-like object. BREAKING CHANGES: These changes alter the behavior of AngularJS such that it will no longer throw an error for non array values, as it will accept array-like values as well. It will throw an error if no array-like value is passed. --- docs/content/error/orderBy/notarray.ngdoc | 10 +++++----- test/ng/filter/orderBySpec.js | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/docs/content/error/orderBy/notarray.ngdoc b/docs/content/error/orderBy/notarray.ngdoc index ba46631eb09a..87845d067a74 100644 --- a/docs/content/error/orderBy/notarray.ngdoc +++ b/docs/content/error/orderBy/notarray.ngdoc @@ -1,9 +1,9 @@ @ngdoc error @name orderBy:notarray -@fullName Not an array +@fullName Value is not array-like @description -This error occurs when {@link ng.orderBy orderBy} is not used with an array: +This error occurs when {@link ng.orderBy orderBy} is not passed an array-like value: ```html
@@ -11,10 +11,10 @@ This error occurs when {@link ng.orderBy orderBy} is not used with an array:
``` -orderBy must be used with an array so a subset of items can be returned. -The array can be initialized asynchronously and therefore null or undefined won't throw this error. +orderBy must be used with an array-like value so a subset of items can be returned. +The array can be initialized asynchronously and therefore `null` or `undefined` won't throw this error. -To orderBy an object by the value of its properties you can create your own array based on that object: +To use orderBy to order the properties of an object, you can create your own array based on that object: ```js angular.module('aModule', []) .controller('aController', function($scope) { diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index fad19ec44420..a3318876ceec 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -13,6 +13,10 @@ describe('Filter: orderBy', function() { toThrowMinErr('orderBy', 'notarray', 'Expected array but received: {}'); }); + it('should not throw an exception if an array-like object is provided', function() { + expect(orderBy('cba')).toEqual(['a', 'b', 'c']); + }); + it('should return sorted array if predicate is not provided', function() { expect(orderBy([2, 1, 3])).toEqual([1, 2, 3]); From e1e87ec54d2aa4adb7a8c6e9d131b6cc2468f3be Mon Sep 17 00:00:00 2001 From: Nuri Hodges Date: Wed, 28 Oct 2015 19:36:59 -0700 Subject: [PATCH 6/6] fix(orderBy): Return null/undefined as-is, add specs for this edge case. --- src/ng/filter/orderBy.js | 4 +++- test/ng/filter/orderBySpec.js | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js index b9fc7cc9c6f4..f6095c50106b 100644 --- a/src/ng/filter/orderBy.js +++ b/src/ng/filter/orderBy.js @@ -177,7 +177,9 @@ orderByFilter.$inject = ['$parse']; function orderByFilter($parse) { return function(array, sortPredicate, reverseOrder) { - if (!(isArrayLike(array))) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array); + if (!array) { return array; } + + if (!isArrayLike(array)) throw minErr('orderBy')('notarray', 'Expected array but received: {0}', array); if (!isArray(sortPredicate)) { sortPredicate = [sortPredicate]; } if (sortPredicate.length === 0) { sortPredicate = ['+']; } diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js index a3318876ceec..131e98d362e8 100644 --- a/test/ng/filter/orderBySpec.js +++ b/test/ng/filter/orderBySpec.js @@ -13,6 +13,11 @@ describe('Filter: orderBy', function() { toThrowMinErr('orderBy', 'notarray', 'Expected array but received: {}'); }); + it('should not throw an exception if a null or undefined value is provided', function() { + expect(orderBy(null)).toEqual(null); + expect(orderBy(undefined)).toEqual(undefined); + }); + it('should not throw an exception if an array-like object is provided', function() { expect(orderBy('cba')).toEqual(['a', 'b', 'c']); });