From d89859ea2c88960ff3c9b229a85b836c22fa8711 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Mon, 30 Oct 2017 12:34:08 -0700 Subject: [PATCH 1/5] Adds 'now' as an option in relative time --- spec/ParseQuery.spec.js | 17 +++++++++++++++++ src/Adapters/Storage/Mongo/MongoTransform.js | 13 +++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index 15884a1cd1..759b9ed710 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -3152,6 +3152,23 @@ describe('Parse.Query testing', () => { .then((results) => { expect(results.length).toBe(2); }) + .then(() => { + const q = new Parse.Query('MyCustomObject'); + q.greaterThan('ttl', { $relativeTime: 'now' }); + return q.find({ useMasterKey: true }); + }) + .then((results) => { + expect(results.length).toBe(1); + }) + .then(() => { + const q = new Parse.Query('MyCustomObject'); + q.greaterThan('ttl', { $relativeTime: 'now' }); + q.lessThan('ttl', { $relativeTime: 'in 1 day' }); + return q.find({ useMasterKey: true }); + }) + .then((results) => { + expect(results.length).toBe(0); + }) .then(done, done.fail); }); diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index 9c5c525621..c27d2c46f7 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -544,7 +544,7 @@ function relativeTimeToDate(text, now = new Date()) { const future = parts[0] === 'in'; const past = parts[parts.length - 1] === 'ago'; - if (!future && !past) { + if (!future && !past && text !== 'now') { return { status: 'error', info: "Time should either start with 'in' or end with 'ago'" }; } @@ -562,7 +562,7 @@ function relativeTimeToDate(text, now = new Date()) { parts = parts.slice(0, parts.length - 1); } - if (parts.length % 2 !== 0) { + if (parts.length % 2 !== 0 && text !== 'now') { return { status: 'error', info: 'Invalid time string. Dangling unit or number.', @@ -626,13 +626,18 @@ function relativeTimeToDate(text, now = new Date()) { info: 'future', result: new Date(now.valueOf() + milliseconds) }; - } - if (past) { + } else if (past) { return { status: 'success', info: 'past', result: new Date(now.valueOf() - milliseconds) }; + } else { + return { + status: 'success', + info: 'present', + result: new Date(now.valueOf()) + } } } From 72cfce70ca170d133af8a04471ccb3acaaa1f44e Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Mon, 30 Oct 2017 12:34:31 -0700 Subject: [PATCH 2/5] reenables no-console in previous spot --- src/ParseServer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ParseServer.js b/src/ParseServer.js index ee2c837501..3b96080eac 100644 --- a/src/ParseServer.js +++ b/src/ParseServer.js @@ -256,6 +256,7 @@ class ParseServer { /* eslint-disable no-console */ console.warn(`\nWARNING, Unable to connect to '${Parse.serverURL}'.` + ` Cloud code and push notifications may be unavailable!\n`); + /* eslint-enable no-console */ if(callback) { callback(false); } From 55857f5d01384cc7a7b23db8c99b13f1c965ad08 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Mon, 30 Oct 2017 14:34:38 -0700 Subject: [PATCH 3/5] Adds weeks,months,years and abbreviations --- spec/ParseQuery.spec.js | 8 +++++++ src/Adapters/Storage/Mongo/MongoTransform.js | 22 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index 759b9ed710..e112cf1bee 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -3169,6 +3169,14 @@ describe('Parse.Query testing', () => { .then((results) => { expect(results.length).toBe(0); }) + .then(() => { + const q = new Parse.Query('MyCustomObject'); + q.greaterThan('ttl', { $relativeTime: '1 year 2 months 3 weeks ago' }); + return q.find({ useMasterKey: true }); + }) + .then((results) => { + expect(results.length).toBe(2); + }) .then(done, done.fail); }); diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index c27d2c46f7..f1c60c9a05 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -585,6 +585,28 @@ function relativeTimeToDate(text, now = new Date()) { } switch(interval) { + case 'yr': + case 'yrs': + case 'year': + case 'years': + seconds += val * 31536000; // 365 * 24 * 60 * 60 + break; + + case 'mo': + case 'mos': + case 'month': + case 'months': + seconds += val * 2592000; // 30 * 24 * 60 * 60, approx. 30 days/month + break; + + case 'wk': + case 'wks': + case 'week': + case 'weeks': + seconds += val * 604800; // 7 * 24 * 60 * 60 + break; + + case 'd': case 'day': case 'days': seconds += val * 86400; // 24 * 60 * 60 From 54ff5c53af1de7888aa66b169d489c59b886884f Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Mon, 30 Oct 2017 21:52:32 -0700 Subject: [PATCH 4/5] modified tests to address coverage --- spec/MongoTransform.spec.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spec/MongoTransform.spec.js b/spec/MongoTransform.spec.js index 73c179d79f..663e513e38 100644 --- a/spec/MongoTransform.spec.js +++ b/spec/MongoTransform.spec.js @@ -387,9 +387,9 @@ describe('relativeTimeToDate', () => { describe('In the future', () => { it('should parse valid natural time', () => { - const text = 'in 12 days 10 hours 24 minutes 30 seconds'; + const text = 'in 1 year 1 month 2 weeks 12 days 10 hours 24 minutes 30 seconds'; const { result, status, info } = transform.relativeTimeToDate(text, now); - expect(result.toISOString()).toBe('2017-10-08T23:52:46.617Z'); + expect(result.toISOString()).toBe('2018-11-21T23:52:46.617Z'); expect(status).toBe('success'); expect(info).toBe('future'); }); @@ -405,6 +405,16 @@ describe('relativeTimeToDate', () => { }); }); + describe('From now', () => { + it('should equal current time', () => { + const text = 'now'; + const { result, status, info } = transform.relativeTimeToDate(text, now); + expect(result.toISOString()).toBe('2017-09-26T13:28:16.617Z'); + expect(status).toBe('success'); + expect(info).toBe('present'); + }); + }); + describe('Error cases', () => { it('should error if string is completely gibberish', () => { expect(transform.relativeTimeToDate('gibberishasdnklasdnjklasndkl123j123')).toEqual({ From 43c190680312ab44e383ed7d534fc175bd8e9010 Mon Sep 17 00:00:00 2001 From: Benjamin Friedman Date: Wed, 1 Nov 2017 10:04:51 -0700 Subject: [PATCH 5/5] month be gone! --- spec/MongoTransform.spec.js | 4 ++-- spec/ParseQuery.spec.js | 2 +- src/Adapters/Storage/Mongo/MongoTransform.js | 7 ------- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/spec/MongoTransform.spec.js b/spec/MongoTransform.spec.js index 663e513e38..0c24f715b1 100644 --- a/spec/MongoTransform.spec.js +++ b/spec/MongoTransform.spec.js @@ -387,9 +387,9 @@ describe('relativeTimeToDate', () => { describe('In the future', () => { it('should parse valid natural time', () => { - const text = 'in 1 year 1 month 2 weeks 12 days 10 hours 24 minutes 30 seconds'; + const text = 'in 1 year 2 weeks 12 days 10 hours 24 minutes 30 seconds'; const { result, status, info } = transform.relativeTimeToDate(text, now); - expect(result.toISOString()).toBe('2018-11-21T23:52:46.617Z'); + expect(result.toISOString()).toBe('2018-10-22T23:52:46.617Z'); expect(status).toBe('success'); expect(info).toBe('future'); }); diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index e112cf1bee..20bc916599 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -3171,7 +3171,7 @@ describe('Parse.Query testing', () => { }) .then(() => { const q = new Parse.Query('MyCustomObject'); - q.greaterThan('ttl', { $relativeTime: '1 year 2 months 3 weeks ago' }); + q.greaterThan('ttl', { $relativeTime: '1 year 3 weeks ago' }); return q.find({ useMasterKey: true }); }) .then((results) => { diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index f1c60c9a05..01c1aa1a8a 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -592,13 +592,6 @@ function relativeTimeToDate(text, now = new Date()) { seconds += val * 31536000; // 365 * 24 * 60 * 60 break; - case 'mo': - case 'mos': - case 'month': - case 'months': - seconds += val * 2592000; // 30 * 24 * 60 * 60, approx. 30 days/month - break; - case 'wk': case 'wks': case 'week':