From 9a594cf0dc811d1b0be416d5c84c7a672cf8e081 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 8 Aug 2014 09:31:20 +0200 Subject: [PATCH 1/2] feat(OrderBy): Allow ordering an Iterable fixes #1324 --- lib/formatter/order_by.dart | 11 +++++------ test/formatter/order_by_spec.dart | 7 +++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/formatter/order_by.dart b/lib/formatter/order_by.dart index 12596e33d..92f087b5d 100644 --- a/lib/formatter/order_by.dart +++ b/lib/formatter/order_by.dart @@ -3,7 +3,7 @@ part of angular.formatter_internal; typedef dynamic _Mapper(dynamic e); /** - * Orders the the elements of a list using a predicate. + * Orders the the elements of an [Iterable] using a predicate. * * # Usage * @@ -15,7 +15,7 @@ typedef dynamic _Mapper(dynamic e); * - **a custom callable expression**: an expression that will be called to transform the element * before a sort. * - **a list**: the list may consist of either strings or callable expressions. A list expression - * indicates a list of fallback expressions to use when a comparision results in the items + * indicates a list of fallback expressions to use when a comparison results in the items * being equal. * * If the expression is explicitly empty(`orderBy:''`), the elements are sorted in @@ -166,10 +166,9 @@ class OrderBy implements Function { * - `expression`: String/Function or Array of String/Function. * - `descending`: When specified, use descending order. (The default is ascending order.) */ - List call(List items, var expression, [bool descending=false]) { - if (items == null) { - return null; - } + List call(Iterable items, var expression, [bool descending=false]) { + if (items == null) return null; + if (items is! List) items = items.toList(); List expressions = null; if (expression is String || expression is _Mapper) { expressions = [expression]; diff --git a/test/formatter/order_by_spec.dart b/test/formatter/order_by_spec.dart index 42ad2ea34..662d1f404 100644 --- a/test/formatter/order_by_spec.dart +++ b/test/formatter/order_by_spec.dart @@ -54,6 +54,11 @@ main() { expect(parse('list | orderBy:"-"').eval(scope.context, formatters)).toEqual([3, 2, 1]); }); + it('should sort Iterables', (Scope scope, Parser parse, FormatterMap formatters) { + scope.context['iterable'] = [1, 3, 2].map((x) => x); + expect(parse('iterable | orderBy:""').eval(scope.context, formatters)).toEqual([1, 2, 3]); + }); + it('should sort by expression', (Scope scope, Parser parse, FormatterMap formatters) { expect(parse('authors | orderBy:"firstName"').eval(scope.context, formatters)).toEqual([ Emily___Bronte, @@ -165,6 +170,8 @@ main() { ]); }); + + it('should support function expressions', (Scope scope, Parser parse, FormatterMap formatters) { scope.context['func'] = (e) => -(e['a'] + e['b']); From 0713d8766d8847bf363adb196df5ba860ef1260e Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 8 Aug 2014 09:38:04 +0200 Subject: [PATCH 2/2] feat(OrderBy): Allow specifying an Iterable expression --- lib/formatter/order_by.dart | 12 +++++++----- test/formatter/order_by_spec.dart | 11 ++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/formatter/order_by.dart b/lib/formatter/order_by.dart index 92f087b5d..f39ee9f83 100644 --- a/lib/formatter/order_by.dart +++ b/lib/formatter/order_by.dart @@ -14,9 +14,9 @@ typedef dynamic _Mapper(dynamic e); * - **a string**: a string containing an expression, such as "user.lastName", used to order the list. * - **a custom callable expression**: an expression that will be called to transform the element * before a sort. - * - **a list**: the list may consist of either strings or callable expressions. A list expression - * indicates a list of fallback expressions to use when a comparison results in the items - * being equal. + * - **an [Iterable]**: it may consist of either strings or callable expressions. A list expression + * indicates a list of fallback expressions to use when a comparison results in the items being + * equal. * * If the expression is explicitly empty(`orderBy:''`), the elements are sorted in * ascending order, using the default comparator, `+`. @@ -26,8 +26,8 @@ typedef dynamic _Mapper(dynamic e); * - `+`: sort the elements in ascending order. This is the default. * - `-`: sort the elements in descending order. * - * Alternately, by appending `true`, you can set "descending order" to true, which has the same effect as the `-` - * prefix. + * Alternately, by appending `true`, you can set "descending order" to true, which has the same + * effect as the `-` prefix. * * # Examples * @@ -174,6 +174,8 @@ class OrderBy implements Function { expressions = [expression]; } else if (expression is List) { expressions = expression as List; + } else if (expression is Iterable) { + expressions = expression.toList(); } if (expressions == null || expressions.length == 0) { // AngularJS behavior. You must have an expression to get any work done. diff --git a/test/formatter/order_by_spec.dart b/test/formatter/order_by_spec.dart index 662d1f404..4dde657c0 100644 --- a/test/formatter/order_by_spec.dart +++ b/test/formatter/order_by_spec.dart @@ -170,7 +170,16 @@ main() { ]); }); - + it('should support an Iterable of expressions', + (Scope scope, Parser parse, FormatterMap formatters) { + scope.context['exp'] = ["-a", "-b"].map((x) => x); + expect(parse('items | orderBy:exp').eval(scope.context, formatters)).toEqual([ + {'a': 20, 'b': 20}, + {'a': 20, 'b': 10}, + {'a': 10, 'b': 20}, + {'a': 10, 'b': 10}, + ]); + }); it('should support function expressions', (Scope scope, Parser parse, FormatterMap formatters) {