strip-trailing-slashes.js 577 B

123456789101112131415161718192021222324
  1. // this is the only approach that was significantly faster than using
  2. // str.replace(/\/+$/, '') for strings ending with a lot of / chars and
  3. // containing multiple / chars.
  4. const batchStrings = [
  5. '/'.repeat(1024),
  6. '/'.repeat(512),
  7. '/'.repeat(256),
  8. '/'.repeat(128),
  9. '/'.repeat(64),
  10. '/'.repeat(32),
  11. '/'.repeat(16),
  12. '/'.repeat(8),
  13. '/'.repeat(4),
  14. '/'.repeat(2),
  15. '/',
  16. ]
  17. module.exports = str => {
  18. for (const s of batchStrings) {
  19. while (str.length >= s.length && str.slice(-1 * s.length) === s)
  20. str = str.slice(0, -1 * s.length)
  21. }
  22. return str
  23. }