getPaths.js 971 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. module.exports = function getPaths(path) {
  7. if (path === "/") return { paths: ["/"], segments: [""] };
  8. const parts = path.split(/(.*?[\\/]+)/);
  9. const paths = [path];
  10. const segments = [parts[parts.length - 1]];
  11. let part = parts[parts.length - 1];
  12. path = path.substr(0, path.length - part.length - 1);
  13. for (let i = parts.length - 2; i > 2; i -= 2) {
  14. paths.push(path);
  15. part = parts[i];
  16. path = path.substr(0, path.length - part.length) || "/";
  17. segments.push(part.substr(0, part.length - 1));
  18. }
  19. part = parts[1];
  20. segments.push(part);
  21. paths.push(part);
  22. return {
  23. paths: paths,
  24. segments: segments
  25. };
  26. };
  27. module.exports.basename = function basename(path) {
  28. const i = path.lastIndexOf("/"),
  29. j = path.lastIndexOf("\\");
  30. const p = i < 0 ? j : j < 0 ? i : i < j ? j : i;
  31. if (p < 0) return null;
  32. const s = path.substr(p + 1);
  33. return s;
  34. };