@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.
@@ -1,2 +1,16 @@
1
- export default function formatTime(time: number): string;
2
- export declare function parseFormattedTime(formattedTime: string): number;
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;
@@ -1,40 +1,66 @@
1
- export default function formatTime(time) {
2
- const totalSeconds = Math.floor(time / 1000);
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 remainingSeconds = totalSeconds % 60;
6
- let formattedTime = "";
7
- if (hours > 0) {
8
- formattedTime += `${hours}h `;
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
- if (minutes > 0) {
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
- timeParts.forEach((part) => {
25
- const unit = part.slice(-1); // Get the last character (h, m, or s)
26
- const value = parseFloat(part.slice(0, -1)); // Get the numeric value
27
- if (!isNaN(value)) {
28
- if (unit === "h") {
29
- totalMilliseconds += value * 3600000; // Convert hours to milliseconds
30
- }
31
- else if (unit === "m") {
32
- totalMilliseconds += value * 60000; // Convert minutes to milliseconds
33
- }
34
- else if (unit === "s") {
35
- totalMilliseconds += value * 1000; // Convert seconds to milliseconds
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
  }