From 78ab44d4f059baf1cf8dcf425f9605eeba7c7b5d Mon Sep 17 00:00:00 2001 From: Andy Klimczak Date: Tue, 6 Dec 2016 12:55:01 -0500 Subject: [PATCH] feat(custom-query): add support for `before` and `between` queries --- README.md | 6 ++++ index.js | 43 +++++++++++++++++++++- test.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ab24689..9ef7d3f 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,17 @@ useful when building an API and accepting various user specificed queries. * Custom query functions * `after` (date) + * `before` (date) + * `between` (date|date) | operation | query string | query object | |-----------|---------------|--------------| | after | `?after=2014-01-01` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z" } }` | | after | `?after=1388534400` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z" } }` | +| before | `?before=2014-01-01` | `{ endret: { $lt: "2014-01-01T00:00:00.000Z" } }` | +| before | `?before=1388534400` | `{ endret: { $lt: "2014-01-01T00:00:00.000Z" } }` | +| between | `?between=2014-01-01|2015-01-01` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z", $lt: "2015-01-01T00:00:00.000Z" } }` | +| between | `?between=1388534400|1420088400` | `{ endret: { $gte: "2014-01-01T00:00:00.000Z", $lt: "2015-01-01T00:00:00.000Z" } }` | ## Install diff --git a/index.js b/index.js index 25625c4..c9b57d4 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,15 @@ module.exports = function MongoQS(options) { this.custom.after = this.customAfter(this.custom.after); } + if (this.custom.before) { + this.custom.before = this.customBefore(this.custom.before); + } + + if (this.custom.between) { + this.custom.between = this.customBetween(this.custom.between); + } + + return this; }; @@ -91,7 +100,7 @@ module.exports.prototype.customNear = field => (query, point) => { } }; -module.exports.prototype.customAfter = field => (query, value) => { +function parseDate(value) { let date = value; if (!isNaN(date)) { @@ -103,6 +112,12 @@ module.exports.prototype.customAfter = field => (query, value) => { date = new Date(date); + return date; +} + +module.exports.prototype.customAfter = field => (query, value) => { + const date = parseDate(value); + if (date.toString() !== 'Invalid Date') { query[field] = { $gte: date.toISOString(), @@ -110,6 +125,32 @@ module.exports.prototype.customAfter = field => (query, value) => { } }; +module.exports.prototype.customBefore = field => (query, value) => { + const date = parseDate(value); + + if (date.toString() !== 'Invalid Date') { + query[field] = { + $lt: date.toISOString(), + }; + } +}; + +module.exports.prototype.customBetween = field => (query, value) => { + const dates = value.split('|'); + const afterValue = dates[0]; + const beforeValue = dates[1]; + + const after = parseDate(afterValue); + const before = parseDate(beforeValue); + + if (after.toString() !== 'Invalid Date' && before.toString() !== 'Invalid Date') { + query[field] = { + $gte: after.toISOString(), + $lt: before.toISOString(), + }; + } +}; + module.exports.prototype.parseString = function parseString(string, array) { let op = string[0] || ''; const eq = string[1] === '='; diff --git a/test.js b/test.js index a56b078..23fb26e 100644 --- a/test.js +++ b/test.js @@ -143,6 +143,81 @@ describe('customAfter()', () => { }); }); +describe('customBefore()', () => { + it('does not return before query for invalid date', () => { + ['foo', '2015-13-40'].forEach((date) => { + mqs.customBefore('endret')(query, date); + assert.deepEqual(query, {}); + }); + }); + + it('returns before query for valid ISO date', () => { + mqs.customBefore('endret')(query, '2014-09-22T11:50:37.843Z'); + assert.deepEqual(query, { + endret: { + $lt: '2014-09-22T11:50:37.843Z', + }, + }); + }); + + it('returns before query for milliseconds timestamp', () => { + mqs.customBefore('endret')(query, '1411386637843'); + assert.deepEqual(query, { + endret: { + $lt: '2014-09-22T11:50:37.843Z', + }, + }); + }); + + it('returns before query for unix timestamp', () => { + mqs.customBefore('endret')(query, '1411386637'); + assert.deepEqual(query, { + endret: { + $lt: '2014-09-22T11:50:37.000Z', + }, + }); + }); +}); + +describe('customBetween()', () => { + it('does not return between query for invalid date', () => { + ['foo|bar', '2015-13-40|2020-42-69'].forEach((date) => { + mqs.customBetween('endret')(query, date); + assert.deepEqual(query, {}); + }); + }); + + it('returns between query for valid ISO date', () => { + mqs.customBetween('endret')(query, '2014-09-22T11:50:37.843Z|2015-09-22T11:50:37.843Z'); + assert.deepEqual(query, { + endret: { + $gte: '2014-09-22T11:50:37.843Z', + $lt: '2015-09-22T11:50:37.843Z', + }, + }); + }); + + it('returns between query for milliseconds timestamp', () => { + mqs.customBetween('endret')(query, '1411386637843|1442922637843'); + assert.deepEqual(query, { + endret: { + $gte: '2014-09-22T11:50:37.843Z', + $lt: '2015-09-22T11:50:37.843Z', + }, + }); + }); + + it('returns before query for unix timestamp', () => { + mqs.customBetween('endret')(query, '1411386637|1442922637'); + assert.deepEqual(query, { + endret: { + $gte: '2014-09-22T11:50:37.000Z', + $lt: '2015-09-22T11:50:37.000Z', + }, + }); + }); +}); + describe('parseStringVal()', () => { describe('true', () => { [ @@ -926,6 +1001,37 @@ describe('parse()', () => { }); }); + it('returns custom before query', () => { + mqs = new MongoQS({ + custom: { + before: 'endret', + }, + }); + assert.deepEqual(mqs.parse({ + before: '2014-01-01', + }), { + endret: { + $lt: '2014-01-01T00:00:00.000Z', + }, + }); + }); + + it('returns custom between query', () => { + mqs = new MongoQS({ + custom: { + between: 'endret', + }, + }); + assert.deepEqual(mqs.parse({ + between: '2014-01-01|2015-01-01', + }), { + endret: { + $gte: '2014-01-01T00:00:00.000Z', + $lt: '2015-01-01T00:00:00.000Z', + }, + }); + }); + it('returns custom function query', () => { mqs = new MongoQS({ custom: {