text: /**
   * Module dependencies.
   */
  
  const parseTimestamps = require('./parseTimestamps')
  
  /**
   * Parse a SRT or WebVTT string.
   * @param {String} srtOrVtt
   * @return {Array} subtitles
   */
  
  module.exports = (srtOrVtt) => {
    if (!srtOrVtt) return []
  
    const source = srtOrVtt
      .trim()
      .concat('\n')
      .replace(/\r\n/g, '\n')
      .replace(/\n{3,}/g, '\n\n')
      .replace(/^WEBVTT.*\n{2}/, '')
      .split('\n')
  
    return source.reduce((captions, row, index) => {
      const caption = captions[captions.length - 1]
  
      if (!caption.index) {
        if (/^\d+$/.test(row)) {
          caption.index = parseInt(row, 10)
          return captions
        }
      }
  
      if (!caption.hasOwnProperty('start')) {
        Object.assign(caption, parseTimestamps(row))
        return captions
      }
  
      if (row === '') {
        delete caption.index
        if (index !== source.length - 1) {
          captions.push({})
        }
      } else {
        caption.text = caption.text
          ? caption.text + '\n' + row
          : row
      }
  
      return captions
    }, [{}])
  }
  