Skip to content

Commit af691f9

Browse files
authored
Adds Relative Time options now,weeks & years (parse-community#4304)
* Adds 'now' as an option in relative time * reenables no-console in previous spot * Adds weeks,months,years and abbreviations * modified tests to address coverage * month be gone!
1 parent 84aadba commit af691f9

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

spec/MongoTransform.spec.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ describe('relativeTimeToDate', () => {
387387

388388
describe('In the future', () => {
389389
it('should parse valid natural time', () => {
390-
const text = 'in 12 days 10 hours 24 minutes 30 seconds';
390+
const text = 'in 1 year 2 weeks 12 days 10 hours 24 minutes 30 seconds';
391391
const { result, status, info } = transform.relativeTimeToDate(text, now);
392-
expect(result.toISOString()).toBe('2017-10-08T23:52:46.617Z');
392+
expect(result.toISOString()).toBe('2018-10-22T23:52:46.617Z');
393393
expect(status).toBe('success');
394394
expect(info).toBe('future');
395395
});
@@ -405,6 +405,16 @@ describe('relativeTimeToDate', () => {
405405
});
406406
});
407407

408+
describe('From now', () => {
409+
it('should equal current time', () => {
410+
const text = 'now';
411+
const { result, status, info } = transform.relativeTimeToDate(text, now);
412+
expect(result.toISOString()).toBe('2017-09-26T13:28:16.617Z');
413+
expect(status).toBe('success');
414+
expect(info).toBe('present');
415+
});
416+
});
417+
408418
describe('Error cases', () => {
409419
it('should error if string is completely gibberish', () => {
410420
expect(transform.relativeTimeToDate('gibberishasdnklasdnjklasndkl123j123')).toEqual({

spec/ParseQuery.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,6 +3152,31 @@ describe('Parse.Query testing', () => {
31523152
.then((results) => {
31533153
expect(results.length).toBe(2);
31543154
})
3155+
.then(() => {
3156+
const q = new Parse.Query('MyCustomObject');
3157+
q.greaterThan('ttl', { $relativeTime: 'now' });
3158+
return q.find({ useMasterKey: true });
3159+
})
3160+
.then((results) => {
3161+
expect(results.length).toBe(1);
3162+
})
3163+
.then(() => {
3164+
const q = new Parse.Query('MyCustomObject');
3165+
q.greaterThan('ttl', { $relativeTime: 'now' });
3166+
q.lessThan('ttl', { $relativeTime: 'in 1 day' });
3167+
return q.find({ useMasterKey: true });
3168+
})
3169+
.then((results) => {
3170+
expect(results.length).toBe(0);
3171+
})
3172+
.then(() => {
3173+
const q = new Parse.Query('MyCustomObject');
3174+
q.greaterThan('ttl', { $relativeTime: '1 year 3 weeks ago' });
3175+
return q.find({ useMasterKey: true });
3176+
})
3177+
.then((results) => {
3178+
expect(results.length).toBe(2);
3179+
})
31553180
.then(done, done.fail);
31563181
});
31573182

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ function relativeTimeToDate(text, now = new Date()) {
544544
const future = parts[0] === 'in';
545545
const past = parts[parts.length - 1] === 'ago';
546546

547-
if (!future && !past) {
547+
if (!future && !past && text !== 'now') {
548548
return { status: 'error', info: "Time should either start with 'in' or end with 'ago'" };
549549
}
550550

@@ -562,7 +562,7 @@ function relativeTimeToDate(text, now = new Date()) {
562562
parts = parts.slice(0, parts.length - 1);
563563
}
564564

565-
if (parts.length % 2 !== 0) {
565+
if (parts.length % 2 !== 0 && text !== 'now') {
566566
return {
567567
status: 'error',
568568
info: 'Invalid time string. Dangling unit or number.',
@@ -585,6 +585,21 @@ function relativeTimeToDate(text, now = new Date()) {
585585
}
586586

587587
switch(interval) {
588+
case 'yr':
589+
case 'yrs':
590+
case 'year':
591+
case 'years':
592+
seconds += val * 31536000; // 365 * 24 * 60 * 60
593+
break;
594+
595+
case 'wk':
596+
case 'wks':
597+
case 'week':
598+
case 'weeks':
599+
seconds += val * 604800; // 7 * 24 * 60 * 60
600+
break;
601+
602+
case 'd':
588603
case 'day':
589604
case 'days':
590605
seconds += val * 86400; // 24 * 60 * 60
@@ -626,13 +641,18 @@ function relativeTimeToDate(text, now = new Date()) {
626641
info: 'future',
627642
result: new Date(now.valueOf() + milliseconds)
628643
};
629-
}
630-
if (past) {
644+
} else if (past) {
631645
return {
632646
status: 'success',
633647
info: 'past',
634648
result: new Date(now.valueOf() - milliseconds)
635649
};
650+
} else {
651+
return {
652+
status: 'success',
653+
info: 'present',
654+
result: new Date(now.valueOf())
655+
}
636656
}
637657
}
638658

src/ParseServer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ class ParseServer {
256256
/* eslint-disable no-console */
257257
console.warn(`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
258258
` Cloud code and push notifications may be unavailable!\n`);
259+
/* eslint-enable no-console */
259260
if(callback) {
260261
callback(false);
261262
}

0 commit comments

Comments
 (0)