-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.ts
113 lines (93 loc) · 2.91 KB
/
time.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
import * as times from '../info/time.json';
export interface TimeOptions {
compact?: boolean;
display?: 1 | 2 | 3 | 4 | 5 | 6 | 7;
removeMs?: boolean;
includeMsInSeconds?: boolean;
}
export const defaultTimeOptions: TimeOptions = {
compact: false,
display: 7,
removeMs: false,
includeMsInSeconds: false,
};
export interface TimeSeparated {
years?: number;
months?: number;
days?: number;
hours?: number;
minutes?: number;
seconds?: number;
milliseconds?: number;
}
function replace(inputString: string): string {
const lastIndex = inputString.lastIndexOf(',');
if (lastIndex === -1) {
return inputString;
}
const beforeLastComma = inputString.slice(0, lastIndex);
const afterLastComma = inputString.slice(lastIndex + 1);
return beforeLastComma + " e" + afterLastComma;
}
export default (input: number, options: TimeOptions = defaultTimeOptions): string => {
if (isNaN(input)) throw new TypeError('Parâmetro input deve ser do tipo numérico');
const userOption: TimeOptions = { ...defaultTimeOptions, ...options };
const intervalsInMilliseconds: Record<string, number> = {
years: 1000 * 60 * 60 * 24 * 365.25,
months: 1000 * 60 * 60 * 24 * 30.4375,
days: 1000 * 60 * 60 * 24,
hours: 1000 * 60 * 60,
minutes: 1000 * 60,
seconds: 1000,
milliseconds: 1,
};
let timeIntervals: Record<string, number | undefined> = {};
let ms: number = input;
for (const interval in intervalsInMilliseconds) {
if (ms >= intervalsInMilliseconds[interval]) {
const val: number = Math.floor(ms / intervalsInMilliseconds[interval]);
timeIntervals[interval] = val;
ms -= val * intervalsInMilliseconds[interval];
}
}
const time: TimeSeparated = {};
for (const interval in timeIntervals) {
const val: number | undefined = timeIntervals[interval];
if (val && userOption.display) {
if (interval === 'milliseconds') {
if (userOption.removeMs) continue;
if (userOption.includeMsInSeconds && time.seconds) {
time.seconds = Number(time.seconds + '.' + String(val).padStart(3, '0').slice(0, 1));
} else {
time.milliseconds = val;
}
} else {
time[interval as keyof TimeSeparated] = val;
}
userOption.display--;
}
}
let result = '';
let isFirst = true;
for (const interval in times) {
const val: {
plural: string;
unique: string;
compact: string;
} = times[interval as keyof typeof times];
const value = time[interval as keyof TimeSeparated];
if (value !== undefined) {
result += !isFirst && !userOption.compact ? ", " : " ";
if (userOption.compact) {
result += `${value}${val.compact}`;
} else if (value > 1) {
result += `${value} ${val.plural}`;
} else {
result += `${value} ${val.unique}`;
}
isFirst = false;
}
}
result = replace(result);
return result.trim() || "";
};