Skip to content

Commit 79f2009

Browse files
authored
Merge pull request #6 from mxdvl/jsdoc
Add JSDoc Types
2 parents 028fde7 + 003ac68 commit 79f2009

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

parse.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// Times are parsed as UTC if no offset is specified
1+
/**
2+
* _Times are parsed as UTC if no offset is specified_
3+
*/
24
export class IsoDateParts {
35
static FULL_DATE_REGEX = /^([+-]\d{6}|\d{4})-?([01]\d)-?([0-3]\d)$/;
46
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 {
1517
};
1618
}
1719

20+
/** @param {RegExpMatchArray} match */
1821
static getByDateTime(
1922
_, // full match
2023
year = "",
@@ -40,6 +43,7 @@ export class IsoDateParts {
4043
};
4144
}
4245

46+
/** @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 */
4347
static getParts(str = "") {
4448
let dateTimeMatch = str.match(this.FULL_DATE_REGEX) ?? str.match(this.DATETIME_REGEX);
4549
if(!dateTimeMatch) {
@@ -54,6 +58,24 @@ export class IsoDateParts {
5458
}
5559

5660
export class IsoDate {
61+
/** @type {number} */
62+
year;
63+
/** @type {number} */
64+
month;
65+
/** @type {number} */
66+
day;
67+
/** @type {number} */
68+
hours;
69+
/** @type {number} */
70+
minutes;
71+
/** @type {number} */
72+
seconds;
73+
/** @type {number} */
74+
milliseconds;
75+
/** @type {string} */
76+
source;
77+
78+
/** @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 */
5779
static parse(str) {
5880
let parts = IsoDateParts.getParts(str);
5981
if(parts) {
@@ -67,11 +89,22 @@ export class IsoDate {
6789
throw new Error(`Unsupported date format: ${str}`);
6890
}
6991

92+
/**
93+
* @param {object} parts
94+
* @param {number} parts.year
95+
* @param {number} parts.month
96+
* @param {number} parts.day
97+
* @param {number} parts.hours
98+
* @param {number} parts.minutes
99+
* @param {number} parts.seconds
100+
* @param {number} parts.milliseconds
101+
*/
70102
constructor(parts) {
71-
// parts.day, parts.year, parts.month, parts.week
103+
// parts.day, parts.year, parts.month
72104
Object.assign(this, parts);
73105
}
74106

107+
/** @returns {[number, number, number, number, number, number, number]} */
75108
getArgs() {
76109
return [this.year, this.month, this.day, this.hours, this.minutes, this.seconds, this.milliseconds];
77110
}
@@ -89,6 +122,7 @@ export class IsoDate {
89122
}
90123
}
91124

125+
/** @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 */
92126
export function parse(str) {
93127
return IsoDate.parse(str);
94128
}

0 commit comments

Comments
 (0)