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

Commit 47129a8

Browse files
author
Hans Kristian Flaatten
committed
Merge branch 'feat/or-equal-operator'
Close #13 #14 #15
2 parents 873e92a + 8067067 commit 47129a8

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
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" }}` |

examples/data.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
11
[{
22
"name": "Solrenningen",
3+
"visits": 40,
34
"geojson": {
45
"type": "Point",
56
"coordinates": [6.13037, 61.00607]
67
},
78
"tags": ["Ved", "Mat", "Båt"]
89
},{
910
"name": "Norddalshytten",
11+
"visits": 5571,
1012
"geojson": {
1113
"type": "Point",
1214
"coordinates": [5.99579, 61.01340]
1315
},
1416
"tags": ["Ved", "Mat", "Tåke"]
1517
},{
1618
"name": "Åsedalen",
19+
"visits": 10000,
1720
"geojson": {
1821
"type": "Point",
1922
"coordinates": [6.22032, 60.96244]
2023
},
2124
"tags": ["Ved", "Mat", "Stekeovn"]
2225
},{
2326
"name": "Vatnane",
27+
"visits": 9290,
2428
"geojson": {
2529
"type": "Point",
2630
"coordinates": [6.32607, 61.02105]
2731
},
2832
"tags": ["Ved"]
2933
},{
3034
"name": "Selhamar",
35+
"visits": 301,
3136
"geojson": {
3237
"type": "Point",
3338
"coordinates": [6.26495, 60.91275]
3439
},
3540
"tags": ["Ved", "Mat", "Stekeovn", "Båt"]
3641
},{
3742
"name": "Vardadalsbu",
43+
"visits": 30149,
3844
"geojson": {
3945
"type": "Point",
4046
"coordinates": [5.89279, 60.94477]

examples/test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('Example App', function() {
7676
.end(done);
7777
});
7878

79-
it('returns palces with any of the following tags', function(done) {
79+
it('returns places with any of the following tags', function(done) {
8080
app.get(url + '?tags[]=Båt&tags[]=Stekeovn')
8181
.expect(200)
8282
.expect(function(res) {
@@ -87,4 +87,44 @@ describe('Example App', function() {
8787
})
8888
.end(done);
8989
});
90+
91+
it('returns places with visits less than 40', function(done) {
92+
app.get(url + '?visits=<40')
93+
.expect(200)
94+
.expect(function(res) {
95+
assert.equal(res.body.length, 0);
96+
})
97+
.end(done);
98+
});
99+
100+
it('returns places with visits less than or equal to 40', function(done) {
101+
app.get(url + '?visits=<=40')
102+
.expect(200)
103+
.expect(function(res) {
104+
assert.equal(res.body.length, 1);
105+
assert.equal(res.body[0].name, 'Solrenningen');
106+
})
107+
.end(done);
108+
});
109+
110+
it('returns places with visits greater than 10,000', function(done) {
111+
app.get(url + '?visits=>10000')
112+
.expect(200)
113+
.expect(function(res) {
114+
assert.equal(res.body.length, 1);
115+
assert.equal(res.body[0].name, 'Vardadalsbu');
116+
})
117+
.end(done);
118+
});
119+
120+
it('returns places with visits > or equal to 10,000', function(done) {
121+
app.get(url + '?visits=>=10000')
122+
.expect(200)
123+
.expect(function(res) {
124+
assert.equal(res.body.length, 2);
125+
assert.equal(res.body[0].name, 'Åsedalen');
126+
assert.equal(res.body[1].name, 'Vardadalsbu');
127+
})
128+
.end(done);
129+
});
90130
});

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)