src/util/util.js
function resolveURLTemplates(URLTemplates, variables = {}, options = {}) {
  const URLs = [];

  // Encode String variables, when given
  if (variables['ASSETURI']) {
    variables['ASSETURI'] = encodeURIComponentRFC3986(variables['ASSETURI']);
  }
  if (variables['CONTENTPLAYHEAD']) {
    variables['CONTENTPLAYHEAD'] = encodeURIComponentRFC3986(
      variables['CONTENTPLAYHEAD']
    );
  }

  // Set default value for invalid ERRORCODE
  if (
    variables['ERRORCODE'] &&
    !options.isCustomCode &&
    !/^[0-9]{3}$/.test(variables['ERRORCODE'])
  ) {
    variables['ERRORCODE'] = 900;
  }

  // Calc random/time based macros
  variables['CACHEBUSTING'] = leftpad(
    Math.round(Math.random() * 1.0e8).toString()
  );
  variables['TIMESTAMP'] = encodeURIComponentRFC3986(new Date().toISOString());

  // RANDOM/random is not defined in VAST 3/4 as a valid macro tho it's used by some adServer (Auditude)
  variables['RANDOM'] = variables['random'] = variables['CACHEBUSTING'];

  for (const URLTemplateKey in URLTemplates) {
    let resolveURL = URLTemplates[URLTemplateKey];

    if (typeof resolveURL !== 'string') {
      continue;
    }

    for (const key in variables) {
      const value = variables[key];
      const macro1 = `[${key}]`;
      const macro2 = `%%${key}%%`;
      resolveURL = resolveURL.replace(macro1, value);
      resolveURL = resolveURL.replace(macro2, value);
    }
    URLs.push(resolveURL);
  }

  return URLs;
}
function resolveURLTemplates(URLTemplates, variables = {}, options = {}) {
  const URLs = [];

  // Encode String variables, when given
  if (variables['ASSETURI']) {
    variables['ASSETURI'] = encodeURIComponentRFC3986(variables['ASSETURI']);
  }
  if (variables['CONTENTPLAYHEAD']) {
    variables['CONTENTPLAYHEAD'] = encodeURIComponentRFC3986(
      variables['CONTENTPLAYHEAD']
    );
  }

  // Set default value for invalid ERRORCODE
  if (
    variables['ERRORCODE'] &&
    !options.isCustomCode &&
    !/^[0-9]{3}$/.test(variables['ERRORCODE'])
  ) {
    variables['ERRORCODE'] = 900;
  }

  // Calc random/time based macros
  variables['CACHEBUSTING'] = leftpad(
    Math.round(Math.random() * 1.0e8).toString()
  );
  variables['TIMESTAMP'] = encodeURIComponentRFC3986(new Date().toISOString());

  // RANDOM/random is not defined in VAST 3/4 as a valid macro tho it's used by some adServer (Auditude)
  variables['RANDOM'] = variables['random'] = variables['CACHEBUSTING'];

  for (const URLTemplateKey in URLTemplates) {
    let resolveURL = URLTemplates[URLTemplateKey];

    if (typeof resolveURL !== 'string') {
      continue;
    }

    for (const key in variables) {
      const value = variables[key];
      const macro1 = `[${key}]`;
      const macro2 = `%%${key}%%`;
      resolveURL = resolveURL.replace(macro1, value);
      resolveURL = resolveURL.replace(macro2, value);
    }
    URLs.push(resolveURL);
  }

  return URLs;
}
function resolveURLTemplates ( URLTemplates , variables = { } , options = { } ) { const URLs = [ ] ; // Encode String variables, when given if ( variables [ 'ASSETURI' ] ) { variables [ 'ASSETURI' ] = encodeURIComponentRFC3986 ( variables [ 'ASSETURI' ] ) ; } if ( variables [ 'CONTENTPLAYHEAD' ] ) { variables [ 'CONTENTPLAYHEAD' ] = encodeURIComponentRFC3986 ( variables [ 'CONTENTPLAYHEAD' ] ) ; } // Set default value for invalid ERRORCODE if ( variables [ 'ERRORCODE' ] && ! options . isCustomCode && ! / ^[0-9]{3}$ / . test ( variables [ 'ERRORCODE' ] ) ) { variables [ 'ERRORCODE' ] = 900 ; } // Calc random/time based macros variables [ 'CACHEBUSTING' ] = leftpad ( Math . round ( Math . random ( ) * 1.0e8 ) . toString ( ) ) ; variables [ 'TIMESTAMP' ] = encodeURIComponentRFC3986 ( new Date ( ) . toISOString ( ) ) ; // RANDOM/random is not defined in VAST 3/4 as a valid macro tho it's used by some adServer (Auditude) variables [ 'RANDOM' ] = variables [ 'random' ] = variables [ 'CACHEBUSTING' ] ; for ( const URLTemplateKey in URLTemplates ) { let resolveURL = URLTemplates [ URLTemplateKey ] ; if ( typeof resolveURL !== 'string' ) { continue ; } for ( const key in variables ) { const value = variables [ key ] ; const macro1 = ` ${ key } ` ; const macro2 = ` ${ key } ` ; resolveURL = resolveURL . replace ( macro1 , value ) ; resolveURL = resolveURL . replace ( macro2 , value ) ; } URLs . push ( resolveURL ) ; } return URLs ; }
Replace the provided URLTemplates with the given values

@param {Array} URLTemplates - An array of tracking url templates.
@param {Object} [variables={}] - An optional Object of parameters to be used in the tracking calls.
@param {Object} [options={}] - An optional Object of options to be used in the tracking calls.
Replace the provided URLTemplates with the given values