-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExPlainDate.ts
343 lines (323 loc) · 10.3 KB
/
ExPlainDate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import { ComPlainDate, PlainDate, PlainDateFactory } from "./PlainDate.ts";
import { createLocalInstant } from "./utils/createLocalInstant.ts";
import { createInstant } from "./utils/createInstant.ts";
import { QuarterNumber, WeekDay, WeekDayNumber } from "./constants.ts";
import { addDays } from "./utils/addDays.ts";
import { addBusinessDays } from "./utils/addBusinessDays.ts";
import { addMonths } from "./utils/addMonths.ts";
import { addQuarters } from "./utils/addQuarters.ts";
import { addYears } from "./utils/addYears.ts";
import { startOfBusinessWeek } from "./utils/startOfBusinessWeek.ts";
import { startOfWeekend } from "./utils/startOfWeekend.ts";
import { startOfMonth } from "./utils/startOfMonth.ts";
import { startOfQuarter } from "./utils/startOfQuarter.ts";
import { startOfYear } from "./utils/startOfYear.ts";
import { firstWeekDay } from "./utils/firstWeekDay.ts";
import { differenceInDays } from "./utils/differenceInDays.ts";
import { differenceInBusinessDays } from "./utils/differenceInBusinessDays.ts";
import { differenceInMonths } from "./utils/differenceInMonths.ts";
import { differenceInYears } from "./utils/differenceInYears.ts";
import { ordinal } from "./utils/ordinal.ts";
import { quarter } from "./utils/quarter.ts";
import { weekDayNumber } from "./utils/weekDayNumber.ts";
import { daysInMonth } from "./utils/daysInMonth.ts";
import { daysInYear } from "./utils/daysInYear.ts";
import { isLeapYear } from "./utils/isLeapYear.ts";
import { isBusinessDay } from "./utils/isBusinessDay.ts";
import { isWeekendDay } from "./utils/isWeekendDay.ts";
import { isFirstDayOfMonth } from "./utils/isFirstDayOfMonth.ts";
import { isLastDayOfMonth } from "./utils/isLastDayOfMonth.ts";
import { isFirstDayOfYear } from "./utils/isFirstDayOfYear.ts";
import { isLastDayOfYear } from "./utils/isLastDayOfYear.ts";
import { formatPlainDate } from "./utils/formatPlainDate.ts";
import { parsePlainDate } from "./utils/parsePlainDate.ts";
import { splitUtcDateTime } from "./utils/splitUtcDateTime.ts";
import { splitLocalDateTime } from "./utils/splitLocalDateTime.ts";
import { splitDateTime } from "./utils/splitDateTime.ts";
/**
* Describes an extended plain-date object with extra properties and
* convenience methods for common operations, of which many are chainable.
*
* @see {@link ExPlainDate} factory for creating objects
*/
export interface ExtendedPlainDate extends ComPlainDate {
/**
* Get a native JS `Date` object in the system's local timezone.
*/
toLocalInstant: (time?: {
hour?: number | string;
minute?: number | string;
second?: number | string;
millisecond?: number | string;
}) => Date;
/**
* Get a native JS `Date` object in a named timezone.
*
* @throws {RangeError} Invalid timezone specified
*/
toInstant: (timezone: string, time?: {
hour?: number | string;
minute?: number | string;
second?: number | string;
millisecond?: number | string;
}) => Date;
toLocaleStringMedium: (locale?: Intl.LocalesArgument) => string;
toLocaleStringLong: (locale?: Intl.LocalesArgument) => string;
toLocaleStringFull: (locale?: Intl.LocalesArgument) => string;
dayName: (locale?: Intl.LocalesArgument) => string;
dayNameShort: (locale?: Intl.LocalesArgument) => string;
dayNameNarrow: (locale?: Intl.LocalesArgument) => string;
monthName: (locale?: Intl.LocalesArgument) => string;
monthNameShort: (locale?: Intl.LocalesArgument) => string;
monthNameNarrow: (locale?: Intl.LocalesArgument) => string;
/** Day of the year (1-366) */
ordinal: () => number;
/** Quarter of the year (1-4) */
quarter: () => QuarterNumber;
/** ISO weekday number (1-7) starting with Monday */
weekDayNumber: () => WeekDayNumber;
/** Monday to Friday */
isBusinessDay: () => boolean;
/** Saturday or Sunday */
isWeekendDay: () => boolean;
isFirstDayOfMonth: () => boolean;
isLastDayOfMonth: () => boolean;
isFirstDayOfYear: () => boolean;
isLastDayOfYear: () => boolean;
isInLeapYear: () => boolean;
daysInMonth: () => number;
/** Common years have 365 days, leap years have 366 */
daysInYear: () => number;
addDays: (days: number) => this;
addBusinessDays: (days: number) => this;
addMonths: (months: number) => this;
addQuarters: (quarters: number) => this;
addYears: (years: number) => this;
/** Monday of the current week */
startOfBusinessWeek: () => this;
/** Saturday of the current week */
startOfWeekend: () => this;
startOfMonth: () => this;
startOfQuarter: () => this;
startOfYear: () => this;
firstMonday: () => this;
firstTuesday: () => this;
firstWednesday: () => this;
firstThursday: () => this;
firstFriday: () => this;
firstSaturday: () => this;
firstSunday: () => this;
differenceInDays: (to: ComPlainDate) => number;
differenceInBusinessDays: (to: ComPlainDate) => number;
differenceInMonths: (to: ComPlainDate) => number;
differenceInYears: (to: ComPlainDate) => number;
}
/**
* Factory function for making extended plain-date objects with extra properties
* and convenience methods.
*
* @param date A date object with properties `year`, `month` & `day`
* @returns A new immutable extended plain-date object
*/
export function ExPlainDate({ year, month = 1, day = 1 }: {
year: number | string;
month?: number | string;
day?: number | string;
}): ExtendedPlainDate {
const exPlainDate: ExtendedPlainDate = {
...PlainDate({ year, month, day }),
constructor: ExPlainDate,
toLocalInstant(
{ hour = 0, minute = 0, second = 0, millisecond = 0 } = {},
) {
return createLocalInstant({ ...this, hour, minute, second, millisecond });
},
toInstant(
timezone,
{ hour = 0, minute = 0, second = 0, millisecond = 0 } = {},
) {
return createInstant(timezone)({
...this,
hour,
minute,
second,
millisecond,
});
},
toLocaleStringMedium(locale) {
return formatPlainDate(locale)({ dateStyle: "medium" })(this);
},
toLocaleStringLong(locale) {
return formatPlainDate(locale)({ dateStyle: "long" })(this);
},
toLocaleStringFull(locale) {
return formatPlainDate(locale)({ dateStyle: "full" })(this);
},
dayName(locale) {
return formatPlainDate(locale)({ weekday: "long" })(this);
},
dayNameShort(locale) {
return formatPlainDate(locale)({ weekday: "short" })(this);
},
dayNameNarrow(locale) {
return formatPlainDate(locale)({ weekday: "narrow" })(this);
},
monthName(locale) {
return formatPlainDate(locale)({ month: "long" })(this);
},
monthNameShort(locale) {
return formatPlainDate(locale)({ month: "short" })(this);
},
monthNameNarrow(locale) {
return formatPlainDate(locale)({ month: "narrow" })(this);
},
ordinal() {
return ordinal(this);
},
quarter() {
return quarter(this);
},
weekDayNumber() {
return weekDayNumber(this);
},
isBusinessDay() {
return isBusinessDay(this);
},
isWeekendDay() {
return isWeekendDay(this);
},
isFirstDayOfMonth() {
return isFirstDayOfMonth(this);
},
isLastDayOfMonth() {
return isLastDayOfMonth(this);
},
isFirstDayOfYear() {
return isFirstDayOfYear(this);
},
isLastDayOfYear() {
return isLastDayOfYear(this);
},
isInLeapYear() {
return isLeapYear(this);
},
daysInYear() {
return daysInYear(this);
},
daysInMonth() {
return daysInMonth(this);
},
addDays(days) {
return addDays(days)(this);
},
addBusinessDays(days) {
return addBusinessDays(days)(this);
},
addMonths(months) {
return addMonths(months)(this);
},
addQuarters(quarters) {
return addQuarters(quarters)(this);
},
addYears(years) {
return addYears(years)(this);
},
startOfBusinessWeek() {
return startOfBusinessWeek(this);
},
startOfWeekend() {
return startOfWeekend(this);
},
startOfMonth() {
return startOfMonth(this);
},
startOfQuarter() {
return startOfQuarter(this);
},
startOfYear() {
return startOfYear(this);
},
firstMonday() {
return firstWeekDay(WeekDay.MONDAY)(this);
},
firstTuesday() {
return firstWeekDay(WeekDay.TUESDAY)(this);
},
firstWednesday() {
return firstWeekDay(WeekDay.WEDNESDAY)(this);
},
firstThursday() {
return firstWeekDay(WeekDay.THURSDAY)(this);
},
firstFriday() {
return firstWeekDay(WeekDay.FRIDAY)(this);
},
firstSaturday() {
return firstWeekDay(WeekDay.SATURDAY)(this);
},
firstSunday() {
return firstWeekDay(WeekDay.SUNDAY)(this);
},
differenceInDays(to) {
return differenceInDays(this)(to);
},
differenceInBusinessDays(to) {
return differenceInBusinessDays(this)(to);
},
differenceInMonths(to) {
return differenceInMonths(this)(to);
},
differenceInYears(to) {
return differenceInYears(this)(to);
},
};
return Object.freeze(exPlainDate);
}
/**
* Create a new plain-date object from an ISO string.
*/
ExPlainDate.fromString = function <T extends ComPlainDate>(
this: PlainDateFactory<T>,
isoDateString: string,
): T {
return this(parsePlainDate(isoDateString));
};
/**
* Create a new plain-date object from a native JS `Date` object in UTC.
*
* @param instant Optional JS `Date`, fallback to current wall-time
*/
ExPlainDate.fromUtcInstant = function <T extends ComPlainDate>(
this: PlainDateFactory<T>,
instant?: Date,
): T {
return this(splitUtcDateTime(instant)[0]);
};
/**
* Create a new plain-date object from a native JS `Date` object
* in the system's local timezone.
*
* @param instant Optional JS `Date`, fallback to current wall-time
*/
ExPlainDate.fromLocalInstant = function <T extends ComPlainDate>(
this: PlainDateFactory<T>,
instant?: Date,
): T {
return this(splitLocalDateTime(instant)[0]);
};
/**
* Create a new plain-date object from a native JS `Date` object
* in a specific timezone.
*
* @param timezone A named IANA timezone
* @param instant Optional JS `Date`, fallback to current wall-time
*
* @throws {RangeError} Invalid timezone specified
*/
ExPlainDate.fromInstant = function <T extends ComPlainDate>(
this: PlainDateFactory<T>,
timezone: string,
instant?: Date,
): T {
return this(splitDateTime(timezone)(instant)[0]);
};