All files trackNode.ts

100% Statements 21/21
81.81% Branches 9/11
100% Functions 3/3
100% Lines 20/20

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    1x 13x 13x 13x 13x 13x 31x 31x 31x 28x 28x 28x 28x   3x       13x 8x 8x                   13x 13x 13x  
import type { Element } from 'hast';
 
export const trackNode = (query: Record<string, string> = {}): { query: Record<string, string>, element: Element[] } => {
  const resultTrack: Record<string, Record<string, string>> = {}
  const resultElement: Element[] = []
  const resultQuery: Record<string, string> = {}
  const result = { query: resultQuery, element: resultElement }
  Object.keys(query).forEach((key) => {
    let keyMatch = key.trim().match(/track\[['"](\w+:?\w+)['"]\]/i)
    let queryKey = keyMatch ? keyMatch[1] : null
    if (queryKey) {
      let [lang, keyString] = queryKey.split(':')
      if (!resultTrack[lang]) resultTrack[lang] = {}
      const value = query[key]
      resultTrack[lang][keyString ?? "src"] = value
    } else {
      resultQuery[key] = query[key]
    }
  });
 
  Object.keys(resultTrack).forEach((lang) => {
    const track = resultTrack[lang]
    resultElement.push({
      type: 'element',
      tagName: 'track',
      properties: {
        kind: track.kind || 'subtitles',
        ...track
      },
      children: []
    })
  })
  result.query = resultQuery
  result.element = resultElement
  return { ...result }
}