diff --git a/parse.js b/parse.js index 8a55c62..5ede29f 100644 --- a/parse.js +++ b/parse.js @@ -1,4 +1,6 @@ -// Times are parsed as UTC if no offset is specified +/** + * _Times are parsed as UTC if no offset is specified_ + */ export class IsoDateParts { static FULL_DATE_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)$/; static DATETIME_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)[Tt ]([0-2]\d(?:[\.\,]\d+)?)(?::?([0-5]\d(?:[\.\,]\d+)?)(?::?([0-5]\d))?(?:[\.\,](\d{1,9}))?)?(Z|[+-][0-2]\d(?::?[0-5]\d)?)?$/; @@ -15,6 +17,7 @@ export class IsoDateParts { }; } + /** @param {RegExpMatchArray} match */ static getByDateTime( _, // full match year = "", @@ -40,6 +43,7 @@ export class IsoDateParts { }; } + /** @param {string} str An [RFC 9557](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime#rfc_9557_format)-compatible string */ static getParts(str = "") { let dateTimeMatch = str.match(this.FULL_DATE_REGEX) ?? str.match(this.DATETIME_REGEX); if(!dateTimeMatch) { @@ -54,6 +58,24 @@ export class IsoDateParts { } export class IsoDate { + /** @type {number} */ + year; + /** @type {number} */ + month; + /** @type {number} */ + day; + /** @type {number} */ + hours; + /** @type {number} */ + minutes; + /** @type {number} */ + seconds; + /** @type {number} */ + milliseconds; + /** @type {string} */ + source; + + /** @param {string} str An [RFC 9557](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime#rfc_9557_format)-compatible string */ static parse(str) { let parts = IsoDateParts.getParts(str); if(parts) { @@ -67,11 +89,22 @@ export class IsoDate { throw new Error(`Unsupported date format: ${str}`); } + /** + * @param {object} parts + * @param {number} parts.year + * @param {number} parts.month + * @param {number} parts.day + * @param {number} parts.hours + * @param {number} parts.minutes + * @param {number} parts.seconds + * @param {number} parts.milliseconds + */ constructor(parts) { - // parts.day, parts.year, parts.month, parts.week + // parts.day, parts.year, parts.month Object.assign(this, parts); } + /** @returns {[number, number, number, number, number, number, number]} */ getArgs() { return [this.year, this.month, this.day, this.hours, this.minutes, this.seconds, this.milliseconds]; } @@ -89,6 +122,7 @@ export class IsoDate { } } +/** @param {string} str An [RFC 9557](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime#rfc_9557_format)-compatible string */ export function parse(str) { return IsoDate.parse(str); } \ No newline at end of file