@majapisoftwares/ui 4.3.13 → 4.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils/formatTime.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Formats the given time in milliseconds into a human-readable string.
|
|
3
|
+
* @param time - The time in milliseconds.
|
|
4
|
+
* @param opts - Optional settings.
|
|
5
|
+
* @param opts.includeMs - Whether to include milliseconds in the output.
|
|
6
|
+
* @returns A string representing the formatted time.
|
|
7
|
+
*/
|
|
8
|
+
export default function formatTime(time: number, opts?: {
|
|
9
|
+
includeMs?: boolean;
|
|
10
|
+
}): string;
|
|
11
|
+
/**
|
|
12
|
+
* Parses a formatted time string and converts it to milliseconds.
|
|
13
|
+
* @param formattedTime - The formatted time string (e.g., "1h 30m 15s"), may be undefined/null.
|
|
14
|
+
* @returns The time in milliseconds.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseFormattedTime(formattedTime?: string | null): number;
|
package/utils/formatTime.js
CHANGED
|
@@ -1,40 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Formats the given time in milliseconds into a human-readable string.
|
|
3
|
+
* @param time - The time in milliseconds.
|
|
4
|
+
* @param opts - Optional settings.
|
|
5
|
+
* @param opts.includeMs - Whether to include milliseconds in the output.
|
|
6
|
+
* @returns A string representing the formatted time.
|
|
7
|
+
*/
|
|
8
|
+
export default function formatTime(time, opts) {
|
|
9
|
+
const includeMs = opts?.includeMs;
|
|
10
|
+
const totalMilliseconds = Math.max(0, Math.floor(time));
|
|
11
|
+
const totalSeconds = Math.floor(totalMilliseconds / 1000);
|
|
3
12
|
const hours = Math.floor(totalSeconds / 3600);
|
|
4
13
|
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
14
|
+
const seconds = totalSeconds % 60;
|
|
15
|
+
const ms = totalMilliseconds % 1000;
|
|
16
|
+
const parts = [];
|
|
17
|
+
if (hours > 0)
|
|
18
|
+
parts.push(`${hours}h`);
|
|
19
|
+
if (minutes > 0)
|
|
20
|
+
parts.push(`${minutes}m`);
|
|
21
|
+
if (seconds > 0)
|
|
22
|
+
parts.push(`${seconds}s`);
|
|
23
|
+
if (includeMs && ms > 0)
|
|
24
|
+
parts.push(`${ms}ms`);
|
|
25
|
+
// Maintain previous behavior: if everything is zero and ms not requested or zero, return empty string
|
|
26
|
+
if (parts.length === 0) {
|
|
27
|
+
if (includeMs && ms > 0)
|
|
28
|
+
return `${ms}ms`;
|
|
29
|
+
return "";
|
|
9
30
|
}
|
|
10
|
-
|
|
11
|
-
formattedTime += `${minutes}m `;
|
|
12
|
-
}
|
|
13
|
-
if (remainingSeconds > 0) {
|
|
14
|
-
formattedTime += `${remainingSeconds}s`;
|
|
15
|
-
}
|
|
16
|
-
return formattedTime.trim();
|
|
31
|
+
return parts.join(" ");
|
|
17
32
|
}
|
|
33
|
+
/**
|
|
34
|
+
* Parses a formatted time string and converts it to milliseconds.
|
|
35
|
+
* @param formattedTime - The formatted time string (e.g., "1h 30m 15s"), may be undefined/null.
|
|
36
|
+
* @returns The time in milliseconds.
|
|
37
|
+
*/
|
|
18
38
|
export function parseFormattedTime(formattedTime) {
|
|
19
|
-
if (!formattedTime)
|
|
39
|
+
if (!formattedTime)
|
|
20
40
|
return 0;
|
|
21
|
-
}
|
|
22
|
-
const timeParts = formattedTime.split(" ");
|
|
23
41
|
let totalMilliseconds = 0;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
42
|
+
const regex = /(\d+(?:\.\d+)?)(ms|h|m|s)/g;
|
|
43
|
+
let match;
|
|
44
|
+
while ((match = regex.exec(formattedTime)) !== null) {
|
|
45
|
+
const valueStr = match[1]; // capture group 1 is ensured by the regex pattern
|
|
46
|
+
const value = parseFloat(valueStr);
|
|
47
|
+
const unit = match[2];
|
|
48
|
+
if (isNaN(value))
|
|
49
|
+
continue;
|
|
50
|
+
switch (unit) {
|
|
51
|
+
case "h":
|
|
52
|
+
totalMilliseconds += value * 3600000;
|
|
53
|
+
break;
|
|
54
|
+
case "m":
|
|
55
|
+
totalMilliseconds += value * 60000;
|
|
56
|
+
break;
|
|
57
|
+
case "s":
|
|
58
|
+
totalMilliseconds += value * 1000;
|
|
59
|
+
break;
|
|
60
|
+
case "ms":
|
|
61
|
+
totalMilliseconds += value;
|
|
62
|
+
break;
|
|
37
63
|
}
|
|
38
|
-
}
|
|
64
|
+
}
|
|
39
65
|
return totalMilliseconds;
|
|
40
66
|
}
|