lib/parser.js
function parseCue (cue, i) {
  let identifier = '';
  let start = 0;
  let end = 0.01;
  let text = '';
  let styles = '';

  // split and remove empty lines
  const lines = cue.split('\n').filter(Boolean);

  if (lines.length > 0 && lines[0].trim().startsWith('NOTE')) {
    return null;
  }

  if (lines.length === 1 && !lines[0].includes('-->')) {
    throw new ParserError(`Cue identifier cannot be standalone (cue #${i})`);
  }

  if (lines.length > 1 &&
      !(lines[0].includes('-->') || lines[1].includes('-->'))) {
    const msg = `Cue identifier needs to be followed by timestamp (cue #${i})`;
    throw new ParserError(msg);
  }

  if (lines.length > 1 && lines[1].includes('-->')) {
    identifier = lines.shift();
  }

  const times = lines[0].split(' --> ');

  if (times.length !== 2 ||
      !validTimestamp(times[0]) ||
      !validTimestamp(times[1])) {
    throw new ParserError(`Invalid cue timestamp (cue #${i})`);
  }

  start = parseTimestamp(times[0]);
  end = parseTimestamp(times[1]);

  if (start > end) {
    throw new ParserError(`Start timestamp greater than end (cue #${i})`);
  }

  if (end <= start) {
    throw new ParserError(`End must be greater than start (cue #${i})`);
  }

  // TODO better style validation
  styles = times[1].replace(TIMESTAMP_REGEXP, '').trim();

  lines.shift();

  text = lines.join('\n');

  if (!text) {
    return false;
  }

  return { identifier, start, end, text, styles };
}
function parseCue (cue, i) {
  let identifier = '';
  let start = 0;
  let end = 0.01;
  let text = '';
  let styles = '';

  // split and remove empty lines
  const lines = cue.split('\n').filter(Boolean);

  if (lines.length > 0 && lines[0].trim().startsWith('NOTE')) {
    return null;
  }

  if (lines.length === 1 && !lines[0].includes('-->')) {
    throw new ParserError(`Cue identifier cannot be standalone (cue #${i})`);
  }

  if (lines.length > 1 &&
      !(lines[0].includes('-->') || lines[1].includes('-->'))) {
    const msg = `Cue identifier needs to be followed by timestamp (cue #${i})`;
    throw new ParserError(msg);
  }

  if (lines.length > 1 && lines[1].includes('-->')) {
    identifier = lines.shift();
  }

  const times = lines[0].split(' --> ');

  if (times.length !== 2 ||
      !validTimestamp(times[0]) ||
      !validTimestamp(times[1])) {
    throw new ParserError(`Invalid cue timestamp (cue #${i})`);
  }

  start = parseTimestamp(times[0]);
  end = parseTimestamp(times[1]);

  if (start > end) {
    throw new ParserError(`Start timestamp greater than end (cue #${i})`);
  }

  if (end <= start) {
    throw new ParserError(`End must be greater than start (cue #${i})`);
  }

  // TODO better style validation
  styles = times[1].replace(TIMESTAMP_REGEXP, '').trim();

  lines.shift();

  text = lines.join('\n');

  if (!text) {
    return false;
  }

  return { identifier, start, end, text, styles };
}
function parseCue ( cue , i ) { let identifier = '' ; let start = 0 ; let end = 0.01 ; let text = '' ; let styles = '' ; // split and remove empty lines const lines = cue . split ( '\n' ) . filter ( Boolean ) ; if ( lines . length > 0 && lines [ 0 ] . trim ( ) . startsWith ( 'NOTE' ) ) { return null ; } if ( lines . length === 1 && ! lines [ 0 ] . includes ( '-->' ) ) { throw new ParserError ( ` ${ i } ` ) ; } if ( lines . length > 1 && ! ( lines [ 0 ] . includes ( '-->' ) || lines [ 1 ] . includes ( '-->' ) ) ) { const msg = ` ${ i } ` ; throw new ParserError ( msg ) ; } if ( lines . length > 1 && lines [ 1 ] . includes ( '-->' ) ) { identifier = lines . shift ( ) ; } const times = lines [ 0 ] . split ( ' --> ' ) ; if ( times . length !== 2 || ! validTimestamp ( times [ 0 ] ) || ! validTimestamp ( times [ 1 ] ) ) { throw new ParserError ( ` ${ i } ` ) ; } start = parseTimestamp ( times [ 0 ] ) ; end = parseTimestamp ( times [ 1 ] ) ; if ( start > end ) { throw new ParserError ( ` ${ i } ` ) ; } if ( end <= start ) { throw new ParserError ( ` ${ i } ` ) ; } // TODO better style validation styles = times [ 1 ] . replace ( TIMESTAMP_REGEXP , '' ) . trim ( ) ; lines . shift ( ) ; text = lines . join ( '\n' ) ; if ( ! text ) { return false ; } return { identifier , start , end , text , styles } ; }
Parse a single cue block.

@param {array} cue Array of content for the cue
@param {number} i Index of cue in array

@returns {object} cue Cue object with start, end, text and styles.
Null if it's a note
Parse a single cue block .