1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | 'use strict';
|
24 | Object.defineProperty(exports, "__esModule", { value: true });
|
25 | exports.isEqualOrParent = exports.getRoot = exports.normalize = exports.extname = exports.basename = exports.nativeSep = exports.sep = void 0;
|
26 | const os_1 = require("./os");
|
27 | const strings_1 = require("./strings");
|
28 |
|
29 |
|
30 |
|
31 | exports.sep = '/';
|
32 |
|
33 |
|
34 |
|
35 | exports.nativeSep = os_1.isWindows ? '\\' : '/';
|
36 | const _posixBadPath = /(\/\.\.?\/)|(\/\.\.?)$|^(\.\.?\/)|(\/\/+)|(\\)/;
|
37 | const _winBadPath = /(\\\.\.?\\)|(\\\.\.?)$|^(\.\.?\\)|(\\\\+)|(\/)/;
|
38 | function _isNormal(path, win) {
|
39 | return win
|
40 | ? !_winBadPath.test(path)
|
41 | : !_posixBadPath.test(path);
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | function basename(path) {
|
47 | const idx = ~path.lastIndexOf('/') || ~path.lastIndexOf('\\');
|
48 | if (idx === 0) {
|
49 | return path;
|
50 | }
|
51 | else if (~idx === path.length - 1) {
|
52 | return basename(path.substring(0, path.length - 1));
|
53 | }
|
54 | else {
|
55 | return path.substr(~idx + 1);
|
56 | }
|
57 | }
|
58 | exports.basename = basename;
|
59 |
|
60 |
|
61 |
|
62 | function extname(path) {
|
63 | path = basename(path);
|
64 | const idx = ~path.lastIndexOf('.');
|
65 | return idx ? path.substring(~idx) : '';
|
66 | }
|
67 | exports.extname = extname;
|
68 | function normalize(path, toOSPath) {
|
69 | if (path === null || path === void 0) {
|
70 | return path;
|
71 | }
|
72 | const len = path.length;
|
73 | if (len === 0) {
|
74 | return '.';
|
75 | }
|
76 | const wantsBackslash = os_1.isWindows && toOSPath;
|
77 | if (_isNormal(path, wantsBackslash)) {
|
78 | return path;
|
79 | }
|
80 |
|
81 | const sep = wantsBackslash ? '\\' : '/';
|
82 | const root = getRoot(path, sep);
|
83 |
|
84 | let start = root.length;
|
85 | let skip = false;
|
86 | let res = '';
|
87 | for (let end = root.length; end <= len; end++) {
|
88 |
|
89 | if (end === len || path.charCodeAt(end) === 47 || path.charCodeAt(end) === 92 ) {
|
90 | if (streql(path, start, end, '..')) {
|
91 |
|
92 | const prev_start = res.lastIndexOf(sep);
|
93 | const prev_part = res.slice(prev_start + 1);
|
94 | if ((root || prev_part.length > 0) && prev_part !== '..') {
|
95 | res = prev_start === -1 ? '' : res.slice(0, prev_start);
|
96 | skip = true;
|
97 | }
|
98 | }
|
99 | else if (streql(path, start, end, '.') && (root || res || end < len - 1)) {
|
100 |
|
101 | skip = true;
|
102 | }
|
103 | if (!skip) {
|
104 | const part = path.slice(start, end);
|
105 | if (res !== '' && res[res.length - 1] !== sep) {
|
106 | res += sep;
|
107 | }
|
108 | res += part;
|
109 | }
|
110 | start = end + 1;
|
111 | skip = false;
|
112 | }
|
113 | }
|
114 | return root + res;
|
115 | }
|
116 | exports.normalize = normalize;
|
117 | function streql(value, start, end, other) {
|
118 | return start + other.length === end && value.indexOf(other, start) === start;
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | function getRoot(path, sep = '/') {
|
127 | if (!path) {
|
128 | return '';
|
129 | }
|
130 | const len = path.length;
|
131 | let code = path.charCodeAt(0);
|
132 | if (code === 47 || code === 92 ) {
|
133 | code = path.charCodeAt(1);
|
134 | if (code === 47 || code === 92 ) {
|
135 |
|
136 |
|
137 | code = path.charCodeAt(2);
|
138 | if (code !== 47 && code !== 92 ) {
|
139 |
|
140 | let pos = 3;
|
141 | const start = pos;
|
142 | for (; pos < len; pos++) {
|
143 | code = path.charCodeAt(pos);
|
144 | if (code === 47 || code === 92 ) {
|
145 | break;
|
146 | }
|
147 | }
|
148 | code = path.charCodeAt(pos + 1);
|
149 | if (start !== pos && code !== 47 && code !== 92 ) {
|
150 | pos += 1;
|
151 | for (; pos < len; pos++) {
|
152 | code = path.charCodeAt(pos);
|
153 | if (code === 47 || code === 92 ) {
|
154 | return path.slice(0, pos + 1)
|
155 | .replace(/[\\/]/g, sep);
|
156 | }
|
157 | }
|
158 | }
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 | return sep;
|
164 | }
|
165 | else if ((code >= 65 && code <= 90 ) || (code >= 97 && code <= 122 )) {
|
166 |
|
167 | if (path.charCodeAt(1) === 58 ) {
|
168 | code = path.charCodeAt(2);
|
169 | if (code === 47 || code === 92 ) {
|
170 |
|
171 |
|
172 | return path.slice(0, 2) + sep;
|
173 | }
|
174 | else {
|
175 |
|
176 |
|
177 | return path.slice(0, 2);
|
178 | }
|
179 | }
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 | let pos = path.indexOf('://');
|
185 | if (pos !== -1) {
|
186 | pos += 3;
|
187 | for (; pos < len; pos++) {
|
188 | code = path.charCodeAt(pos);
|
189 | if (code === 47 || code === 92 ) {
|
190 | return path.slice(0, pos + 1);
|
191 | }
|
192 | }
|
193 | }
|
194 | return '';
|
195 | }
|
196 | exports.getRoot = getRoot;
|
197 | function isEqualOrParent(path, candidate, ignoreCase) {
|
198 | if (path === candidate) {
|
199 | return true;
|
200 | }
|
201 | if (!path || !candidate) {
|
202 | return false;
|
203 | }
|
204 | if (candidate.length > path.length) {
|
205 | return false;
|
206 | }
|
207 | if (ignoreCase) {
|
208 | const beginsWith = (0, strings_1.startsWithIgnoreCase)(path, candidate);
|
209 | if (!beginsWith) {
|
210 | return false;
|
211 | }
|
212 | if (candidate.length === path.length) {
|
213 | return true;
|
214 | }
|
215 | let sepOffset = candidate.length;
|
216 | if (candidate.charAt(candidate.length - 1) === exports.nativeSep) {
|
217 | sepOffset--;
|
218 | }
|
219 | return path.charAt(sepOffset) === exports.nativeSep;
|
220 | }
|
221 | if (candidate.charAt(candidate.length - 1) !== exports.nativeSep) {
|
222 | candidate += exports.nativeSep;
|
223 | }
|
224 | return path.indexOf(candidate) === 0;
|
225 | }
|
226 | exports.isEqualOrParent = isEqualOrParent;
|
227 |
|
\ | No newline at end of file |