Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Don't return NaN for timeToInt

Chocobozzz186336658256
diff --git a/packages/core-utils/src/common/date.ts b/packages/core-utils/src/common/date.ts
index 5a1c0daa4e..1361346e31 100644
--- a/packages/core-utils/src/common/date.ts
+++ b/packages/core-utils/src/common/date.ts
@@ -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
diff --git a/packages/tests/src/core-utils/date.ts b/packages/tests/src/core-utils/date.ts
index df8f897b44..70755e9827 100644
--- a/packages/tests/src/core-utils/date.ts
+++ b/packages/tests/src/core-utils/date.ts
@@ -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')