Permalink
Browse files

Compare filters numerically if possible

  • Loading branch information...
1 parent 5145655 commit b3b7fec337d623893115dadc469220c2fce999fa @tmcw tmcw committed Dec 20, 2012
Showing with 13 additions and 5 deletions.
  1. +9 −1 lib/carto/tree/filterset.js
  2. +4 −4 test/filterset.test.js
@@ -84,6 +84,8 @@ tree.Filterset.prototype.addable = function(filter) {
var key = filter.key.toString(),
value = filter.val.toString();
+ if (!isNaN(parseFloat(value))) value = parseFloat(value);
+
switch (filter.op) {
case '=':
// if there is already foo= and we're adding foo=
@@ -114,7 +116,13 @@ tree.Filterset.prototype.addable = function(filter) {
return true;
case '>':
- if (key + '=' in this.filters) return (this.filters[key + '='].val <= value) ? false : null;
+ if (key + '=' in this.filters) {
+ if (this.filters[key + '='].val <= value) {
+ return false;
+ } else {
+ return null;
+ }
+ }
if (key + '<' in this.filters && this.filters[key + '<'].val <= value) return false;
if (key + '<=' in this.filters && this.filters[key + '<='].val <= value) return false;
if (key + '>' in this.filters && this.filters[key + '>'].val >= value) return null;
View
@@ -35,10 +35,10 @@ describe('Filtersets', function() {
f.add({ key: 'TOTAL', op: '=', val: '11' });
assert.ok(null === f.addable({ key: 'TOTAL', op: '=', val: '11' }), '=11 =11');
assert.ok(null === f.addable({ key: 'TOTAL', op: '!=', val: '90' }), '=11 !=90');
- assert.ok(null === f.addable({ key: 'TOTAL', op: '>', val: 9 }), '=11 >9');
- assert.ok(null === f.addable({ key: 'TOTAL', op: '>=', val: 9 }), '=11 >=9');
- assert.ok(null === f.addable({ key: 'TOTAL', op: '<', val: 90 }), '=11 <90');
- assert.ok(null === f.addable({ key: 'TOTAL', op: '<=', val: 90 }), '=11 <=90');
+ assert.ok(null === f.addable({ key: 'TOTAL', op: '>', val: '9' }), '=11 >9');
+ assert.ok(null === f.addable({ key: 'TOTAL', op: '>=', val: '9' }), '=11 >=9');
+ assert.ok(null === f.addable({ key: 'TOTAL', op: '<', val: '90' }), '=11 <90');
+ assert.ok(null === f.addable({ key: 'TOTAL', op: '<=', val: '90' }), '=11 <=90');
assert.ok(false === f.addable({ key: 'TOTAL', op: '=', val: '90' }), '=11 =90');
assert.ok(false === f.addable({ key: 'TOTAL', op: '!=', val: '11' }), '=11 !=11');
assert.ok(false === f.addable({ key: 'TOTAL', op: '>', val: 90 }), '=11 >90');

0 comments on commit b3b7fec

Please sign in to comment.