Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. "use strict";
  2. /**
  3. * A minimal path module to resolve Unix, Windows and URL paths alike.
  4. * @memberof util
  5. * @namespace
  6. */
  7. var path = exports;
  8. var isAbsolute =
  9. /**
  10. * Tests if the specified path is absolute.
  11. * @param {string} path Path to test
  12. * @returns {boolean} `true` if path is absolute
  13. */
  14. path.isAbsolute = function isAbsolute(path) {
  15. return /^(?:\/|\w+:)/.test(path);
  16. };
  17. var normalize =
  18. /**
  19. * Normalizes the specified path.
  20. * @param {string} path Path to normalize
  21. * @returns {string} Normalized path
  22. */
  23. path.normalize = function normalize(path) {
  24. path = path.replace(/\\/g, "/")
  25. .replace(/\/{2,}/g, "/");
  26. var parts = path.split("/"),
  27. absolute = isAbsolute(path),
  28. prefix = "";
  29. if (absolute)
  30. prefix = parts.shift() + "/";
  31. for (var i = 0; i < parts.length;) {
  32. if (parts[i] === "..") {
  33. if (i > 0 && parts[i - 1] !== "..")
  34. parts.splice(--i, 2);
  35. else if (absolute)
  36. parts.splice(i, 1);
  37. else
  38. ++i;
  39. } else if (parts[i] === ".")
  40. parts.splice(i, 1);
  41. else
  42. ++i;
  43. }
  44. return prefix + parts.join("/");
  45. };
  46. /**
  47. * Resolves the specified include path against the specified origin path.
  48. * @param {string} originPath Path to the origin file
  49. * @param {string} includePath Include path relative to origin path
  50. * @param {boolean} [alreadyNormalized=false] `true` if both paths are already known to be normalized
  51. * @returns {string} Path to the include file
  52. */
  53. path.resolve = function resolve(originPath, includePath, alreadyNormalized) {
  54. if (!alreadyNormalized)
  55. includePath = normalize(includePath);
  56. if (isAbsolute(includePath))
  57. return includePath;
  58. if (!alreadyNormalized)
  59. originPath = normalize(originPath);
  60. return (originPath = originPath.replace(/(?:\/|^)[^/]+$/, "")).length ? normalize(originPath + "/" + includePath) : includePath;
  61. };