rtrim.js 935 B

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = rtrim;
  6. var _assertString = _interopRequireDefault(require("./util/assertString"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function rtrim(str, chars) {
  9. (0, _assertString.default)(str);
  10. if (chars) {
  11. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
  12. var pattern = new RegExp("[".concat(chars.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "]+$"), 'g');
  13. return str.replace(pattern, '');
  14. } // Use a faster and more safe than regex trim method https://blog.stevenlevithan.com/archives/faster-trim-javascript
  15. var strIndex = str.length - 1;
  16. while (/\s/.test(str.charAt(strIndex))) {
  17. strIndex -= 1;
  18. }
  19. return str.slice(0, strIndex + 1);
  20. }
  21. module.exports = exports.default;
  22. module.exports.default = exports.default;