rtrim.js 624 B

123456789101112131415
  1. import assertString from './util/assertString';
  2. export default function rtrim(str, chars) {
  3. assertString(str);
  4. if (chars) {
  5. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
  6. var pattern = new RegExp("[".concat(chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "]+$"), 'g');
  7. return str.replace(pattern, '');
  8. }
  9. // Use a faster and more safe than regex trim method https://blog.stevenlevithan.com/archives/faster-trim-javascript
  10. var strIndex = str.length - 1;
  11. while (/\s/.test(str.charAt(strIndex))) {
  12. strIndex -= 1;
  13. }
  14. return str.slice(0, strIndex + 1);
  15. }