-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
50 lines (44 loc) · 1.1 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
export type UnitOfTime =
| 'ms'
| 'sec'
| 'min'
| 'hour'
| 'day'
| 'week'
| 'month'
| 'year';
export type Duration = {
unit: UnitOfTime;
duration: number;
}
const CONVERSION_FACTORS: Record<UnitOfTime, number> = {
ms: 1,
sec: 1000,
min: 1000 * 60,
hour: 1000 * 60 * 60,
day: 1000 * 60 * 60 * 24,
week: 1000 * 60 * 60 * 24 * 7,
month: 1000 * 60 * 60 * 24 * 30,
year: 1000 * 60 * 60 * 24 * 365,
};
export const convertTime = (input: {
duration: number;
from: UnitOfTime;
to?: UnitOfTime;
}): number => {
const inputMs = input.duration * CONVERSION_FACTORS[input.from];
return input.to ? inputMs / CONVERSION_FACTORS[input.to] ?? 0 : inputMs;
};
export default convertTime;
export const setMorphInterval = (callback: () => void, duration: Duration) => {
return window.setInterval(
callback,
convertTime({ duration: duration.duration, from: duration.unit })
);
}
export const setMorphTimeout = (callback: () => void, duration: Duration) => {
return window.setTimeout(
callback,
convertTime({ duration: duration.duration, from: duration.unit })
);
};