Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 37bc5ef

Browse files
committed
fix(orderBy): support string predicates containing non-ident characters
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 #6143 Closes #6144
1 parent 3652831 commit 37bc5ef

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/ng/filter/orderBy.js

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ function orderByFilter($parse){
7474
predicate = predicate.substring(1);
7575
}
7676
get = $parse(predicate);
77+
if (get.constant) {
78+
var key = get();
79+
return reverseComparator(function(a,b) {
80+
return compare(a[key], b[key]);
81+
}, descending);
82+
}
7783
}
7884
return reverseComparator(function(a,b){
7985
return compare(get(a),get(b));

test/ng/filter/orderBySpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,16 @@ describe('Filter: orderBy', function() {
3131
toEqual([{a:2, b:1},{a:15, b:1}]);
3232
});
3333

34+
it('should support string predicates with names containing non-identifier characters', function() {
35+
expect(orderBy([{"Tip %": .25}, {"Tip %": .15}, {"Tip %": .40}], '"Tip %"'))
36+
.toEqualData([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}]);
37+
expect(orderBy([{"원": 76000}, {"원": 31000}, {"원": 156000}], '"원"'))
38+
.toEqualData([{"원": 31000}, {"원": 76000}, {"원": 156000}])
39+
});
40+
41+
it('should throw if quoted string predicate is quoted incorrectly', function() {
42+
expect(function() {
43+
return orderBy([{"Tip %": .15}, {"Tip %": .25}, {"Tip %": .40}], '"Tip %\'');
44+
}).toThrow();
45+
});
3446
});

0 commit comments

Comments
 (0)