Skip to content

Commit 26c75bf

Browse files
authored
fix: time converter function incorrectly returns NaN for colon-separated strings (#1056)
- e.g. `hmsToMilliSeconds('06:50')` currently returns NaN. This bug was introduced during typescript migration.
1 parent 3772cb9 commit 26c75bf

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

src/lib/shared/time-functions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// convert HH:MM:SS or MM:SS to milliseconds
2-
export function hmsToMilliSeconds(str: number | string | undefined): number {
3-
if (typeof str == 'undefined' || str === '' || isNaN(Number(str))) return NaN;
2+
export function hmsToMilliSeconds(str: number | string | null | undefined): number {
3+
if (typeof str == 'undefined' || str === null || str === '') return NaN;
44
if (typeof str == 'number') return str;
55
const t = str.split(':');
66
let s = 0;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { hmsToMilliSeconds } from '../../src/lib/shared/time-functions';
4+
5+
describe('hmsToMilliSeconds', () => {
6+
it('converts MM:SS durations to milliseconds', () => {
7+
expect(hmsToMilliSeconds('06:50')).toBe(410_000);
8+
});
9+
10+
it('converts HH:MM:SS durations to milliseconds', () => {
11+
expect(hmsToMilliSeconds('1:02:03')).toBe(3_723_000);
12+
});
13+
14+
it('keeps numeric millisecond durations unchanged', () => {
15+
expect(hmsToMilliSeconds(410_000)).toBe(410_000);
16+
});
17+
18+
it('returns NaN for missing or empty durations', () => {
19+
expect(hmsToMilliSeconds(undefined)).toBeNaN();
20+
expect(hmsToMilliSeconds(null)).toBeNaN();
21+
expect(hmsToMilliSeconds('')).toBeNaN();
22+
expect(hmsToMilliSeconds(NaN)).toBeNaN();
23+
expect(hmsToMilliSeconds('NaN')).toBeNaN();
24+
expect(hmsToMilliSeconds('random text')).toBeNaN();
25+
});
26+
});

0 commit comments

Comments
 (0)