-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.ts
527 lines (473 loc) · 14.6 KB
/
index.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import { Result, ValueObject } from 'rich-domain';
import { TimeZones } from './types';
const timeFormat12h = { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: true };
const timeFormat24h = { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
function commonDateFormat(timeFormat = null) {
const date = { year: 'numeric', month: '2-digit', day: '2-digit' };
const time = timeFormat ? { ...timeFormat } : null;
return { date, time };
}
const DateFormats = {
// only dates
'DD-MM-YYYY': commonDateFormat(),
'MM-DD-YYYY': commonDateFormat(),
'DD-MM-YY': commonDateFormat(),
'MM-DD-YY': commonDateFormat(),
'MM/DD/YYYY': commonDateFormat(),
'MM/DD/YY': commonDateFormat(),
'DD/MM/YYYY': commonDateFormat(),
'DD/MM/YY': commonDateFormat(),
'YYYY-MM-DD': commonDateFormat(),
'YYYY/MM/DD': commonDateFormat(),
// separate with dot
'DD.MM.YYYY': commonDateFormat(),
'MM.DD.YYYY': commonDateFormat(),
'DD.MM.YY': commonDateFormat(),
'MM.DD.YY': commonDateFormat(),
'YYYY.MM.DD': commonDateFormat(),
'DD.MM.YYYY hh:mm:ss': commonDateFormat(timeFormat12h),
'DD.MM.YY hh:mm:ss': commonDateFormat(timeFormat12h),
'MM.DD.YYYY hh:mm:ss': commonDateFormat(timeFormat12h),
'MM.DD.YY hh:mm:ss': commonDateFormat(timeFormat12h),
'DD.MM.YYYY HH:mm:ss': commonDateFormat(timeFormat24h),
'DD.MM.YY HH:mm:ss': commonDateFormat(timeFormat24h),
'MM.DD.YYYY HH:mm:ss': commonDateFormat(timeFormat24h),
'MM.DD.YY HH:mm:ss': commonDateFormat(timeFormat24h),
'YYYY.MM.DD hh:mm:ss': commonDateFormat(timeFormat12h),
'YYYY.MM.DD HH:mm:ss': commonDateFormat(timeFormat24h),
// with time 12h
'DD/MM/YYYY hh:mm:ss': commonDateFormat(timeFormat12h),
'DD-MM-YYYY hh:mm:ss': commonDateFormat(timeFormat12h),
'DD/MM/YY hh:mm:ss': commonDateFormat(timeFormat12h),
'DD-MM-YY hh:mm:ss': commonDateFormat(timeFormat12h),
'MM/DD/YYYY hh:mm:ss': commonDateFormat(timeFormat12h),
'MM-DD-YYYY hh:mm:ss': commonDateFormat(timeFormat12h),
'MM/DD/YY hh:mm:ss': commonDateFormat(timeFormat12h),
'MM-DD-YY hh:mm:ss': commonDateFormat(timeFormat12h),
// with time 24h
'DD/MM/YYYY HH:mm:ss': commonDateFormat(timeFormat24h),
'DD-MM-YYYY HH:mm:ss': commonDateFormat(timeFormat24h),
'DD/MM/YY HH:mm:ss': commonDateFormat(timeFormat24h),
'DD-MM-YY HH:mm:ss': commonDateFormat(timeFormat24h),
'MM/DD/YYYY HH:mm:ss': commonDateFormat(timeFormat24h),
'MM-DD-YYYY HH:mm:ss': commonDateFormat(timeFormat24h),
'MM/DD/YY HH:mm:ss': commonDateFormat(timeFormat24h),
'MM-DD-YY HH:mm:ss': commonDateFormat(timeFormat24h),
// manual
'YYYY/MM/DD hh:mm:ss': commonDateFormat(timeFormat12h),
'YYYY-MM-DD hh:mm:ss': commonDateFormat(timeFormat12h),
'YYYY/MM/DD HH:mm:ss': commonDateFormat(timeFormat24h),
'YYYY-MM-DD HH:mm:ss': commonDateFormat(timeFormat24h),
};
type DateFormat = keyof typeof DateFormats;
type FormatParams = { date: Intl.DateTimeFormatOptions; time: Intl.DateTimeFormatOptions | null };
export class Dates extends ValueObject<Date> {
protected static readonly MESSAGE: string = 'Invalid date value';
private readonly ONE_DAY: number = 86400000;
private readonly ONE_HOUR: number = 3600000;
private readonly ONE_MINUTE: number = 60000;
private readonly ONE_MONTH: number = 2678400000;
private readonly ONE_WEEK: number = 604800000;
private readonly ONE_YEAR: number = 31622400000;
private constructor(props: Date) {
super(props);
}
/**
* @returns instance value as Date
*/
value(): Date {
return this.props;
}
/**
*
* @param days as number to be added
* @returns instance of Dates with updated value
*/
addDays(days: number): Dates {
return new Dates(this.util.date(this.props).add(days).days());
}
/**
*
* @param months as number to be added
* @returns instance of Dates with updated value
*/
addMonths(months: number): Dates {
return new Dates(this.util.date(this.props).add(months).months());
}
/**
*
* @param hours as number to be added
* @returns instance of Dates with updated value
*/
addHours(hours: number): Dates {
return new Dates(this.util.date(this.props).add(hours).hours());
}
/**
*
* @param minutes as number to be added
* @returns instance of Dates with updated value
*/
addMinutes(minutes: number): Dates {
return new Dates(this.util.date(this.props).add(minutes).minutes());
}
/**
*
* @param weeks as number to be added
* @returns instance of Dates with updated value
*/
addWeeks(weeks: number): Dates {
return new Dates(this.util.date(this.props).add(weeks).weeks());
}
/**
*
* @param years as number to be added
* @returns instance of Dates with updated value
*/
addYears(years: number): Dates {
return new Dates(this.util.date(this.props).add(years).years());
}
/**
*
* @param days as number to be subtracted
* @returns instance of Dates with updated value
*/
subtractDays(days: number): Dates {
return new Dates(this.util.date(this.props).remove(days).days());
}
/**
*
* @param months as number to be subtracted
* @returns instance of Dates with updated value
*/
subtractMonths(months: number): Dates {
return new Dates(this.util.date(this.props).remove(months).months());
}
/**
*
* @param hours as number to be subtracted
* @returns instance of Dates with updated value
*/
subtractHours(hours: number): Dates {
return new Dates(this.util.date(this.props).remove(hours).hours());
}
/**
*
* @param minutes as number to be subtracted
* @returns instance of Dates with updated value
*/
subtractMinutes(minutes: number): Dates {
return new Dates(this.util.date(this.props).remove(minutes).minutes());
}
/**
*
* @param weeks as number to be subtracted
* @returns instance of Dates with updated value
*/
subtractWeeks(weeks: number): Dates {
return new Dates(this.util.date(this.props).remove(weeks).weeks());
}
/**
*
* @param years as number to be subtracted
* @returns instance of Dates with updated value
*/
subtractYears(years: number): Dates {
return new Dates(this.util.date(this.props).remove(years).years());
}
/**
*
* @param date as Date to be compared or instance of Dates
* @returns result as number of days
* @summary returns positive result if instance value is greater than provided value
*/
differenceInDays(date: Date | Dates): number {
if (date instanceof Date) {
const currentDays = this.props.getTime() / this.ONE_DAY;
const dateTime = date.getTime() / this.ONE_DAY;
const calc = (currentDays * 100 - dateTime * 100) / 100;
return parseFloat(calc.toFixed(2));
}
if (date instanceof Dates) {
return this.differenceInDays(date.value());
}
return 0;
}
/**
*
* @param date to be compared or instance of Dates
* @returns result as number of hours
* @summary returns positive result if instance value is greater than provided value
*/
differenceInHours(date: Date | Dates): number {
if (date instanceof Date) {
const currentDays = this.props.getTime() / this.ONE_HOUR;
const dateTime = date.getTime() / this.ONE_HOUR;
const calc = (currentDays * 100 - dateTime * 100) / 100;
return parseFloat(calc.toFixed(2));
}
if (date instanceof Dates) {
return this.differenceInHours(date.value());
}
return 0;
}
/**
*
* @param date to be compared or instance of Dates
* @returns result as number of minutes
* @summary returns positive result if instance value is greater than provided value
*/
differenceInMinutes(date: Date | Dates): number {
if (date instanceof Date) {
const currentDays = this.props.getTime() / this.ONE_MINUTE;
const dateTime = date.getTime() / this.ONE_MINUTE;
const calc = (currentDays * 100 - dateTime * 100) / 100;
return parseFloat(calc.toFixed(2));
}
if (date instanceof Dates) {
return this.differenceInMinutes(date.value());
}
return 0;
}
/**
*
* @param date to be compared or Dates
* @returns result as number of months
* @summary returns positive result if instance value is greater than provided value
*/
differenceInMonths(date: Date | Dates): number {
if (date instanceof Date) {
const currentDays = this.props.getTime() / this.ONE_MONTH;
const dateTime = date.getTime() / this.ONE_MONTH;
const calc = (currentDays * 100 - dateTime * 100) / 100;
return parseFloat(calc.toFixed(2));
}
if (date instanceof Dates) {
return this.differenceInMonths(date.value());
}
return 0;
}
/**
*
* @param date to be compared or Dates
* @returns result as number of years
* @summary returns positive result if instance value is greater than provided value
*/
differenceInYears(date: Date | Dates): number {
if (date instanceof Date) {
const currentDays = this.props.getTime() / this.ONE_YEAR;
const dateTime = date.getTime() / this.ONE_YEAR;
const calc = (currentDays * 100 - dateTime * 100) / 100;
return parseFloat(calc.toFixed(2));
}
if (date instanceof Dates) {
return this.differenceInYears(date.value());
}
return 0;
}
/**
*
* @param date to be compared or instance of Dates
* @returns result as number of weeks
* @summary returns positive result if instance value is greater than provided value
*/
differenceInWeeks(date: Date | Dates): number {
if (date instanceof Date) {
const currentDays = this.props.getTime() / this.ONE_WEEK;
const dateTime = date.getTime() / this.ONE_WEEK;
const calc = (currentDays * 100 - dateTime * 100) / 100;
return parseFloat(calc.toFixed(2));
}
if (date instanceof Dates) {
return this.differenceInWeeks(date.value());
}
return 0;
}
/**
*
* @param format pattern to be applied
* @param timeZone as TimeZone name as string to be considered
* @returns formatted date as string
*/
format(format: DateFormat, timeZone?: TimeZones): string {
const firstChars = format.slice(0, 2);
const dateLocale = firstChars === 'DD' ? 'pt-BR' : 'en-US';
const formatDate = (date: Date, formats: FormatParams) => {
const locale = (format.slice(0, 2) === 'YY') ? 'sv-SE' : undefined;
const year = (format.includes('YYYY')) ? 'numeric' : '2-digit';
const dateFn = new Intl.DateTimeFormat(locale || dateLocale, {
month: '2-digit',
day: '2-digit',
...(formats?.date ?? {}),
year,
timeZone
}).format(date);
if (formats?.time) {
const timeFn = new Intl.DateTimeFormat(dateLocale, {
...formats.time,
timeZone
}).format(date);
return { dateFn, timeFn }
}
return { dateFn, timeFn: '' };
};
const result = formatDate(this.props, DateFormats[format] as FormatParams);
const applySeparator = (date: string): string => {
if (format.includes('/')) return date.replace(/-/g, '/');
if (format.includes('-')) return date.replace(/\//g, '-');
if (format.includes('.')) return date.replace(/\/|\-/g, '.');
return date;
}
return applySeparator(`${result.dateFn} ${result.timeFn}`.trim());
}
/**
* @param value as Date or date string
* @returns true if provided value is instance of date, and false if not.
*/
public static isValidProps(value: Date | string | number): boolean {
if(typeof value === 'number') {
const date = new Date(value);
return date instanceof Date;
}
if (typeof value === 'string') {
const date = new Date(value);
return (
!isNaN(date as unknown as number) &&
date.toISOString().slice(0, 10) === value &&
!isNaN(date.getFullYear())
);
}
return this.validator.isDate(value);
}
/**
* @param value as Date or date string or number as timestamp
* @returns true if provided value is instance of date, and false if not.
*/
public static isValid(value: Date | string): boolean {
return this.isValidProps(value);
}
/**
*
* @returns true if date day is week day [Monday-Friday]
*/
isWeekday(): boolean {
return !this.isWeekend()
}
/**
*
* @returns true if date value day is weekend day [Saturday-Sunday]
*/
isWeekend(): boolean {
return this.validator.date(this.props).isWeekend();
}
/**
*
* @param date value as date.
* @returns true if date day is week day [Monday-Friday]
*/
public static isWeekday(date: Date): boolean {
return !this.isWeekend(date);
}
/**
*
* @param date value as date.
* @returns true if date day is weekend day [Sat-Sunday]
*/
public static isWeekend(date: Date): boolean {
return this.validator.date(date).isWeekend();
}
/**
*
* @param date as Date or instance of Dates
* @returns true or false. True if instance date is greater than provided value
* @example
*
* const date = new Date("1989-05-31 11:42:00");
*
* const vo = Dates.create(date).value();
*
* const isAfter = vo.isAfter(new Date());
*
* console.log(isAfter);
*
* > false
*
* ...
*/
isAfter(date: Date | Dates): boolean {
if (date instanceof Date) {
return this.validator.date(this.props).isAfterThan(date);
}
if (date instanceof Dates) {
return this.isAfter(date.value());
}
return false;
}
/**
*
* @param date as Date or instance of Dates
* @returns true or false. True if instance date is less than provided value
* @example
*
* const date = new Date("1989-05-31 11:42:00");
*
* const vo = Dates.create(date).value();
*
* const isBefore = vo.isBefore(new Date());
*
* console.log(isBefore);
*
* > true
*
* ...
*/
isBefore(date: Date | Dates): boolean {
if (date instanceof Date) {
return this.validator.date(this.props).isBeforeThan(date);
}
if (date instanceof Dates) {
return this.isBefore(date.value());
}
return false;
}
/**
*
* @param date as Date or instance of Dates
* @returns true or false. True if instance date is equal to provided value
*/
isEqualDate(date: Date | Dates): boolean {
if (date instanceof Date) {
const time = date.getTime();
const instanceTime = this.props.getTime();
return this.validator.number(time).isEqualTo(instanceTime);
}
if (date instanceof Dates) {
return this.isEqualDate(date.value());
}
return false;
}
/**
*
* @param value value as Date or date string or number as timestamp
* @returns instance of Dates or throw an error
*/
public static init(value?: Date | string | number): Dates {
if (!value) return new Dates(new Date());
const isValidValue = Dates.isValidProps(value);
if (!isValidValue) throw new Error(Dates.MESSAGE);
if (value instanceof Date) return new Dates(value);
return new Dates(new Date(value));
}
/**
*
* @param value value as Date or date string or number as timestamp
* @returns Result of Dates
*/
public static create(date?: Date | string | number): Result<Dates | null> {
const value = date ?? new Date();
const isValid = Dates.isValidProps(value);
if (!isValid) return Result.fail(Dates.MESSAGE);
if (value instanceof Date) return Result.Ok(new Dates(value));
return Result.Ok(new Dates(new Date(value)));
}
}
export default Dates;