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