function rtrim(str, c, invert) {
    var l = str.length;

    if (l === 0) {
      return '';
    } // Length of suffix matching the invert condition.


    var suffLen = 0; // Step left until we fail to match the invert condition.

    while (suffLen < l) {
      var currChar = str.charAt(l - suffLen - 1);

      if (currChar === c && !invert) {
        suffLen++;
      } else if (currChar !== c && invert) {
        suffLen++;
      } else {
        break;
      }
    }

    return str.slice(0, l - suffLen);
  }
Remove trailing 'c's. Equivalent to str.replace(/c*$/, '').
/c*$/ is vulnerable to REDOS.

@param {string} str
@param {string} c
@param {boolean} invert Remove suffix of non-c chars instead. Default falsey.