Skip to content

Commit

Permalink
fix(orderBy): support string predicates containing non-ident characters
Browse files Browse the repository at this point in the history
The orderBy filter now allows string predicates passed to the orderBy filter to make use property
name predicates containing non-ident strings, such as spaces or percent signs, or non-latin
characters.

This behaviour requires the predicate string to be double-quoted.

In markup, this might look like so:

```html
<div ng-repeat="item in items | orderBy:'\"Tip %\"'">
...
</div>
```

Or in JS:

```js
var sorted = $filter('orderBy')(array, ['"Tip %"', '-"Subtotal $"'], false);
```

Closes angular#6143
Closes angular#6144
caitp authored and tbosch committed Mar 21, 2014

Unverified

The committer email address is not verified.
1 parent 93d1c95 commit 10d3e1e
Showing 2 changed files with 18 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ng/filter/orderBy.js
Original file line number Diff line number Diff line change
@@ -74,6 +74,12 @@ function orderByFilter($parse){
predicate = predicate.substring(1);
}
get = $parse(predicate);
if (get.constant) {
var key = get();
return reverseComparator(function(a,b) {
return compare(a[key], b[key]);
}, descending);
}
}
return reverseComparator(function(a,b){
return compare(get(a),get(b));
12 changes: 12 additions & 0 deletions test/ng/filter/orderBySpec.js
Original file line number Diff line number Diff line change
@@ -31,4 +31,16 @@ describe('Filter: orderBy', function() {
toEqual([{a:2, b:1},{a:15, b:1}]);
});

it('should support string predicates with names containing non-identifier characters', function() {
expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"'))
.toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]);
expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"'))
.toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}])
});

it('should throw if quoted string predicate is quoted incorrectly', function() {
expect(function() {
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
}).toThrow();
});
});

0 comments on commit 10d3e1e

Please sign in to comment.