aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCaitlin Potter2014-02-05 23:50:58 -0500
committerCaitlin Potter2014-03-18 22:54:46 -0400
commit37bc5ef4d87f19da47d3ab454c43d1e532c4f924 (patch)
tree0beac055610fbdb39363ca4070d8d48a7b3c21c1
parent3652831084c3788f786046b907a7361d2e89c520 (diff)
downloadangular.js-37bc5ef4d87f19da47d3ab454c43d1e532c4f924.tar.bz2
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
-rw-r--r--src/ng/filter/orderBy.js6
-rw-r--r--test/ng/filter/orderBySpec.js12
2 files changed, 18 insertions, 0 deletions
diff --git a/src/ng/filter/orderBy.js b/src/ng/filter/orderBy.js
index b6262698..faeb8ed1 100644
--- a/src/ng/filter/orderBy.js
+++ b/src/ng/filter/orderBy.js
@@ -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));
diff --git a/test/ng/filter/orderBySpec.js b/test/ng/filter/orderBySpec.js
index 5c117891..5dc96677 100644
--- a/test/ng/filter/orderBySpec.js
+++ b/test/ng/filter/orderBySpec.js
@@ -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();
+ });
});