@technobuddha/library
Version:
A large library of useful functions
169 lines (168 loc) • 8.15 kB
JavaScript
;
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDate = void 0;
var isNil_1 = __importDefault(require("lodash/isNil"));
var compact_1 = __importDefault(require("lodash/compact"));
var zip_1 = __importDefault(require("lodash/zip"));
var build_1 = __importDefault(require("../build"));
var constants_1 = require("../constants");
function re(template) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return new RegExp(build_1.default(pre.source, compact_1.default(zip_1.default(template, args.map(function (a) { return a.source; })).flat()), post.source), 'u');
}
var pre = /^\s*/u;
var post = /\s*$/u;
var sep = /(?:\s*[/.-]?\s*)/u;
var mm = /([0][1-9]|[1][012])/u;
var mmm = /(jan|january|feb|february|mar|march|apr|april|may|jun|june|jul|july|aug|august|sep|sept|september|oct|october|nov|november|dec|december)/u;
var dd = /([0][1-9]|[12][0-9]|[3][01])/u;
var yyyy = /(\d{4})/u;
var time = /(?:(?:\s+|\s*t\s*)(?:(\d{1,2}):(\d{2})(?:[:](\d{1,2})(?:[.,](\d+))?)?(?:\s*(a|p)(?:m)?)?))?/ui;
var zone = /(?:\s*(?:(z|gmt)(?:([+-]\d{1,2})(?:[:](\d{2}))?)?))?/ui;
var mdyNumeric = re(templateObject_1 || (templateObject_1 = __makeTemplateObject(["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""])), mm, sep, dd, sep, yyyy, time, zone);
var ymdNumeric = re(templateObject_2 || (templateObject_2 = __makeTemplateObject(["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""])), yyyy, sep, mm, sep, dd, time, zone);
var mdyString = re(templateObject_3 || (templateObject_3 = __makeTemplateObject(["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""])), mmm, sep, dd, sep, yyyy, time, zone);
var dmyString = re(templateObject_4 || (templateObject_4 = __makeTemplateObject(["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""])), dd, sep, mmm, sep, yyyy, time, zone);
var ymdString = re(templateObject_5 || (templateObject_5 = __makeTemplateObject(["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""])), yyyy, sep, mmm, sep, dd, time, zone);
var ydmString = re(templateObject_6 || (templateObject_6 = __makeTemplateObject(["", "", "", "", "", "", "", ""], ["", "", "", "", "", "", "", ""])), yyyy, sep, dd, sep, mmm, time, zone);
var mNumeric = re(templateObject_7 || (templateObject_7 = __makeTemplateObject(["", ""], ["", ""])), mm);
var myNumeric = re(templateObject_8 || (templateObject_8 = __makeTemplateObject(["", "", "", ""], ["", "", "", ""])), mm, sep, yyyy);
var ymNumeric = re(templateObject_9 || (templateObject_9 = __makeTemplateObject(["", "", "", ""], ["", "", "", ""])), yyyy, sep, mm);
var mString = re(templateObject_10 || (templateObject_10 = __makeTemplateObject(["", ""], ["", ""])), mmm);
var myString = re(templateObject_11 || (templateObject_11 = __makeTemplateObject(["", "", "", ""], ["", "", "", ""])), mmm, sep, yyyy);
var ymString = re(templateObject_12 || (templateObject_12 = __makeTemplateObject(["", "", "", ""], ["", "", "", ""])), yyyy, sep, mmm);
var yNumeric = re(templateObject_13 || (templateObject_13 = __makeTemplateObject(["", ""], ["", ""])), yyyy);
/**
* Parse a string into a Date object
*
* @remarks this is a little more generous about what formats it will take for a date, and if it can't match the input to one of it's supported formats it falls
* back to new Date(text)
*
* @param text The string containing a date
* @returns new Date object
*/
function parseDate(text) {
var now = new Date();
var dY = 0;
var dM = 0;
var dD = 0;
var tH = 0;
var tM = 0;
var tS = 0;
var tF = 0;
var zH = null;
var zM = null;
text = text.toLowerCase();
var match;
if ((match = mdyNumeric.exec(text)) !== null) {
dM = Number.parseInt(match[1], 10) - 1;
dD = Number.parseInt(match[2], 10);
dY = Number.parseInt(match[3], 10);
}
else if ((match = ymdNumeric.exec(text)) !== null) {
dM = Number.parseInt(match[2], 10) - 1;
dD = Number.parseInt(match[3], 10);
dY = Number.parseInt(match[1], 10);
}
else if ((match = mdyString.exec(text)) !== null) {
dM = constants_1.month[match[1]];
dD = Number.parseInt(match[2], 10);
dY = Number.parseInt(match[3], 10);
}
else if ((match = dmyString.exec(text)) !== null) {
dM = constants_1.month[match[2]];
dD = Number.parseInt(match[1], 10);
dY = Number.parseInt(match[3], 10);
}
else if ((match = ymdString.exec(text)) !== null) {
dM = constants_1.month[match[2]];
dD = Number.parseInt(match[3], 10);
dY = Number.parseInt(match[1], 10);
}
else if ((match = ydmString.exec(text)) !== null) {
dM = constants_1.month[match[3]];
dD = Number.parseInt(match[2], 10);
dY = Number.parseInt(match[1], 10);
}
if (isNil_1.default(match)) {
if ((match = mNumeric.exec(text)) !== null) {
dM = Number.parseInt(match[1], 10) - 1;
dY = 1000;
dD = 1;
}
else if ((match = mString.exec(text)) !== null) {
dM = constants_1.month[match[1]];
dY = 1000;
dD = 1;
}
else if ((match = myNumeric.exec(text)) !== null) {
dM = Number.parseInt(match[1], 10) - 1;
dY = Number.parseInt(match[2], 10);
dD = 1;
}
else if ((match = ymNumeric.exec(text)) !== null) {
dM = Number.parseInt(match[2], 10) - 1;
dY = Number.parseInt(match[1], 10);
dD = 1;
}
else if ((match = myString.exec(text)) !== null) {
dM = constants_1.month[match[1]];
dY = Number.parseInt(match[2], 10);
dD = 1;
}
else if ((match = ymString.exec(text)) !== null) {
dM = constants_1.month[match[2]];
dY = Number.parseInt(match[1], 10);
dD = 1;
}
else if ((match = yNumeric.exec(text)) !== null) {
dM = 0;
dY = Number.parseInt(match[1], 10);
dD = 1;
}
else {
//We have tried everything, so default to the built-in date parsing
return new Date(Date.parse(text));
}
}
else {
tH = isNil_1.default(match[4]) ? 0 : Number.parseInt(match[4], 10);
tM = isNil_1.default(match[5]) ? 0 : Number.parseInt(match[5], 10);
tS = isNil_1.default(match[6]) ? 0 : Number.parseInt(match[6], 10);
tF = isNil_1.default(match[7]) ? 0 : Number.parseFloat("0." + match[7]) * 1000;
if (!isNil_1.default(match[8]) && match[8].toLowerCase() === 'p' && tH !== 12)
tH += 12;
else if (!isNil_1.default(match[8]) && match[8].toLowerCase() === 'a' && tH === 12)
tH -= 12;
if (!isNil_1.default(match[9])) {
zH = isNil_1.default(match[10]) ? 0 : Number.parseInt(match[10], 10);
zM = isNil_1.default(match[11]) ? 0 : Number.parseInt(match[11], 10);
}
}
now.setFullYear(dY);
now.setMonth(dM);
now.setDate(dD);
now.setHours(tH);
now.setMinutes(tM);
now.setSeconds(tS);
now.setMilliseconds(tF);
if (!isNil_1.default(zH) && !isNil_1.default(zM)) {
zH += now.getTimezoneOffset() / 60;
now.setMinutes(now.getMinutes() -
(zH < 0 ? (zH * 60 - zM) : (zH * 60 + zM))); //Adjust the time zone
}
return now;
}
exports.parseDate = parseDate;
exports.default = parseDate;
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13;