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

Commit 00b67be

Browse files
vicbchirayuk
authored andcommitted
feat(OrderBy): allow specifying an Iterable expression
Closes #1329
1 parent ddbbfac commit 00b67be

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lib/formatter/order_by.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ typedef dynamic _Mapper(dynamic e);
1414
* - **a string**: a string containing an expression, such as "user.lastName", used to order the list.
1515
* - **a custom callable expression**: an expression that will be called to transform the element
1616
* before a sort.
17-
* - **a list**: the list may consist of either strings or callable expressions. A list expression
18-
* indicates a list of fallback expressions to use when a comparison results in the items
19-
* being equal.
17+
* - **an [Iterable]**: it may consist of either strings or callable expressions. A list expression
18+
* indicates a list of fallback expressions to use when a comparison results in the items being
19+
* equal.
2020
*
2121
* If the expression is explicitly empty(`orderBy:''`), the elements are sorted in
2222
* ascending order, using the default comparator, `+`.
@@ -26,8 +26,8 @@ typedef dynamic _Mapper(dynamic e);
2626
* - `+`: sort the elements in ascending order. This is the default.
2727
* - `-`: sort the elements in descending order.
2828
*
29-
* Alternately, by appending `true`, you can set "descending order" to true, which has the same effect as the `-`
30-
* prefix.
29+
* Alternately, by appending `true`, you can set "descending order" to true, which has the same
30+
* effect as the `-` prefix.
3131
*
3232
* # Examples
3333
*
@@ -174,6 +174,8 @@ class OrderBy implements Function {
174174
expressions = [expression];
175175
} else if (expression is List) {
176176
expressions = expression as List;
177+
} else if (expression is Iterable) {
178+
expressions = expression.toList();
177179
}
178180
if (expressions == null || expressions.length == 0) {
179181
// AngularJS behavior. You must have an expression to get any work done.

test/formatter/order_by_spec.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ main() {
170170
]);
171171
});
172172

173+
it('should support an Iterable of expressions',
174+
(Scope scope, Parser parse, FormatterMap formatters) {
175+
scope.context['exp'] = ["-a", "-b"].map((x) => x);
176+
expect(parse('items | orderBy:exp').eval(scope.context, formatters)).toEqual([
177+
{'a': 20, 'b': 20},
178+
{'a': 20, 'b': 10},
179+
{'a': 10, 'b': 20},
180+
{'a': 10, 'b': 10},
181+
]);
182+
});
183+
173184
it('should support function expressions',
174185
(Scope scope, Parser parse, FormatterMap formatters) {
175186
scope.context['func'] = (e) => -(e['a'] + e['b']);

0 commit comments

Comments
 (0)