From 1bd7a5204ddadfd87f70f7299cf44ce99338b270 Mon Sep 17 00:00:00 2001 From: Edward Knowles Date: Fri, 19 Feb 2016 10:39:26 +0000 Subject: [PATCH] feat(equalto): add operator for `or equal to` for greater/less than --- README.md | 4 ++++ index.js | 6 ++++-- test.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3894ec6..e2b538f 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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" }}` | diff --git a/index.js b/index.js index eb04ab9..01d3bf2 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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) { diff --git a/test.js b/test.js index 28fd31f..6352bf1 100644 --- a/test.js +++ b/test.js @@ -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({ @@ -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({