Skip to content
This repository was archived by the owner on May 1, 2025. It is now read-only.

Commit 1bd7a52

Browse files
committed
feat(equalto): add operator for or equal to for greater/less than
1 parent 873e92a commit 1bd7a52

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ useful when building an API and accepting various user specificed queries.
1717
* Basic operators
1818
* `$eq`
1919
* `$gt`
20+
* `$gte`
2021
* `$lt`
22+
* `$lte`
2123
* `$ne`
2224
* `$in`
2325
* `$nin`
@@ -34,6 +36,8 @@ useful when building an API and accepting various user specificed queries.
3436
| not exists | `?foo=!` | `{ foo: { $exists: false }}` |
3537
| greater than | `?foo=>10` | `{ foo: { $gt: 10 }}` |
3638
| less than | `?foo=<10` | `{ foo: { $lt: 10 }}` |
39+
| greater than or equal to | `?foo=>=10` | `{ foo: { $gte: 10 }}` |
40+
| less than or equal to | `?foo=<=10` | `{ foo: { $lte: 10 }}` |
3741
| starts with | `?foo=^bar` | `{ foo: { $regex: "^bar", $options: "i" }}` |
3842
| ends with | `?foo=$bar` | `{ foo: { $regex: "bar$", $options: "i" }}` |
3943
| contains | `?foo=~bar` | `{ foo: { $regex: "bar", $options: "i" }}` |

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ module.exports.prototype.parse = function(query) {
187187
val = val.substr(1);
188188

189189
res[key] = (function() {
190+
var hasEqual = (val.charAt(0) === '=');
191+
var output = parseFloat((hasEqual ? val.substr(1) : val), 10);
190192
switch (op) {
191193
case '!':
192194
if (val) {
@@ -196,9 +198,9 @@ module.exports.prototype.parse = function(query) {
196198
}
197199
break;
198200
case '>':
199-
return { $gt: parseFloat(val, 10) };
201+
return output ? hasEqual ? { $gte: output } : { $gt: output } : {};
200202
case '<':
201-
return { $lt: parseFloat(val, 10) };
203+
return output ? hasEqual ? { $lte: output } : { $lt: output } : {};
202204
default:
203205
val = val.replace(/[^a-zæøå0-9-_.* ]/i, '');
204206
switch (op) {

test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,34 @@ describe('parse()', function() {
297297
});
298298
});
299299

300+
describe('>= operator', function() {
301+
it('returns greater than or equal to query', function() {
302+
query = qs.parse({
303+
navn: '>=10.110'
304+
});
305+
assert.deepEqual(query, {
306+
navn: {
307+
$gte: 10.110
308+
}
309+
});
310+
return assert.strictEqual(query.navn.$gte, 10.110);
311+
});
312+
});
313+
314+
describe('>= operator', function() {
315+
it('returns greater than or equal to query', function() {
316+
query = qs.parse({
317+
navn: '>=10.110'
318+
});
319+
assert.deepEqual(query, {
320+
navn: {
321+
$gte: 10.110
322+
}
323+
});
324+
return assert.strictEqual(query.navn.$gte, 10.110);
325+
});
326+
});
327+
300328
describe('< operator', function() {
301329
it('returns less than query', function() {
302330
query = qs.parse({
@@ -311,6 +339,34 @@ describe('parse()', function() {
311339
});
312340
});
313341

342+
describe('<= operator', function() {
343+
it('returns less than query or equal to', function() {
344+
query = qs.parse({
345+
navn: '<=10.110'
346+
});
347+
assert.deepEqual(query, {
348+
navn: {
349+
$lte: 10.110
350+
}
351+
});
352+
assert.strictEqual(query.navn.$lte, 10.110);
353+
});
354+
});
355+
356+
describe('<= operator', function() {
357+
it('returns less than query or equal to', function() {
358+
query = qs.parse({
359+
navn: '<=10.110'
360+
});
361+
assert.deepEqual(query, {
362+
navn: {
363+
$lte: 10.110
364+
}
365+
});
366+
assert.strictEqual(query.navn.$lte, 10.110);
367+
});
368+
});
369+
314370
describe('^ operator', function() {
315371
it('returns starts with query', function() {
316372
assert.deepEqual(qs.parse({

0 commit comments

Comments
 (0)