4 * A minimal path module to resolve Unix, Windows and URL paths alike.
\r
12 * Tests if the specified path is absolute.
\r
13 * @param {string} path Path to test
\r
14 * @returns {boolean} `true` if path is absolute
\r
16 path.isAbsolute = function isAbsolute(path) {
\r
17 return /^(?:\/|\w+:)/.test(path);
\r
22 * Normalizes the specified path.
\r
23 * @param {string} path Path to normalize
\r
24 * @returns {string} Normalized path
\r
26 path.normalize = function normalize(path) {
\r
27 path = path.replace(/\\/g, "/")
\r
28 .replace(/\/{2,}/g, "/");
\r
29 var parts = path.split("/"),
\r
30 absolute = isAbsolute(path),
\r
33 prefix = parts.shift() + "/";
\r
34 for (var i = 0; i < parts.length;) {
\r
35 if (parts[i] === "..") {
\r
36 if (i > 0 && parts[i - 1] !== "..")
\r
37 parts.splice(--i, 2);
\r
42 } else if (parts[i] === ".")
\r
47 return prefix + parts.join("/");
\r
51 * Resolves the specified include path against the specified origin path.
\r
52 * @param {string} originPath Path to the origin file
\r
53 * @param {string} includePath Include path relative to origin path
\r
54 * @param {boolean} [alreadyNormalized=false] `true` if both paths are already known to be normalized
\r
55 * @returns {string} Path to the include file
\r
57 path.resolve = function resolve(originPath, includePath, alreadyNormalized) {
\r
58 if (!alreadyNormalized)
\r
59 includePath = normalize(includePath);
\r
60 if (isAbsolute(includePath))
\r
62 if (!alreadyNormalized)
\r
63 originPath = normalize(originPath);
\r
64 return (originPath = originPath.replace(/(?:\/|^)[^/]+$/, "")).length ? normalize(originPath + "/" + includePath) : includePath;
\r