-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (91 loc) · 3.02 KB
/
index.js
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
import { daysToMillis, daysToSeconds, hoursToMillis, hoursToSeconds, millisToSeconds, minutesToMillis, minutesToSeconds, secondsToMillis } from './calculations.js';
import { durationUnitsDays, durationUnitsHours, durationUnitsMilliseconds, durationUnitsMinutes, durationUnitsSeconds } from './units.js';
/**
* Converts a duration amount and duration unit into milliseconds.
* @param durationAmount - Duration amount.
* @param durationUnit - Duration unit.
* @returns Total number of milliseconds.
*/
export default function toMillis(durationAmount, durationUnit) {
const durationUnitLowerCase = durationUnit.toLowerCase();
/*
* Days
*/
if (durationUnitsDays.includes(durationUnitLowerCase)) {
return daysToMillis(durationAmount);
}
/*
* Hours
*/
if (durationUnitsHours.includes(durationUnitLowerCase)) {
return hoursToMillis(durationAmount);
}
/*
* Minutes
*/
if (durationUnitsMinutes.includes(durationUnitLowerCase)) {
return minutesToMillis(durationAmount);
}
/*
* Seconds
*/
if (durationUnitsSeconds.includes(durationUnitLowerCase)) {
return secondsToMillis(durationAmount);
}
/*
* Milliseconds
*/
if (durationUnitsMilliseconds.includes(durationUnitLowerCase)) {
return durationAmount;
}
/*
* Unknown unit, throw error
*/
throw new Error(`Unknown durationUnit: ${durationUnit}`);
}
/**
* Converts a duration amount and duration unit into seconds.
* @param durationAmount - Duration amount.
* @param durationUnit - Duration unit.
* @returns Total number of seconds.
*/
export function toSeconds(durationAmount, durationUnit) {
const durationUnitLowerCase = durationUnit.toLowerCase();
/*
* Days
*/
if (durationUnitsDays.includes(durationUnitLowerCase)) {
return daysToSeconds(durationAmount);
}
/*
* Hours
*/
if (durationUnitsHours.includes(durationUnitLowerCase)) {
return hoursToSeconds(durationAmount);
}
/*
* Minutes
*/
if (durationUnitsMinutes.includes(durationUnitLowerCase)) {
return minutesToSeconds(durationAmount);
}
/*
* Seconds
*/
if (durationUnitsSeconds.includes(durationUnitLowerCase)) {
return durationAmount;
}
/*
* Milliseconds
*/
if (durationUnitsMilliseconds.includes(durationUnitLowerCase)) {
return millisToSeconds(durationAmount);
}
/*
* Unknown unit, throw error
*/
throw new Error(`Unknown durationUnit: ${durationUnit}`);
}
export { daysToMillis, daysToSeconds, hoursToMillis, hoursToSeconds, minutesToMillis, minutesToSeconds, millisToSeconds, secondsToMillis } from './calculations.js';
export { millisecondsInOneDay, millisecondsInOneHour, millisecondsInOneMinute, millisecondsInOneSecond, secondsInOneDay, secondsInOneHour, secondsInOneMinute } from './multipliers.js';
export { durationUnitsDays, durationUnitsHours, durationUnitsMilliseconds, durationUnitsMinutes, durationUnitsSeconds } from './units.js';