Adds Relative Time options now,weeks & years (#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!
This commit is contained in:
Benjamin Wilson Friedman
2017-11-01 13:31:08 -07:00
committed by GitHub
parent 84aadba23a
commit 46af1b6955
4 changed files with 62 additions and 6 deletions

View File

@@ -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 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-10-22T23: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({

View File

@@ -3152,6 +3152,31 @@ 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(() => {
const q = new Parse.Query('MyCustomObject');
q.greaterThan('ttl', { $relativeTime: '1 year 3 weeks ago' });
return q.find({ useMasterKey: true });
})
.then((results) => {
expect(results.length).toBe(2);
})
.then(done, done.fail);
});

View File

@@ -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.',
@@ -585,6 +585,21 @@ 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 '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
@@ -626,13 +641,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())
}
}
}

View File

@@ -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);
}