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

Added option for converting date to string. #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] || '';
Expand Down
31 changes: 31 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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, {
Expand Down