Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion functions/utils/video-processing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const timeToSeconds = (timeStr) => {

const [timePart, msPart] = timeStr.split(',');
const parts = timePart.split(':').map(Number);
const milliseconds = msPart ? parseInt(msPart) / 1000 : 0;
const milliseconds = msPart ? parseInt(msPart, 10) / 1000 : 0;

if (parts.length === 2) {
const [minutes, seconds] = parts;
Expand Down
22 changes: 19 additions & 3 deletions tests/unit/utils/video-processing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ describe('Video Processing Utilities', () => {
throw new Error('Invalid time string');
}

const parts = timeStr.split(':').map(part => parseInt(part, 10));
const [timePart, msPart] = timeStr.split(',');
const parts = timePart.split(':').map(Number);
const milliseconds = msPart ? parseInt(msPart, 10) / 1000 : 0;

if (parts.length === 2) {
const [minutes, seconds] = parts;
return minutes * 60 + seconds;
return minutes * 60 + seconds + milliseconds;
} else if (parts.length === 3) {
const [hours, minutes, seconds] = parts;
return hours * 3600 + minutes * 60 + seconds;
return hours * 3600 + minutes * 60 + seconds + milliseconds;
} else {
throw new Error('Time string must be in HH:MM:SS or MM:SS format');
}
Expand Down Expand Up @@ -87,6 +89,20 @@ describe('Video Processing Utilities', () => {
expect(timeToSeconds('00:00:00')).toBe(0);
});

test('should convert HH:MM:SS,mmm format with milliseconds correctly', () => {
expect(timeToSeconds('00:00:00,000')).toBe(0);
expect(timeToSeconds('01:30:45,500')).toBe(5445.5);
expect(timeToSeconds('00:00:01,100')).toBe(1.1);
expect(timeToSeconds('02:15:30,250')).toBe(8130.25);
});

test('should convert MM:SS,mmm format with milliseconds correctly', () => {
expect(timeToSeconds('00:00,000')).toBe(0);
expect(timeToSeconds('10:00,100')).toBe(600.1);
expect(timeToSeconds('01:30,500')).toBe(90.5);
expect(timeToSeconds('05:45,750')).toBe(345.75);
});

test('should throw error for invalid input', () => {
expect(() => timeToSeconds('')).toThrow('Invalid time string');
expect(() => timeToSeconds(null)).toThrow('Invalid time string');
Expand Down