rtrim.js 781 B

123456789101112131415161718192021222324252627282930313233
  1. var toString = require('../lang/toString');
  2. var WHITE_SPACES = require('./WHITE_SPACES');
  3. /**
  4. * Remove chars from end of string.
  5. */
  6. function rtrim(str, chars) {
  7. str = toString(str);
  8. chars = chars || WHITE_SPACES;
  9. var end = str.length - 1,
  10. charLen = chars.length,
  11. found = true,
  12. i, c;
  13. while (found && end >= 0) {
  14. found = false;
  15. i = -1;
  16. c = str.charAt(end);
  17. while (++i < charLen) {
  18. if (c === chars[i]) {
  19. found = true;
  20. end--;
  21. break;
  22. }
  23. }
  24. }
  25. return (end >= 0) ? str.substring(0, end + 1) : '';
  26. }
  27. module.exports = rtrim;