Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(utils): format sub-zero, non-finite, and nullish durations safely (#1513)

Henry Su2501c6139fc9
diff --git a/src/utils/formatDuration.ts b/src/utils/formatDuration.ts
index 0611b66ba..be2aa7f7a 100644
--- a/src/utils/formatDuration.ts
+++ b/src/utils/formatDuration.ts
@@ -1,4 +1,7 @@
export function formatMmSs(totalSeconds: number): string {
+ if (!Number.isFinite(totalSeconds) || totalSeconds <= 0) {
+ return "00:00";
+ }
const whole = Math.floor(totalSeconds);
const minutes = Math.floor(whole / 60);
const seconds = whole % 60;
diff --git a/test/utils/formatDuration.test.js b/test/utils/formatDuration.test.js
new file mode 100644
index 000000000..5335e154b
--- /dev/null
+++ b/test/utils/formatDuration.test.js
@@ -0,0 +1,22 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+
+const load = () => import("../../src/utils/formatDuration.ts");
+
+test("formatMmSs formats valid positive seconds", async () => {
+ const { formatMmSs } = await load();
+ assert.equal(formatMmSs(0), "00:00");
+ assert.equal(formatMmSs(5), "00:05");
+ assert.equal(formatMmSs(65), "01:05");
+ assert.equal(formatMmSs(3605), "60:05");
+});
+
+test("formatMmSs handles sub-zero, non-finite, NaN, and nullish inputs safely", async () => {
+ const { formatMmSs } = await load();
+ assert.equal(formatMmSs(-5), "00:00");
+ assert.equal(formatMmSs(NaN), "00:00");
+ assert.equal(formatMmSs(Infinity), "00:00");
+ assert.equal(formatMmSs(-Infinity), "00:00");
+ assert.equal(formatMmSs(null), "00:00");
+ assert.equal(formatMmSs(undefined), "00:00");
+});