-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
33 lines (27 loc) · 1.05 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
import type { UTCDateOffset, UTCDateParams } from './custom_typings.js';
export function utcDateSync({
startDate,
offset,
}: UTCDateParams = {}): Date {
const dated = null == startDate ? new Date() : new Date(startDate);
const { year, month, day }: UTCDateOffset = offset || {};
const validatedYear = +(null == year ? 0 : year);
const validatedMonth = +(null == month ? 0 : month);
const validatedDay = +(null == day ? 0 : day);
if (isNaN(validatedYear)) {
throw new TypeError(`Expected 'year' to be a valid number, but received '${year}'`);
}
if (isNaN(validatedMonth)) {
throw new TypeError(`Expected 'month' to be a valid number, but received '${month}'`);
}
if (isNaN(validatedDay)) {
throw new TypeError(`Expected 'day' to be a valid number, but received '${day}'`);
}
return new Date(Date.UTC(
dated.getUTCFullYear() + validatedYear,
dated.getUTCMonth() + validatedMonth,
dated.getUTCDate() + validatedDay));
}
export async function utcDate(options?: UTCDateParams): Promise<Date> {
return utcDateSync(options);
}