All files / atom/videoPlayer/src/hooks useDetectVideoType.js

100% Statements 25/25
100% Branches 17/17
100% Functions 5/5
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43      1x 32x 134x 4x     130x 130x 130x     32x 32x 134x 134x   134x   59x 47x     20x 19x     88x 16x 39x   105x     32x   32x        
import {UNKNOWN} from '../settings/index.js'
import {DETECTABLE_VIDEO_TYPES, DETECTION_TYPES} from '../settings/players.js'
 
const useDetectVideoType = () => {
  const getVideoExtension = src => {
    if (typeof src !== 'string') {
      return ''
    }
 
    const srcSplittedByPoints = src.split('.')
    const extension = srcSplittedByPoints[srcSplittedByPoints.length - 1]
    return extension
  }
 
  const detectVideoType = src => {
    const type = DETECTABLE_VIDEO_TYPES.find(detectableType => {
      const {DETECTION_TYPE, TYPE_DESCRIPTION} = detectableType
      const extension = getVideoExtension(src)
 
      switch (DETECTION_TYPE) {
        case DETECTION_TYPES.FILE_EXTENSION:
          if (TYPE_DESCRIPTION.FILE_FORMATS.includes(extension)) return true
          break
 
        case DETECTION_TYPES.SRC_INSTANCE_TYPE:
          if (TYPE_DESCRIPTION.INSTANCE_TYPE && src instanceof TYPE_DESCRIPTION.INSTANCE_TYPE) return true
          break
 
        case DETECTION_TYPES.SRC_PATTERN:
          if (TYPE_DESCRIPTION.SRC_PATTERNS.some(pattern => typeof src === 'string' && src.includes(pattern)))
            return true
          break
      }
      return false
    })
 
    return type !== undefined ? type.TYPE_DESCRIPTION : UNKNOWN
  }
  return {detectVideoType}
}
 
export default useDetectVideoType