valid-moment
Version:
Convert standard date string to Date, based on moment.js; and invalidate bad formatting
109 lines (108 loc) • 3.61 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var moment_1 = __importDefault(require("moment"));
var format_1 = require("./format");
/**
* “Deprecation warning: moment construction falls back to js Date” when trying to convert RFC2822 date in moment.js
* https://stackoverflow.com/a/46410816/9023855
*/
moment_1.default.suppressDeprecationWarnings = true;
/**
* Date parser that can only reveal valid .date or .moment
*/
var Parser = /** @class */ (function () {
function Parser(s, formats) {
var e_1, _a;
if (formats === void 0) { formats = []; }
this.s = s;
this.moment = null;
try {
for (var _b = __values(__spread(format_1.DATE_FORMATS, formats)), _c = _b.next(); !_c.done; _c = _b.next()) {
var f = _c.value;
this.moment = moment_1.default(s, f);
if (this.moment.isValid()) {
break;
}
else {
this.moment = null;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
if (this.moment) {
this.moment = moment_1.default(s);
if (!this.moment.isValid()) {
this.moment = null;
}
}
}
Object.defineProperty(Parser.prototype, "date", {
get: function () {
return this.moment ? this.moment.toDate() : null;
},
enumerable: true,
configurable: true
});
return Parser;
}());
exports.Parser = Parser;
/**
* Convert string to Date only if is valid ISO 8601, RFC 2822, or local formats. Can specify additional formats.
* @param s
* @param formats
*/
function toDate(s, formats) {
if (formats === void 0) { formats = []; }
return new Parser(s, formats).date;
}
exports.toDate = toDate;
/**
* Convert string to moment only if is valid ISO 8601, RFC 2822, or local formats. Can specify additional formats.
* @param s
* @param formats
*/
function toMoment(s, formats) {
if (formats === void 0) { formats = []; }
return new Parser(s, formats).moment;
}
exports.toMoment = toMoment;