Skip to content

Commit

Permalink
Merge branch 'feat/or-equal-operator'
Browse files Browse the repository at this point in the history
Close #13 #14 #15
  • Loading branch information
Hans Kristian Flaatten committed Mar 28, 2016
2 parents 873e92a + 8067067 commit 47129a8
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 3 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: 6 additions & 0 deletions examples/data.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
[{
"name": "Solrenningen",
"visits": 40,
"geojson": {
"type": "Point",
"coordinates": [6.13037, 61.00607]
},
"tags": ["Ved", "Mat", "Båt"]
},{
"name": "Norddalshytten",
"visits": 5571,
"geojson": {
"type": "Point",
"coordinates": [5.99579, 61.01340]
},
"tags": ["Ved", "Mat", "Tåke"]
},{
"name": "Åsedalen",
"visits": 10000,
"geojson": {
"type": "Point",
"coordinates": [6.22032, 60.96244]
},
"tags": ["Ved", "Mat", "Stekeovn"]
},{
"name": "Vatnane",
"visits": 9290,
"geojson": {
"type": "Point",
"coordinates": [6.32607, 61.02105]
},
"tags": ["Ved"]
},{
"name": "Selhamar",
"visits": 301,
"geojson": {
"type": "Point",
"coordinates": [6.26495, 60.91275]
},
"tags": ["Ved", "Mat", "Stekeovn", "Båt"]
},{
"name": "Vardadalsbu",
"visits": 30149,
"geojson": {
"type": "Point",
"coordinates": [5.89279, 60.94477]
Expand Down
42 changes: 41 additions & 1 deletion examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('Example App', function() {
.end(done);
});

it('returns palces with any of the following tags', function(done) {
it('returns places with any of the following tags', function(done) {
app.get(url + '?tags[]=Båt&tags[]=Stekeovn')
.expect(200)
.expect(function(res) {
Expand All @@ -87,4 +87,44 @@ describe('Example App', function() {
})
.end(done);
});

it('returns places with visits less than 40', function(done) {
app.get(url + '?visits=<40')
.expect(200)
.expect(function(res) {
assert.equal(res.body.length, 0);
})
.end(done);
});

it('returns places with visits less than or equal to 40', function(done) {
app.get(url + '?visits=<=40')
.expect(200)
.expect(function(res) {
assert.equal(res.body.length, 1);
assert.equal(res.body[0].name, 'Solrenningen');
})
.end(done);
});

it('returns places with visits greater than 10,000', function(done) {
app.get(url + '?visits=>10000')
.expect(200)
.expect(function(res) {
assert.equal(res.body.length, 1);
assert.equal(res.body[0].name, 'Vardadalsbu');
})
.end(done);
});

it('returns places with visits > or equal to 10,000', function(done) {
app.get(url + '?visits=>=10000')
.expect(200)
.expect(function(res) {
assert.equal(res.body.length, 2);
assert.equal(res.body[0].name, 'Åsedalen');
assert.equal(res.body[1].name, 'Vardadalsbu');
})
.end(done);
});
});
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 47129a8

Please sign in to comment.