Don't return NaN for timeToInt

This commit is contained in:
Chocobozzz
2026-07-13 14:01:17 +02:00
parent cce19088a5
commit 1863366582
2 changed files with 35 additions and 1 deletions
+2
View File
@@ -74,6 +74,8 @@ export function timeToInt (time: number | string) {
let result = 0
for (let i = 0; i < parts.length; i++) {
if (iMultiplier[i] === undefined) return 0
const partInt = parseInt(parts[i], 10)
if (isNaN(partInt)) return 0
+33 -1
View File
@@ -9,7 +9,8 @@ import {
isYesterday,
millisecondsToTime,
millisecondsToVttTime,
secondsToTime
secondsToTime,
timeToInt
} from '@peertube/peertube-core-utils'
import { expect } from 'chai'
@@ -111,6 +112,37 @@ describe('Is last week', function () {
})
})
describe('Time to int', function () {
it('Returns 0 for falsy input', function () {
expect(timeToInt(0)).to.equal(0)
expect(timeToInt('')).to.equal(0)
})
it('Floors a number input', function () {
expect(timeToInt(61.9)).to.equal(61)
})
it('Parses the 00h00m00s format', function () {
expect(timeToInt('1h1m1s')).to.equal(3661)
expect(timeToInt('1h')).to.equal(3600)
expect(timeToInt('90s')).to.equal(90)
})
it('Parses the colon-separated format', function () {
expect(timeToInt('20')).to.equal(20)
expect(timeToInt('05:20')).to.equal(320)
expect(timeToInt('01:02:03')).to.equal(3723)
})
it('Returns 0 instead of NaN for more than 3 colon-separated parts', function () {
expect(timeToInt('1:02:03:04')).to.equal(0)
})
it('Returns 0 for an invalid string', function () {
expect(timeToInt('abc')).to.equal(0)
})
})
describe('Seconds to time', function () {
it('Outputs a human readable time', function () {
expect(secondsToTime(61.1335)).to.equals('1m1s')