Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Compare strings with >, >=, < and <= #24

Merged
merged 1 commit into from

3 participants

@caseycrites

I have numbers that were being passed to me as strings that I needed to do comparisons on. This allows comparisons to be done as long as both sides of the expression are strings.

@benatkin

It would also be nice to be able to compare strings that aren't numbers (which will work with your change).

@lloyd lloyd merged commit 3d44ba2 into lloyd:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 21, 2011
  1. @caseycrites
This page is out of date. Refresh to see the latest.
Showing with 4 additions and 4 deletions.
  1. +4 −4 src/jsonselect.js
View
8 src/jsonselect.js
@@ -124,13 +124,13 @@
'%': [ 9, function(lhs, rhs) { return lhs % rhs; } ],
'+': [ 7, function(lhs, rhs) { return lhs + rhs; } ],
'-': [ 7, function(lhs, rhs) { return lhs - rhs; } ],
- '<=': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs <= rhs; } ],
- '>=': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs >= rhs; } ],
+ '<=': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs <= rhs || is(lhs, 'string') && is(rhs, 'string') && lhs <= rhs; } ],
+ '>=': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs >= rhs || is(lhs, 'string') && is(rhs, 'string') && lhs >= rhs; } ],
'$=': [ 5, function(lhs, rhs) { return is(lhs, 'string') && is(rhs, 'string') && lhs.lastIndexOf(rhs) === lhs.length - rhs.length; } ],
'^=': [ 5, function(lhs, rhs) { return is(lhs, 'string') && is(rhs, 'string') && lhs.indexOf(rhs) === 0; } ],
'*=': [ 5, function(lhs, rhs) { return is(lhs, 'string') && is(rhs, 'string') && lhs.indexOf(rhs) !== -1; } ],
- '>': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs > rhs; } ],
- '<': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs < rhs; } ],
+ '>': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs > rhs || is(lhs, 'string') && is(rhs, 'string') && lhs > rhs; } ],
+ '<': [ 5, function(lhs, rhs) { return is(lhs, 'number') && is(rhs, 'number') && lhs < rhs || is(lhs, 'string') && is(rhs, 'string') && lhs < rhs; } ],
'=': [ 3, function(lhs, rhs) { return lhs === rhs; } ],
'!=': [ 3, function(lhs, rhs) { return lhs !== rhs; } ],
'&&': [ 2, function(lhs, rhs) { return lhs && rhs; } ],
Something went wrong with that request. Please try again.