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

Commit ddbbfac

Browse files
vicbchirayuk
authored andcommitted
feat(OrderBy): allow ordering an Iterable
fixes #1324
1 parent b58a866 commit ddbbfac

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

lib/formatter/order_by.dart

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ part of angular.formatter_internal;
33
typedef dynamic _Mapper(dynamic e);
44

55
/**
6-
* Orders the the elements of a list using a predicate.
6+
* Orders the the elements of an [Iterable] using a predicate.
77
*
88
* # Usage
99
*
@@ -15,7 +15,7 @@ typedef dynamic _Mapper(dynamic e);
1515
* - **a custom callable expression**: an expression that will be called to transform the element
1616
* before a sort.
1717
* - **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 comparision results in the items
18+
* indicates a list of fallback expressions to use when a comparison results in the items
1919
* being equal.
2020
*
2121
* If the expression is explicitly empty(`orderBy:''`), the elements are sorted in
@@ -166,10 +166,9 @@ class OrderBy implements Function {
166166
* - `expression`: String/Function or Array of String/Function.
167167
* - `descending`: When specified, use descending order. (The default is ascending order.)
168168
*/
169-
List call(List items, var expression, [bool descending=false]) {
170-
if (items == null) {
171-
return null;
172-
}
169+
List call(Iterable items, var expression, [bool descending=false]) {
170+
if (items == null) return null;
171+
if (items is! List) items = items.toList();
173172
List expressions = null;
174173
if (expression is String || expression is _Mapper) {
175174
expressions = [expression];

test/formatter/order_by_spec.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ main() {
5454
expect(parse('list | orderBy:"-"').eval(scope.context, formatters)).toEqual([3, 2, 1]);
5555
});
5656

57+
it('should sort Iterables', (Scope scope, Parser parse, FormatterMap formatters) {
58+
scope.context['iterable'] = [1, 3, 2].map((x) => x);
59+
expect(parse('iterable | orderBy:""').eval(scope.context, formatters)).toEqual([1, 2, 3]);
60+
});
61+
5762
it('should sort by expression', (Scope scope, Parser parse, FormatterMap formatters) {
5863
expect(parse('authors | orderBy:"firstName"').eval(scope.context, formatters)).toEqual([
5964
Emily___Bronte,

0 commit comments

Comments
 (0)