-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (165 loc) · 5.36 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict";
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var SLASH = 47;
var DOT = 46;
var getCWD;
if (typeof process !== "undefined" && typeof process.cwd !== "undefined") {
getCWD = process.cwd;
}
else {
getCWD = function () {
var pathname = window.location.pathname;
return pathname.slice(0, pathname.lastIndexOf("/") + 1);
};
}
/**
* Resolves . and .. elements in a path with directory names
* @param {string} path
* @param {boolean} allowAboveRoot
* @return {string}
*/
function normalizeStringPosix(path, allowAboveRoot) {
var res = '';
var lastSlash = -1;
var dots = 0;
var code = void 0;
var isAboveRoot = false;
for (var i = 0; i <= path.length; ++i) {
if (i < path.length) {
code = path.charCodeAt(i);
}
else if (code === SLASH) {
break;
}
else {
code = SLASH;
}
if (code === SLASH) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
}
else if (lastSlash !== i - 1 && dots === 2) {
if (res.length < 2 || !isAboveRoot ||
res.charCodeAt(res.length - 1) !== DOT ||
res.charCodeAt(res.length - 2) !== DOT) {
if (res.length > 2) {
var start = res.length - 1;
var j = start;
for (; j >= 0; --j) {
if (res.charCodeAt(j) === SLASH) {
break;
}
}
if (j !== start) {
res = (j === -1) ? '' : res.slice(0, j);
lastSlash = i;
dots = 0;
isAboveRoot = false;
continue;
}
}
else if (res.length === 2 || res.length === 1) {
res = '';
lastSlash = i;
dots = 0;
isAboveRoot = false;
continue;
}
}
if (allowAboveRoot) {
if (res.length > 0) {
res += '/..';
}
else {
res = '..';
}
isAboveRoot = true;
}
}
else {
var slice = path.slice(lastSlash + 1, i);
if (res.length > 0) {
res += '/' + slice;
}
else {
res = slice;
}
isAboveRoot = false;
}
lastSlash = i;
dots = 0;
}
else if (code === DOT && dots !== -1) {
++dots;
}
else {
dots = -1;
}
}
return res;
}
/**
* https://nodejs.org/api/path.html#path_path_resolve_paths
* @param {...string} paths A sequence of paths or path segments.
* @return {string}
*/
function resolvePath() {
var paths = [];
for (var _i = 0; _i < arguments.length; _i++) {
paths[_i] = arguments[_i];
}
var resolvedPath = "";
var resolvedAbsolute = false;
var cwd = void 0;
for (var i = paths.length - 1; i >= -1 && !resolvedAbsolute; i--) {
var path = void 0;
if (i >= 0) {
path = paths[i];
}
else {
if (cwd === void 0) {
cwd = getCWD();
}
path = cwd;
}
// Skip empty entries
if (path.length === 0) {
continue;
}
resolvedPath = path + "/" + resolvedPath;
resolvedAbsolute = path.charCodeAt(0) === SLASH;
}
// At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails)
// Normalize the path (removes leading slash)
resolvedPath = normalizeStringPosix(resolvedPath, !resolvedAbsolute);
if (resolvedAbsolute) {
return "/" + resolvedPath;
}
else if (resolvedPath.length > 0) {
return resolvedPath;
}
else {
return '.';
}
}
module.exports = resolvePath;