Skip to content

Commit

Permalink
feat(equalto): add operator for or equal to for greater/less than
Browse files Browse the repository at this point in the history
  • Loading branch information
eknowles committed Feb 19, 2016
1 parent 873e92a commit 1bd7a52
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ useful when building an API and accepting various user specificed queries.
* Basic operators
* `$eq`
* `$gt`
* `$gte`
* `$lt`
* `$lte`
* `$ne`
* `$in`
* `$nin`
Expand All @@ -34,6 +36,8 @@ useful when building an API and accepting various user specificed queries.
| not exists | `?foo=!` | `{ foo: { $exists: false }}` |
| greater than | `?foo=>10` | `{ foo: { $gt: 10 }}` |
| less than | `?foo=<10` | `{ foo: { $lt: 10 }}` |
| greater than or equal to | `?foo=>=10` | `{ foo: { $gte: 10 }}` |
| less than or equal to | `?foo=<=10` | `{ foo: { $lte: 10 }}` |
| starts with | `?foo=^bar` | `{ foo: { $regex: "^bar", $options: "i" }}` |
| ends with | `?foo=$bar` | `{ foo: { $regex: "bar$", $options: "i" }}` |
| contains | `?foo=~bar` | `{ foo: { $regex: "bar", $options: "i" }}` |
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ module.exports.prototype.parse = function(query) {
val = val.substr(1);

res[key] = (function() {
var hasEqual = (val.charAt(0) === '=');
var output = parseFloat((hasEqual ? val.substr(1) : val), 10);
switch (op) {
case '!':
if (val) {
Expand All @@ -196,9 +198,9 @@ module.exports.prototype.parse = function(query) {
}
break;
case '>':
return { $gt: parseFloat(val, 10) };
return output ? hasEqual ? { $gte: output } : { $gt: output } : {};
case '<':
return { $lt: parseFloat(val, 10) };
return output ? hasEqual ? { $lte: output } : { $lt: output } : {};
default:
val = val.replace(/[^a-zæøå0-9-_.* ]/i, '');
switch (op) {
Expand Down
56 changes: 56 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,34 @@ describe('parse()', function() {
});
});

describe('>= operator', function() {
it('returns greater than or equal to query', function() {
query = qs.parse({
navn: '>=10.110'
});
assert.deepEqual(query, {
navn: {
$gte: 10.110
}
});
return assert.strictEqual(query.navn.$gte, 10.110);
});
});

describe('>= operator', function() {
it('returns greater than or equal to query', function() {
query = qs.parse({
navn: '>=10.110'
});
assert.deepEqual(query, {
navn: {
$gte: 10.110
}
});
return assert.strictEqual(query.navn.$gte, 10.110);
});
});

describe('< operator', function() {
it('returns less than query', function() {
query = qs.parse({
Expand All @@ -311,6 +339,34 @@ describe('parse()', function() {
});
});

describe('<= operator', function() {
it('returns less than query or equal to', function() {
query = qs.parse({
navn: '<=10.110'
});
assert.deepEqual(query, {
navn: {
$lte: 10.110
}
});
assert.strictEqual(query.navn.$lte, 10.110);
});
});

describe('<= operator', function() {
it('returns less than query or equal to', function() {
query = qs.parse({
navn: '<=10.110'
});
assert.deepEqual(query, {
navn: {
$lte: 10.110
}
});
assert.strictEqual(query.navn.$lte, 10.110);
});
});

describe('^ operator', function() {
it('returns starts with query', function() {
assert.deepEqual(qs.parse({
Expand Down

0 comments on commit 1bd7a52

Please sign in to comment.