function stringToMilliseconds(
  text,
) {
  const matches = text.match(/(\d+[w|d|h|m|s]{1})/g);
  if (!matches) return;
  let total = 0;
  for (const match of matches) {
    // Finds the first of these letters
    const validMatch = /(w|d|h|m|s)/.exec(match);
    // if none of them were found cancel
    if (!validMatch) return;
    // Get the number which should be before the index of that match
    const number = match.substring(0, validMatch.index);
    // Get the letter that was found
    const [letter] = validMatch;
    if (!number || !letter) return;
    let multiplier = Milliseconds.SECOND;
    switch (letter.toLowerCase()) {
      case `w`:
        multiplier = Milliseconds.WEEK;
        break;
      case `d`:
        multiplier = Milliseconds.DAY;
        break;
      case `h`:
        multiplier = Milliseconds.HOUR;
        break;
      case `m`:
        multiplier = Milliseconds.MINUTE;
        break;
    }
    const amount = number ? parseInt(number, 10) : undefined;
    if (!amount) return;
    total += amount * multiplier;
  }
  return total;
}
/** This function helps convert a string like 1d5h to milliseconds. */
This function helps convert a string like 1d5h to milliseconds.
This function helps convert a string like 1d5h to milliseconds .
function stringToMilliseconds(
  text,
) {
  const matches = text.match(/(\d+[w|d|h|m|s]{1})/g);
  if (!matches) return;
  let total = 0;
  for (const match of matches) {
    const validMatch = /(w|d|h|m|s)/.exec(match);
    if (!validMatch) return;
    const number = match.substring(0, validMatch.index);
    const [letter] = validMatch;
    if (!number || !letter) return;
    let multiplier = Milliseconds.SECOND;
    switch (letter.toLowerCase()) {
      case `w`:
        multiplier = Milliseconds.WEEK;
        break;
      case `d`:
        multiplier = Milliseconds.DAY;
        break;
      case `h`:
        multiplier = Milliseconds.HOUR;
        break;
      case `m`:
        multiplier = Milliseconds.MINUTE;
        break;
    }
    const amount = number ? parseInt(number, 10) : undefined;
    if (!amount) return;
    total += amount * multiplier;
  }
  return total;
}
function stringToMilliseconds ( text , ) { const matches = text . match ( / (\d+[w|d|h|m|s]{1}) / g ) ; if ( ! matches ) return ; let total = 0 ; for ( const match of matches ) { const validMatch = / (w|d|h|m|s) / . exec ( match ) ; if ( ! validMatch ) return ; const number = match . substring ( 0 , validMatch . index ) ; const [ letter ] = validMatch ; if ( ! number || ! letter ) return ; let multiplier = Milliseconds . SECOND ; switch ( letter . toLowerCase ( ) ) { case ` ` : multiplier = Milliseconds . WEEK ; break ; case ` ` : multiplier = Milliseconds . DAY ; break ; case ` ` : multiplier = Milliseconds . HOUR ; break ; case ` ` : multiplier = Milliseconds . MINUTE ; break ; } const amount = number ? parseInt ( number , 10 ) : undefined ; if ( ! amount ) return ; total += amount * multiplier ; } return total ; }
This function helps convert a string like 1d5h to milliseconds.
This function helps convert a string like 1d5h to milliseconds .
// Finds the first of these letters // if none of them were found cancel // Get the number which should be before the index of that match // Get the letter that was found