Skip to content

Commit 4892e70

Browse files
committed
chore: update duration formatting logic
1 parent 8cd482c commit 4892e70

File tree

5 files changed

+40
-33
lines changed

5 files changed

+40
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Formats a test duration value in milliseconds into a human-readable format. Perf
243243

244244
#### `formatTestMessage`
245245

246-
Core formatting logic for duration in milliseconds / const formatDurationMs = (durationMs: number): string => { if (Number.isNaN(durationMs) || durationMs < 0) { return "not captured"; } if (durationMs < 1) { return "1ms"; } else if (durationMs < 1000) { return `${Math.floor(durationMs)}ms`; } else if (durationMs < 60000) { return `${(durationMs / 1000).toFixed(1)}s`; } else if (durationMs < 3600000) { const minutes = Math.floor(durationMs / 60000); const seconds = Math.floor((durationMs % 60000) / 1000); return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`; } else { const hours = Math.floor(durationMs / 3600000); const minutes = Math.floor((durationMs % 3600000) / 60000); return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`; } }; /** Converts ANSI-formatted test messages into HTML and replaces newlines with `<br>` tags. Specifically designed for formatting the `test.message` property in CTRF reports. Ideal for rendering multi-line console messages with colors in a human-friendly HTML format. This helper formats test messages so they behave well with markdown and regular HTML content.
246+
Converts ANSI-formatted test messages into HTML and replaces newlines with `<br>` tags. Specifically designed for formatting the `test.message` property in CTRF reports. Ideal for rendering multi-line console messages with colors in a human-friendly HTML format. This helper formats test messages so they behave well with markdown and regular HTML content.
247247

248248
**Parameters:**
249249

__tests__/helpers/ctrf.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ describe("CTRF Helpers", () => {
459459

460460
it("should handle negative duration (stop before start)", () => {
461461
const result = formatDurationFromTimesHelper.fn(2000, 1000);
462-
expect(result).toBe("not captured");
462+
expect(result).toBe("1ms");
463463
});
464464
});
465465

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "handlebars-helpers-ctrf",
3-
"version": "0.0.4-next.9",
3+
"version": "0.0.4-next.11",
44
"description": "A collection of Handlebars helpers for working with Common Test Report Format",
55
"type": "module",
66
"main": "dist/index.js",

src/helpers/ctrf.ts

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export const formatDurationFromTimesHelper: Helper = {
307307
const startNum = typeof start === "number" ? start : 0;
308308
const stopNum = typeof stop === "number" ? stop : 0;
309309

310-
if (startNum === 0 && stopNum === 0) {
310+
if (start === 0 && stop === 0) {
311311
return "not captured";
312312
}
313313

@@ -316,7 +316,21 @@ export const formatDurationFromTimesHelper: Helper = {
316316
}
317317

318318
const durationMs = stopNum - startNum;
319-
return formatDurationMs(durationMs);
319+
if (durationMs < 1) {
320+
return `1ms`;
321+
} else if (durationMs < 1000) {
322+
return `${Math.floor(durationMs)}ms`;
323+
} else if (durationMs < 60000) {
324+
return `${(durationMs / 1000).toFixed(1)}s`;
325+
} else if (durationMs < 3600000) {
326+
const minutes = Math.floor(durationMs / 60000);
327+
const seconds = Math.floor((durationMs % 60000) / 1000);
328+
return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
329+
} else {
330+
const hours = Math.floor(durationMs / 3600000);
331+
const minutes = Math.floor((durationMs % 3600000) / 60000);
332+
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
333+
}
320334
},
321335
};
322336

@@ -336,33 +350,26 @@ export const formatDurationHelper: Helper = {
336350
category: "CTRF",
337351
fn: (duration: unknown) => {
338352
const durationNum = typeof duration === "number" ? duration : 0;
339-
return formatDurationMs(durationNum);
340-
},
341-
};
353+
if (Number.isNaN(durationNum) || durationNum < 0) {
354+
return "not captured";
355+
}
342356

343-
/**
344-
* Core formatting logic for duration in milliseconds
345-
*/
346-
const formatDurationMs = (durationMs: number): string => {
347-
if (Number.isNaN(durationMs) || durationMs < 0) {
348-
return "not captured";
349-
}
350-
351-
if (durationMs < 1) {
352-
return "1ms";
353-
} else if (durationMs < 1000) {
354-
return `${Math.floor(durationMs)}ms`;
355-
} else if (durationMs < 60000) {
356-
return `${(durationMs / 1000).toFixed(1)}s`;
357-
} else if (durationMs < 3600000) {
358-
const minutes = Math.floor(durationMs / 60000);
359-
const seconds = Math.floor((durationMs % 60000) / 1000);
360-
return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
361-
} else {
362-
const hours = Math.floor(durationMs / 3600000);
363-
const minutes = Math.floor((durationMs % 3600000) / 60000);
364-
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
365-
}
357+
if (durationNum < 1) {
358+
return `1ms`;
359+
} else if (durationNum < 1000) {
360+
return `${Math.floor(durationNum)}ms`;
361+
} else if (durationNum < 60000) {
362+
return `${(durationNum / 1000).toFixed(1)}s`;
363+
} else if (durationNum < 3600000) {
364+
const minutes = Math.floor(durationNum / 60000);
365+
const seconds = Math.floor((durationNum % 60000) / 1000);
366+
return seconds > 0 ? `${minutes}m ${seconds}s` : `${minutes}m`;
367+
} else {
368+
const hours = Math.floor(durationNum / 3600000);
369+
const minutes = Math.floor((durationNum % 3600000) / 60000);
370+
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`;
371+
}
372+
},
366373
};
367374

368375
/**

0 commit comments

Comments
 (0)