diff --git a/index.js b/index.js index 0fb7d3b..8bd6eed 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,10 @@ module.exports = function MongoQS(options) { this.string.toBoolean = (typeof opts.string.toBoolean === 'boolean') ? opts.string.toBoolean : true; this.string.toNumber = (typeof opts.string.toNumber === 'boolean') ? opts.string.toNumber : true; + opts.date = opts.date || {}; + this.date = opts.date || {}; + this.date.toString = (typeof opts.date.toString === 'boolean') ? opts.date.toString : true; + this.keyRegex = opts.keyRegex || /^[a-zæøå0-9-_.]+$/i; this.valRegex = opts.valRegex || /[^a-zæøå0-9-_.* ]/i; this.arrRegex = opts.arrRegex || /^[a-zæøå0-9-_.]+(\[])?$/i; @@ -115,41 +119,47 @@ function parseDate(value) { return date; } -module.exports.prototype.customAfter = field => (query, value) => { - const date = parseDate(value); +module.exports.prototype.customAfter = function customAfter(field) { + return (query, value) => { + const date = parseDate(value); - if (date.toString() !== 'Invalid Date') { - query[field] = { - $gte: date.toISOString(), - }; - } + if (date.toString() !== 'Invalid Date') { + query[field] = { + $gte: this.date.toString ? date.toISOString() : date, + }; + } + }; }; -module.exports.prototype.customBefore = field => (query, value) => { - const date = parseDate(value); +module.exports.prototype.customBefore = function customBefore(field) { + return (query, value) => { + const date = parseDate(value); - if (date.toString() !== 'Invalid Date') { - query[field] = { - $lt: date.toISOString(), - }; - } + if (date.toString() !== 'Invalid Date') { + query[field] = { + $lt: this.date.toString ? date.toISOString() : date, + }; + } + }; }; -module.exports.prototype.customBetween = field => (query, value) => { - const dates = value.split('|'); - const afterValue = dates[0]; - const beforeValue = dates[1]; +module.exports.prototype.customBetween = function customBetween(field) { + return (query, value) => { + const dates = value.split('|'); + const afterValue = dates[0]; + const beforeValue = dates[1]; - const after = parseDate(afterValue); - const before = parseDate(beforeValue); + 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(), - }; - } -}; + if (after.toString() !== 'Invalid Date' && before.toString() !== 'Invalid Date') { + query[field] = { + $gte: this.date.toString ? after.toISOString() : after, + $lt: this.date.toString ? before.toISOString() : before, + }; + } + }; +} module.exports.prototype.parseString = function parseString(string, array) { let op = string[0] || ''; diff --git a/test.js b/test.js index 23fb26e..6f86869 100644 --- a/test.js +++ b/test.js @@ -124,6 +124,16 @@ describe('customAfter()', () => { }); }); + it('returns after query for valid ISO date, toString false', () => { + mqs.date.toString = false; + mqs.customAfter('endret')(query, '2014-09-22T11:50:37.843Z'); + assert.deepEqual(query, { + endret: { + $gte: new Date('2014-09-22T11:50:37.843Z'), + }, + }); + }); + it('returns after query for milliseconds timestamp', () => { mqs.customAfter('endret')(query, '1411386637843'); assert.deepEqual(query, { @@ -160,6 +170,16 @@ describe('customBefore()', () => { }); }); + it('returns before query for valid ISO date, toString false', () => { + mqs.date.toString = false; + mqs.customBefore('endret')(query, '2014-09-22T11:50:37.843Z'); + assert.deepEqual(query, { + endret: { + $lt: new Date('2014-09-22T11:50:37.843Z'), + }, + }); + }); + it('returns before query for milliseconds timestamp', () => { mqs.customBefore('endret')(query, '1411386637843'); assert.deepEqual(query, { @@ -197,6 +217,17 @@ describe('customBetween()', () => { }); }); + it('returns between query for valid ISO date, toString false', () => { + mqs.date.toString = false; + mqs.customBetween('endret')(query, '2014-09-22T11:50:37.843Z|2015-09-22T11:50:37.843Z'); + assert.deepEqual(query, { + endret: { + $gte: new Date('2014-09-22T11:50:37.843Z'), + $lt: new Date('2015-09-22T11:50:37.843Z'), + }, + }); + }); + it('returns between query for milliseconds timestamp', () => { mqs.customBetween('endret')(query, '1411386637843|1442922637843'); assert.deepEqual(query, {