rtrim.js 627 B

12345678910111213141516171819
  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. } // Use a faster and more safe than regex trim method https://blog.stevenlevithan.com/archives/faster-trim-javascript
  9. var strIndex = str.length - 1;
  10. while (/\s/.test(str.charAt(strIndex))) {
  11. strIndex -= 1;
  12. }
  13. return str.slice(0, strIndex + 1);
  14. }