Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(OrderBy): allow specifying an Iterable expression
Browse files Browse the repository at this point in the history
Closes #1329
  • Loading branch information
vicb authored and chirayuk committed Aug 15, 2014
1 parent ddbbfac commit 00b67be
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/formatter/order_by.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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, `+`.
Expand All @@ -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
*
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions test/formatter/order_by_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ 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) {
scope.context['func'] = (e) => -(e['a'] + e['b']);
Expand Down

0 comments on commit 00b67be

Please sign in to comment.