-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
162 lines (149 loc) · 4.67 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
/**
* A bunch of utilities to deal with dates.
*
* @packageDocumentation
*/
import { Ok, Result, err, ok } from 'neverthrow';
import { isNumber, isString, isDate } from '../is';
/**
* Pads a number to exactly 2 digits with leading zeros.
* Used if the month and day values only contain a single digit (are less than 10).
*
* @param num - The number to pad.
* @returns A `Result` object with either the padded number as a string on success, or an `Error`
* object if the provided input is not a number.
*
*
* @example
* ```typescript
* padTo2Digits(7)
* // => { ok: true, value: '07' }
* ```
*
* @example
* Handle invalid input:
* ```typescript
* padTo2Digits("hello")
* // => { ok: false, error: Error('[dates.padTo2Digits] Expected number value as input') }
* ```
*/
export function padTo2Digits(num: number): Result<string, Error> {
if (!isNumber(num)) {
return err(new Error('[dates.padTo2Digits] Expected number value as input'));
}
return ok(num.toString().padStart(2, '0'));
}
/**
* Formats a Date object into a string representation of the date in the format "dd/mm/yyyy".
*
* @param date - The date to format.
* @returns A `Result` object with either a string containing the formatted date
* in the format "dd/mm/yyyy" on success, or an `Error` object if the input value is not
* a valid `Date` object.
*
* @example
* ```typescript
* formatDate(new Date('2023-03-07T03:24:00'))
* // => { ok: true, value: '07/03/2023' }
* ```
*
* @example
* Handle invalid input:
* ```typescript
* formatDate(10)
* // => { ok: false, error: Error('[dates.formatDate] Expected string value as input') }
* ```
*/
export function formatDate(date: Date): Result<string, Error> {
if (!isDate(date)) {
return err(new Error('[dates.formatDate] Expected date value as input'));
}
const day = padTo2Digits(date.getDate()) as Ok<string, never>;
const month = padTo2Digits(date.getMonth() + 1) as Ok<string, never>;
const year = date.getFullYear();
return ok([day.value, month.value, year].join('/'));
}
/**
* Formats a Date object into a string representation of the date in the ISO 8601 format "yyyy-mm-dd".
*
* @param date - The Date object to format.
* @returns A `Result` object with a string containing the formatted date
* in the ISO 8601 format "yyyy-mm-dd" on success, or an Error object if the provided
* input is not a valid Date object.
*
* @example
* ```typescript
* formatDateISO(new Date())
* // => { ok: true, value: '2023-04-25' }
* ```
*
* @example
* Handle invalid input:
* ```typescript
* formatDateISO(10)
* // => { ok: false, error: Error('[dates.formatDateISO] Expected string value as input') }
* ```
*/
export function formatDateISO(date: Date): Result<string, Error> {
if (!isDate(date)) {
return err(new Error('[dates.formatDateISO] Expected date value as input'));
}
const year = date.getFullYear();
const month = padTo2Digits(date.getMonth() + 1) as Ok<string, never>;
const day = padTo2Digits(date.getDate()) as Ok<string, never>;
return ok([year, month.value, day.value].join('-'));
}
/**
* Extracts the day of the month as a number from a given date string.
*
* @param dateStr - The date string to extract the day of the month from.
* @returns A `Result` object with either the day of the month as a number on success,
* or an `Error` object if the input value is not a valid date string.
*
* @example
* ```typescript
* dayOfMonth("07/03/2023")
* // => { ok: true, value: '3' }
* ```
*
* @example
* Handle invalid input:
* ```typescript
* dayOfMonth(10)
* // => { ok: false, error: Error('[dates.dayOfMonth] Expected string value as input') }
* ```
*/
export function dayOfMonth(dateStr: string): Result<number, Error> {
if (!isString(dateStr)) {
return err(new Error('[dates.dayOfMonth] Expected string value as input'));
}
const dt = new Date(dateStr);
return ok(dt.getDate());
}
/**
* Extracts the short name of the month from a given date string.
*
* @param dateStr - The date string to extract the short name of the month from.
* @returns A `Result` object with either the short name of the month on success,
* or an `Error` object if the input value is not a valid date string.
*
* @example
* ```typescript
* monthShort("07/03/2023")
* // => { ok: true, value: 'Jul' }
* ```
*
* @example
* Handle invalid input:
* ```typescript
* monthShort(10)
* // => { ok: false, error: Error('[dates.monthShort] Expected string value as input') }
* ```
*/
export function monthShort(dateStr: string): Result<string, Error> {
if (!isString(dateStr)) {
return err(new Error('[dates.monthShort] Expected string value as input'));
}
const dt = new Date(dateStr);
return ok(dt.toLocaleString('default', { month: 'short' }));
}