-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.ts
159 lines (126 loc) · 3.77 KB
/
utils.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
import { MILLISECONDS_IN_MINUTE } from "./constants.ts";
import { DateObj } from "./types.ts";
export const INVALID_DATE = {
year: NaN,
month: NaN,
day: NaN,
hour: NaN,
minute: NaN,
second: NaN,
millisecond: NaN,
offsetMillisec: NaN,
} as const;
export function isLeapYear(year: number): boolean {
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
}
export function daysInYear(year: number): number {
return isLeapYear(year) ? 366 : 365;
}
export function daysInMonth(year: number, month: number): number {
const modMonth = floorMod(month - 1, 12) + 1,
modYear = year + (month - modMonth) / 12;
if (modMonth === 2) {
return isLeapYear(modYear) ? 29 : 28;
} else {
return [31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][modMonth - 1];
}
}
export function weeksInWeekYear(year: number) {
const p1 = (year +
Math.floor(year / 4) -
Math.floor(year / 100) +
Math.floor(year / 400)) %
7,
last = year - 1,
p2 = (last + Math.floor(last / 4) - Math.floor(last / 100) +
Math.floor(last / 400)) % 7;
return p1 === 4 || p2 === 3 ? 53 : 52;
}
export function isBetween(n: number, min: number, max: number): boolean {
return min <= n && n <= max;
}
function isValidMonth(month: number): boolean {
return isBetween(month, 1, 12);
}
function isValidDay(day: number, year: number, month: number): boolean {
return isBetween(day, 1, daysInMonth(year, month));
}
function isValidHour(hour: number): boolean {
return isBetween(hour, 1, 23);
}
function isValidMinute(minute: number): boolean {
return isBetween(minute, 0, 59);
}
function isValidSec(sec: number): boolean {
return isBetween(sec, 0, 59);
}
function isValidMillisec(millisecond: number): boolean {
return isBetween(millisecond, 0, 999);
}
export function isValidDate(dateObj: Partial<DateObj>): boolean {
const { year, month, day, hour, minute, second, millisecond } = dateObj;
if (!year || isNaN(year)) return false;
if (month && !isValidMonth(month)) return false;
if (month && day) {
if (!isValidDay(day, year, month)) return false;
}
if (hour) {
const isValid = isValidHour(hour) ||
(hour === 24 && minute === 0 && second === 0 && millisecond === 0);
if (!isValid) return false;
}
if (minute) {
if (!isValidMinute(minute)) return false;
}
if (second) {
if (!isValidSec(second)) return false;
}
if (millisecond) {
if (!isValidMillisec(millisecond)) return false;
}
return true;
}
export function isValidOrdinalDate(year: number, ordinal: number): boolean {
const days = daysInYear(year);
return isBetween(ordinal, 1, days);
}
export function parseInteger(value: string | undefined): number | undefined {
if (!value) return undefined;
const parsed = parseInt(value, 10);
if (isNaN(parsed)) return undefined;
return parsed;
}
export function formatToTwoDigits(n: number): string {
return n <= 9 ? `0${n}` : n.toString();
}
export function formatToThreeDigits(n: number): string {
if (n <= 9) return `00${n}`;
if (n <= 99) return `0${n}`;
return n.toString();
}
export function weeksOfYear(year: number): number {
const p1 = (year +
Math.floor(year / 4) -
Math.floor(year / 100) +
Math.floor(year / 400)) %
7,
last = year - 1,
p2 = (last + Math.floor(last / 4) - Math.floor(last / 100) +
Math.floor(last / 400)) % 7;
return p1 === 4 || p2 === 3 ? 53 : 52;
}
export function truncNumber(n?: number): number {
if (!n || isNaN(n)) {
return 0;
}
return Math.trunc(n);
}
export function floorMod(x: number, n: number) {
return x - n * Math.floor(x / n);
}
export function millisecToMin(millisec: number): number {
return millisec / MILLISECONDS_IN_MINUTE;
}
export function minToMillisec(min: number) {
return min * MILLISECONDS_IN_MINUTE;
}