Skip to content

Commit

Permalink
feat(OrderBy): Allow ordering an Iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Aug 8, 2014
1 parent 3de00bd commit 9a594cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 5 additions & 6 deletions lib/formatter/order_by.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down Expand Up @@ -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];
Expand Down
7 changes: 7 additions & 0 deletions test/formatter/order_by_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -165,6 +170,8 @@ main() {
]);
});



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 9a594cf

Please sign in to comment.