You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
spa_instat_tv/src/features/MatchPage/helpers/getHalfTime.tsx

53 lines
1.1 KiB

import head from 'lodash/head'
import last from 'lodash/last'
import inRange from 'lodash/inRange'
import type { VideoBounds } from 'requests'
export const getHalfTime = (videoBounds: VideoBounds, currentTime: number) => {
const firstBound = head(videoBounds)
const lastBound = last(videoBounds)
const matchSecond = (Number(firstBound?.s) || 0) + currentTime
let period = 1
let second = 1
if (firstBound === lastBound || matchSecond < (Number(videoBounds[1]?.s || 0))) {
return {
period,
second,
}
}
if (lastBound?.e && matchSecond > (Number(lastBound.e))) {
return {}
}
for (let i = 1; i < videoBounds.length; i++) {
const { e, s } = videoBounds[i]
if (inRange(
matchSecond,
Number(s),
Number(e || 1e5) + 1,
)) {
period = i
second = matchSecond - Number(videoBounds[i].s)
break
} else if (
videoBounds[i + 1] && inRange(
matchSecond,
Number(e) + 1,
Number(videoBounds[i + 1].s),
)) {
period = i + 1
break
}
}
return {
period,
second,
}
}