2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import moment from 'moment';
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2018-08-29 07:26:50 -05:00
|
|
|
const units = ['y', 'M', 'w', 'd', 'h', 'm', 's'];
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2017-07-24 11:55:57 -05:00
|
|
|
export function parse(text, roundUp?, timezone?) {
|
2017-12-19 09:06:54 -06:00
|
|
|
if (!text) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (moment.isMoment(text)) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
if (_.isDate(text)) {
|
|
|
|
return moment(text);
|
|
|
|
}
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2018-08-30 02:03:11 -05:00
|
|
|
let time;
|
|
|
|
let mathString = '';
|
|
|
|
let index;
|
|
|
|
let parseString;
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (text.substring(0, 3) === 'now') {
|
|
|
|
if (timezone === 'utc') {
|
2017-07-24 11:55:57 -05:00
|
|
|
time = moment.utc();
|
|
|
|
} else {
|
|
|
|
time = moment();
|
|
|
|
}
|
2017-12-20 05:33:33 -06:00
|
|
|
mathString = text.substring('now'.length);
|
2015-09-16 12:49:05 -05:00
|
|
|
} else {
|
2017-12-20 05:33:33 -06:00
|
|
|
index = text.indexOf('||');
|
2015-09-16 12:49:05 -05:00
|
|
|
if (index === -1) {
|
|
|
|
parseString = text;
|
2017-12-20 05:33:33 -06:00
|
|
|
mathString = ''; // nothing else
|
2015-09-15 06:23:36 -05:00
|
|
|
} else {
|
2015-09-16 12:49:05 -05:00
|
|
|
parseString = text.substring(0, index);
|
|
|
|
mathString = text.substring(index + 2);
|
2015-09-15 06:23:36 -05:00
|
|
|
}
|
2015-09-16 12:49:05 -05:00
|
|
|
// We're going to just require ISO8601 timestamps, k?
|
2016-05-19 00:58:44 -05:00
|
|
|
time = moment(parseString, moment.ISO_8601);
|
2015-09-16 12:49:05 -05:00
|
|
|
}
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2015-09-16 12:49:05 -05:00
|
|
|
if (!mathString.length) {
|
|
|
|
return time;
|
2015-09-15 06:23:36 -05:00
|
|
|
}
|
|
|
|
|
2015-09-16 12:49:05 -05:00
|
|
|
return parseDateMath(mathString, time, roundUp);
|
|
|
|
}
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2015-12-16 05:21:13 -06:00
|
|
|
export function isValid(text) {
|
2018-08-29 07:26:50 -05:00
|
|
|
const date = parse(text);
|
2015-09-17 04:21:38 -05:00
|
|
|
if (!date) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (moment.isMoment(date)) {
|
|
|
|
return date.isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-16 05:21:13 -06:00
|
|
|
export function parseDateMath(mathString, time, roundUp?) {
|
2018-08-29 07:26:50 -05:00
|
|
|
const dateTime = time;
|
2018-08-30 02:03:11 -05:00
|
|
|
let i = 0;
|
2018-08-29 07:26:50 -05:00
|
|
|
const len = mathString.length;
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2015-09-16 12:49:05 -05:00
|
|
|
while (i < len) {
|
2018-08-29 07:26:50 -05:00
|
|
|
const c = mathString.charAt(i++);
|
2018-08-30 02:03:11 -05:00
|
|
|
let type;
|
|
|
|
let num;
|
|
|
|
let unit;
|
2015-09-16 12:49:05 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (c === '/') {
|
2015-09-16 12:49:05 -05:00
|
|
|
type = 0;
|
2017-12-20 05:33:33 -06:00
|
|
|
} else if (c === '+') {
|
2015-09-16 12:49:05 -05:00
|
|
|
type = 1;
|
2017-12-20 05:33:33 -06:00
|
|
|
} else if (c === '-') {
|
2015-09-16 12:49:05 -05:00
|
|
|
type = 2;
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNaN(mathString.charAt(i))) {
|
|
|
|
num = 1;
|
|
|
|
} else if (mathString.length === 2) {
|
|
|
|
num = mathString.charAt(i);
|
|
|
|
} else {
|
2018-08-29 07:26:50 -05:00
|
|
|
const numFrom = i;
|
2015-09-16 12:49:05 -05:00
|
|
|
while (!isNaN(mathString.charAt(i))) {
|
|
|
|
i++;
|
2017-12-19 09:06:54 -06:00
|
|
|
if (i > 10) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2015-09-15 06:23:36 -05:00
|
|
|
}
|
2015-09-16 12:49:05 -05:00
|
|
|
num = parseInt(mathString.substring(numFrom, i), 10);
|
|
|
|
}
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2015-09-16 12:49:05 -05:00
|
|
|
if (type === 0) {
|
|
|
|
// rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
|
|
|
|
if (num !== 1) {
|
|
|
|
return undefined;
|
2015-09-15 06:23:36 -05:00
|
|
|
}
|
2015-09-16 12:49:05 -05:00
|
|
|
}
|
|
|
|
unit = mathString.charAt(i++);
|
2015-09-15 06:23:36 -05:00
|
|
|
|
2016-09-13 15:10:44 -05:00
|
|
|
if (!_.includes(units, unit)) {
|
2015-09-16 12:49:05 -05:00
|
|
|
return undefined;
|
|
|
|
} else {
|
2015-09-15 06:23:36 -05:00
|
|
|
if (type === 0) {
|
2015-09-16 12:49:05 -05:00
|
|
|
if (roundUp) {
|
|
|
|
dateTime.endOf(unit);
|
2016-01-13 14:07:57 -06:00
|
|
|
} else {
|
2015-09-16 12:49:05 -05:00
|
|
|
dateTime.startOf(unit);
|
2015-09-15 06:23:36 -05:00
|
|
|
}
|
2015-09-16 12:49:05 -05:00
|
|
|
} else if (type === 1) {
|
|
|
|
dateTime.add(num, unit);
|
|
|
|
} else if (type === 2) {
|
|
|
|
dateTime.subtract(num, unit);
|
2015-09-15 06:23:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-16 12:49:05 -05:00
|
|
|
return dateTime;
|
2015-09-15 06:23:36 -05:00
|
|
|
}
|