Skip to content

Commit 6481a5a

Browse files
committed
move relativeTimeToDate to Utils
1 parent 44c2c8d commit 6481a5a

File tree

4 files changed

+148
-142
lines changed

4 files changed

+148
-142
lines changed

spec/MongoTransform.spec.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const transform = require('../lib/Adapters/Storage/Mongo/MongoTransform');
55
const dd = require('deep-diff');
66
const mongodb = require('mongodb');
7+
const Utils = require('../lib/Utils');
78

89
describe('parseObjectToMongoObjectForCreate', () => {
910
it('a basic number', done => {
@@ -592,7 +593,7 @@ describe('relativeTimeToDate', () => {
592593
describe('In the future', () => {
593594
it('should parse valid natural time', () => {
594595
const text = 'in 1 year 2 weeks 12 days 10 hours 24 minutes 30 seconds';
595-
const { result, status, info } = transform.relativeTimeToDate(text, now);
596+
const { result, status, info } = Utils.relativeTimeToDate(text, now);
596597
expect(result.toISOString()).toBe('2018-10-22T23:52:46.617Z');
597598
expect(status).toBe('success');
598599
expect(info).toBe('future');
@@ -602,7 +603,7 @@ describe('relativeTimeToDate', () => {
602603
describe('In the past', () => {
603604
it('should parse valid natural time', () => {
604605
const text = '2 days 12 hours 1 minute 12 seconds ago';
605-
const { result, status, info } = transform.relativeTimeToDate(text, now);
606+
const { result, status, info } = Utils.relativeTimeToDate(text, now);
606607
expect(result.toISOString()).toBe('2017-09-24T01:27:04.617Z');
607608
expect(status).toBe('success');
608609
expect(info).toBe('past');
@@ -612,7 +613,7 @@ describe('relativeTimeToDate', () => {
612613
describe('From now', () => {
613614
it('should equal current time', () => {
614615
const text = 'now';
615-
const { result, status, info } = transform.relativeTimeToDate(text, now);
616+
const { result, status, info } = Utils.relativeTimeToDate(text, now);
616617
expect(result.toISOString()).toBe('2017-09-26T13:28:16.617Z');
617618
expect(status).toBe('success');
618619
expect(info).toBe('present');
@@ -621,54 +622,54 @@ describe('relativeTimeToDate', () => {
621622

622623
describe('Error cases', () => {
623624
it('should error if string is completely gibberish', () => {
624-
expect(transform.relativeTimeToDate('gibberishasdnklasdnjklasndkl123j123')).toEqual({
625+
expect(Utils.relativeTimeToDate('gibberishasdnklasdnjklasndkl123j123')).toEqual({
625626
status: 'error',
626627
info: "Time should either start with 'in' or end with 'ago'",
627628
});
628629
});
629630

630631
it('should error if string contains neither `ago` nor `in`', () => {
631-
expect(transform.relativeTimeToDate('12 hours 1 minute')).toEqual({
632+
expect(Utils.relativeTimeToDate('12 hours 1 minute')).toEqual({
632633
status: 'error',
633634
info: "Time should either start with 'in' or end with 'ago'",
634635
});
635636
});
636637

637638
it('should error if there are missing units or numbers', () => {
638-
expect(transform.relativeTimeToDate('in 12 hours 1')).toEqual({
639+
expect(Utils.relativeTimeToDate('in 12 hours 1')).toEqual({
639640
status: 'error',
640641
info: 'Invalid time string. Dangling unit or number.',
641642
});
642643

643-
expect(transform.relativeTimeToDate('12 hours minute ago')).toEqual({
644+
expect(Utils.relativeTimeToDate('12 hours minute ago')).toEqual({
644645
status: 'error',
645646
info: 'Invalid time string. Dangling unit or number.',
646647
});
647648
});
648649

649650
it('should error on floating point numbers', () => {
650-
expect(transform.relativeTimeToDate('in 12.3 hours')).toEqual({
651+
expect(Utils.relativeTimeToDate('in 12.3 hours')).toEqual({
651652
status: 'error',
652653
info: "'12.3' is not an integer.",
653654
});
654655
});
655656

656657
it('should error if numbers are invalid', () => {
657-
expect(transform.relativeTimeToDate('12 hours 123a minute ago')).toEqual({
658+
expect(Utils.relativeTimeToDate('12 hours 123a minute ago')).toEqual({
658659
status: 'error',
659660
info: "'123a' is not an integer.",
660661
});
661662
});
662663

663664
it('should error on invalid interval units', () => {
664-
expect(transform.relativeTimeToDate('4 score 7 years ago')).toEqual({
665+
expect(Utils.relativeTimeToDate('4 score 7 years ago')).toEqual({
665666
status: 'error',
666667
info: "Invalid interval: 'score'",
667668
});
668669
});
669670

670671
it("should error when string contains 'ago' and 'in'", () => {
671-
expect(transform.relativeTimeToDate('in 1 day 2 minutes ago')).toEqual({
672+
expect(Utils.relativeTimeToDate('in 1 day 2 minutes ago')).toEqual({
672673
status: 'error',
673674
info: "Time cannot have both 'in' and 'ago'",
674675
});

src/Adapters/Storage/Mongo/MongoTransform.js

+2-129
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import log from '../../../logger';
22
import _ from 'lodash';
33
var mongodb = require('mongodb');
44
var Parse = require('parse/node').Parse;
5+
const Utils = require('../../../Utils');
56

67
const transformKey = (className, fieldName, schema) => {
78
// Check if the schema is known since it's a built-in field.
@@ -634,133 +635,6 @@ function transformTopLevelAtom(atom, field) {
634635
}
635636
}
636637

637-
function relativeTimeToDate(text, now = new Date()) {
638-
text = text.toLowerCase();
639-
640-
let parts = text.split(' ');
641-
642-
// Filter out whitespace
643-
parts = parts.filter(part => part !== '');
644-
645-
const future = parts[0] === 'in';
646-
const past = parts[parts.length - 1] === 'ago';
647-
648-
if (!future && !past && text !== 'now') {
649-
return {
650-
status: 'error',
651-
info: "Time should either start with 'in' or end with 'ago'",
652-
};
653-
}
654-
655-
if (future && past) {
656-
return {
657-
status: 'error',
658-
info: "Time cannot have both 'in' and 'ago'",
659-
};
660-
}
661-
662-
// strip the 'ago' or 'in'
663-
if (future) {
664-
parts = parts.slice(1);
665-
} else {
666-
// past
667-
parts = parts.slice(0, parts.length - 1);
668-
}
669-
670-
if (parts.length % 2 !== 0 && text !== 'now') {
671-
return {
672-
status: 'error',
673-
info: 'Invalid time string. Dangling unit or number.',
674-
};
675-
}
676-
677-
const pairs = [];
678-
while (parts.length) {
679-
pairs.push([parts.shift(), parts.shift()]);
680-
}
681-
682-
let seconds = 0;
683-
for (const [num, interval] of pairs) {
684-
const val = Number(num);
685-
if (!Number.isInteger(val)) {
686-
return {
687-
status: 'error',
688-
info: `'${num}' is not an integer.`,
689-
};
690-
}
691-
692-
switch (interval) {
693-
case 'yr':
694-
case 'yrs':
695-
case 'year':
696-
case 'years':
697-
seconds += val * 31536000; // 365 * 24 * 60 * 60
698-
break;
699-
700-
case 'wk':
701-
case 'wks':
702-
case 'week':
703-
case 'weeks':
704-
seconds += val * 604800; // 7 * 24 * 60 * 60
705-
break;
706-
707-
case 'd':
708-
case 'day':
709-
case 'days':
710-
seconds += val * 86400; // 24 * 60 * 60
711-
break;
712-
713-
case 'hr':
714-
case 'hrs':
715-
case 'hour':
716-
case 'hours':
717-
seconds += val * 3600; // 60 * 60
718-
break;
719-
720-
case 'min':
721-
case 'mins':
722-
case 'minute':
723-
case 'minutes':
724-
seconds += val * 60;
725-
break;
726-
727-
case 'sec':
728-
case 'secs':
729-
case 'second':
730-
case 'seconds':
731-
seconds += val;
732-
break;
733-
734-
default:
735-
return {
736-
status: 'error',
737-
info: `Invalid interval: '${interval}'`,
738-
};
739-
}
740-
}
741-
742-
const milliseconds = seconds * 1000;
743-
if (future) {
744-
return {
745-
status: 'success',
746-
info: 'future',
747-
result: new Date(now.valueOf() + milliseconds),
748-
};
749-
} else if (past) {
750-
return {
751-
status: 'success',
752-
info: 'past',
753-
result: new Date(now.valueOf() - milliseconds),
754-
};
755-
} else {
756-
return {
757-
status: 'success',
758-
info: 'present',
759-
result: new Date(now.valueOf()),
760-
};
761-
}
762-
}
763-
764638
// Transforms a query constraint from REST API format to Mongo format.
765639
// A constraint is something with fields like $lt.
766640
// If it is not a valid constraint but it could be a valid something
@@ -813,7 +687,7 @@ function transformConstraint(constraint, field, count = false) {
813687
);
814688
}
815689

816-
const parserResult = relativeTimeToDate(val.$relativeTime);
690+
const parserResult = Utils.relativeTimeToDate(val.$relativeTime);
817691
if (parserResult.status === 'success') {
818692
answer[key] = parserResult.result;
819693
break;
@@ -1556,7 +1430,6 @@ module.exports = {
15561430
transformUpdate,
15571431
transformWhere,
15581432
mongoObjectToParseObject,
1559-
relativeTimeToDate,
15601433
transformConstraint,
15611434
transformPointerString,
15621435
};

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { v4 as uuidv4 } from 'uuid';
99
import sql from './sql';
1010
import { StorageAdapter } from '../StorageAdapter';
1111
import type { SchemaType, QueryType, QueryOptions } from '../StorageAdapter';
12-
import { relativeTimeToDate } from '../Mongo/MongoTransform';
12+
const Utils = require('../../../Utils');
1313

1414
const PostgresRelationDoesNotExistError = '42P01';
1515
const PostgresDuplicateRelationError = '42P07';
@@ -797,7 +797,7 @@ const buildWhereClause = ({ schema, query, index, caseInsensitive }): WhereClaus
797797
'$relativeTime can only be used with Date field'
798798
);
799799
}
800-
const parserResult = relativeTimeToDate(postgresValue.$relativeTime);
800+
const parserResult = Utils.relativeTimeToDate(postgresValue.$relativeTime);
801801
if (parserResult.status === 'success') {
802802
postgresValue = toPostgresValue(parserResult.result);
803803
} else {

0 commit comments

Comments
 (0)