Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / @protobufjs / path / index.js
1 "use strict";\r
2 \r
3 /**\r
4  * A minimal path module to resolve Unix, Windows and URL paths alike.\r
5  * @memberof util\r
6  * @namespace\r
7  */\r
8 var path = exports;\r
9 \r
10 var isAbsolute =\r
11 /**\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
15  */\r
16 path.isAbsolute = function isAbsolute(path) {\r
17     return /^(?:\/|\w+:)/.test(path);\r
18 };\r
19 \r
20 var normalize =\r
21 /**\r
22  * Normalizes the specified path.\r
23  * @param {string} path Path to normalize\r
24  * @returns {string} Normalized path\r
25  */\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
31         prefix   = "";\r
32     if (absolute)\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
38             else if (absolute)\r
39                 parts.splice(i, 1);\r
40             else\r
41                 ++i;\r
42         } else if (parts[i] === ".")\r
43             parts.splice(i, 1);\r
44         else\r
45             ++i;\r
46     }\r
47     return prefix + parts.join("/");\r
48 };\r
49 \r
50 /**\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
56  */\r
57 path.resolve = function resolve(originPath, includePath, alreadyNormalized) {\r
58     if (!alreadyNormalized)\r
59         includePath = normalize(includePath);\r
60     if (isAbsolute(includePath))\r
61         return includePath;\r
62     if (!alreadyNormalized)\r
63         originPath = normalize(originPath);\r
64     return (originPath = originPath.replace(/(?:\/|^)[^/]+$/, "")).length ? normalize(originPath + "/" + includePath) : includePath;\r
65 };\r