FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/lib/path.js at master · CombatCode/node · GitHub
CombatCode
/
node
Public
forked from
nodejs/node-v0.x-archive
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
lib
/
path.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
504 lines (412 loc) · 14.4 KB
Breadcrumbs
node
/
lib
/
path.js
Copy path
File metadata and controls
504 lines (412 loc) · 14.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
// 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
isWindows
=
process
.
platform
===
'win32'
;
var
util
=
require
(
'util'
)
;
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function
normalizeArray
(
parts
,
allowAboveRoot
)
{
// if the path tries to go above the root, `up` ends up > 0
var
up
=
0
;
for
(
var
i
=
parts
.
length
-
1
;
i
>=
0
;
i
--
)
{
var
last
=
parts
[
i
]
;
if
(
last
===
'.'
)
{
parts
.
splice
(
i
,
1
)
;
}
else
if
(
last
===
'..'
)
{
parts
.
splice
(
i
,
1
)
;
up
++
;
}
else
if
(
up
)
{
parts
.
splice
(
i
,
1
)
;
up
--
;
}
}
// if the path is allowed to go above the root, restore leading ..s
if
(
allowAboveRoot
)
{
for
(
;
up
--
;
up
)
{
parts
.
unshift
(
'..'
)
;
}
}
return
parts
;
}
if
(
isWindows
)
{
// Regex to split a windows path into three parts: [*, device, slash,
// tail] windows-only
var
splitDeviceRe
=
/
^
(
[
a
-
z
A
-
Z
]
:
|
[
\\
\/
]
{
2
}
[
^
\\
\/
]
+
[
\\
\/
]
+
[
^
\\
\/
]
+
)
?
(
[
\\
\/
]
)
?
(
[
\s
\S
]
*
?
)
$
/
;
// Regex to split the tail part of the above into [*, dir, basename, ext]
var
splitTailRe
=
/
^
(
[
\s
\S
]
*
?
)
(
(?:
\.
{
1
,
2
}
|
[
^
\\
\/
]
+
?
|
)
(
\.
[
^
.
\/
\\
]
*
|
)
)
(?:
[
\\
\/
]
*
)
$
/
;
// Function to split a filename into [root, dir, basename, ext]
// windows version
var
splitPath
=
function
(
filename
)
{
// Separate device+slash from tail
var
result
=
splitDeviceRe
.
exec
(
filename
)
,
device
=
(
result
[
1
]
||
''
)
+
(
result
[
2
]
||
''
)
,
tail
=
result
[
3
]
||
''
;
// Split the tail into dir, basename and extension
var
result2
=
splitTailRe
.
exec
(
tail
)
,
dir
=
result2
[
1
]
,
basename
=
result2
[
2
]
,
ext
=
result2
[
3
]
;
return
[
device
,
dir
,
basename
,
ext
]
;
}
;
var
normalizeUNCRoot
=
function
(
device
)
{
return
'\\\\'
+
device
.
replace
(
/
^
[
\\
\/
]
+
/
,
''
)
.
replace
(
/
[
\\
\/
]
+
/
g
,
'\\'
)
;
}
;
// path.resolve([from ...], to)
// windows version
exports
.
resolve
=
function
(
)
{
var
resolvedDevice
=
''
,
resolvedTail
=
''
,
resolvedAbsolute
=
false
;
for
(
var
i
=
arguments
.
length
-
1
;
i
>=
-
1
;
i
--
)
{
var
path
;
if
(
i
>=
0
)
{
path
=
arguments
[
i
]
;
}
else
if
(
!
resolvedDevice
)
{
path
=
process
.
cwd
(
)
;
}
else
{
// Windows has the concept of drive-specific current working
// directories. If we've resolved a drive letter but not yet an
// absolute path, get cwd for that drive. We're sure the device is not
// an unc path at this points, because unc paths are always absolute.
path
=
process
.
env
[
'='
+
resolvedDevice
]
;
// Verify that a drive-local cwd was found and that it actually points
// to our drive. If not, default to the drive's root.
if
(
!
path
||
path
.
substr
(
0
,
3
)
.
toLowerCase
(
)
!==
resolvedDevice
.
toLowerCase
(
)
+
'\\'
)
{
path
=
resolvedDevice
+
'\\'
;
}
}
// Skip empty and invalid entries
if
(
typeof
path
!==
'string'
)
{
throw
new
TypeError
(
'Arguments to path.resolve must be strings'
)
;
}
else
if
(
!
path
)
{
continue
;
}
var
result
=
splitDeviceRe
.
exec
(
path
)
,
device
=
result
[
1
]
||
''
,
isUnc
=
device
&&
device
.
charAt
(
1
)
!==
':'
,
isAbsolute
=
exports
.
isAbsolute
(
path
)
,
tail
=
result
[
3
]
;
if
(
device
&&
resolvedDevice
&&
device
.
toLowerCase
(
)
!==
resolvedDevice
.
toLowerCase
(
)
)
{
// This path points to another device so it is not applicable
continue
;
}
if
(
!
resolvedDevice
)
{
resolvedDevice
=
device
;
}
if
(
!
resolvedAbsolute
)
{
resolvedTail
=
tail
+
'\\'
+
resolvedTail
;
resolvedAbsolute
=
isAbsolute
;
}
if
(
resolvedDevice
&&
resolvedAbsolute
)
{
break
;
}
}
// Convert slashes to backslashes when `resolvedDevice` points to an UNC
// root. Also squash multiple slashes into a single one where appropriate.
if
(
isUnc
)
{
resolvedDevice
=
normalizeUNCRoot
(
resolvedDevice
)
;
}
// 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 tail path
function
f
(
p
)
{
return
!
!
p
;
}
resolvedTail
=
normalizeArray
(
resolvedTail
.
split
(
/
[
\\
\/
]
+
/
)
.
filter
(
f
)
,
!
resolvedAbsolute
)
.
join
(
'\\'
)
;
return
(
resolvedDevice
+
(
resolvedAbsolute
?
'\\'
:
''
)
+
resolvedTail
)
||
'.'
;
}
;
// windows version
exports
.
normalize
=
function
(
path
)
{
var
result
=
splitDeviceRe
.
exec
(
path
)
,
device
=
result
[
1
]
||
''
,
isUnc
=
device
&&
device
.
charAt
(
1
)
!==
':'
,
isAbsolute
=
exports
.
isAbsolute
(
path
)
,
tail
=
result
[
3
]
,
trailingSlash
=
/
[
\\
\/
]
$
/
.
test
(
tail
)
;
// If device is a drive letter, we'll normalize to lower case.
if
(
device
&&
device
.
charAt
(
1
)
===
':'
)
{
device
=
device
[
0
]
.
toLowerCase
(
)
+
device
.
substr
(
1
)
;
}
// Normalize the tail path
tail
=
normalizeArray
(
tail
.
split
(
/
[
\\
\/
]
+
/
)
.
filter
(
function
(
p
)
{
return
!
!
p
;
}
)
,
!
isAbsolute
)
.
join
(
'\\'
)
;
if
(
!
tail
&&
!
isAbsolute
)
{
tail
=
'.'
;
}
if
(
tail
&&
trailingSlash
)
{
tail
+=
'\\'
;
}
// Convert slashes to backslashes when `device` points to an UNC root.
// Also squash multiple slashes into a single one where appropriate.
if
(
isUnc
)
{
device
=
normalizeUNCRoot
(
device
)
;
}
return
device
+
(
isAbsolute
?
'\\'
:
''
)
+
tail
;
}
;
// windows version
exports
.
isAbsolute
=
function
(
path
)
{
var
result
=
splitDeviceRe
.
exec
(
path
)
,
device
=
result
[
1
]
||
''
,
isUnc
=
device
&&
device
.
charAt
(
1
)
!==
':'
;
// UNC paths are always absolute
return
!
!
result
[
2
]
||
isUnc
;
}
;
// windows version
exports
.
join
=
function
(
)
{
function
f
(
p
)
{
if
(
typeof
p
!==
'string'
)
{
throw
new
TypeError
(
'Arguments to path.join must be strings'
)
;
}
return
p
;
}
var
paths
=
Array
.
prototype
.
filter
.
call
(
arguments
,
f
)
;
var
joined
=
paths
.
join
(
'\\'
)
;
// Make sure that the joined path doesn't start with two slashes, because
// normalize() will mistake it for an UNC path then.
//
// This step is skipped when it is very clear that the user actually
// intended to point at an UNC path. This is assumed when the first
// non-empty string arguments starts with exactly two slashes followed by
// at least one more non-slash character.
//
// Note that for normalize() to treat a path as an UNC path it needs to
// have at least 2 components, so we don't filter for that here.
// This means that the user can use join to construct UNC paths from
// a server name and a share name; for example:
// path.join('//server', 'share') -> '\\\\server\\share\')
if
(
!
/
^
[
\\
\/
]
{
2
}
[
^
\\
\/
]
/
.
test
(
paths
[
0
]
)
)
{
joined
=
joined
.
replace
(
/
^
[
\\
\/
]
{
2
,
}
/
,
'\\'
)
;
}
return
exports
.
normalize
(
joined
)
;
}
;
// path.relative(from, to)
// it will solve the relative path from 'from' to 'to', for instance:
// from = 'C:\\orandea\\test\\aaa'
// to = 'C:\\orandea\\impl\\bbb'
// The output of the function should be: '..\\..\\impl\\bbb'
// windows version
exports
.
relative
=
function
(
from
,
to
)
{
from
=
exports
.
resolve
(
from
)
;
to
=
exports
.
resolve
(
to
)
;
// windows is not case sensitive
var
lowerFrom
=
from
.
toLowerCase
(
)
;
var
lowerTo
=
to
.
toLowerCase
(
)
;
function
trim
(
arr
)
{
var
start
=
0
;
for
(
;
start
<
arr
.
length
;
start
++
)
{
if
(
arr
[
start
]
!==
''
)
break
;
}
var
end
=
arr
.
length
-
1
;
for
(
;
end
>=
0
;
end
--
)
{
if
(
arr
[
end
]
!==
''
)
break
;
}
if
(
start
>
end
)
return
[
]
;
return
arr
.
slice
(
start
,
end
-
start
+
1
)
;
}
var
toParts
=
trim
(
to
.
split
(
'\\'
)
)
;
var
lowerFromParts
=
trim
(
lowerFrom
.
split
(
'\\'
)
)
;
var
lowerToParts
=
trim
(
lowerTo
.
split
(
'\\'
)
)
;
var
length
=
Math
.
min
(
lowerFromParts
.
length
,
lowerToParts
.
length
)
;
var
samePartsLength
=
length
;
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
if
(
lowerFromParts
[
i
]
!==
lowerToParts
[
i
]
)
{
samePartsLength
=
i
;
break
;
}
}
if
(
samePartsLength
==
0
)
{
return
to
;
}
var
outputParts
=
[
]
;
for
(
var
i
=
samePartsLength
;
i
<
lowerFromParts
.
length
;
i
++
)
{
outputParts
.
push
(
'..'
)
;
}
outputParts
=
outputParts
.
concat
(
toParts
.
slice
(
samePartsLength
)
)
;
return
outputParts
.
join
(
'\\'
)
;
}
;
exports
.
sep
=
'\\'
;
exports
.
delimiter
=
';'
;
}
else
/* posix */
{
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
var
splitPathRe
=
/
^
(
\/
?
|
)
(
[
\s
\S
]
*
?
)
(
(?:
\.
{
1
,
2
}
|
[
^
\/
]
+
?
|
)
(
\.
[
^
.
\/
]
*
|
)
)
(?:
[
\/
]
*
)
$
/
;
var
splitPath
=
function
(
filename
)
{
return
splitPathRe
.
exec
(
filename
)
.
slice
(
1
)
;
}
;
// path.resolve([from ...], to)
// posix version
exports
.
resolve
=
function
(
)
{
var
resolvedPath
=
''
,
resolvedAbsolute
=
false
;
for
(
var
i
=
arguments
.
length
-
1
;
i
>=
-
1
&&
!
resolvedAbsolute
;
i
--
)
{
var
path
=
(
i
>=
0
)
?
arguments
[
i
]
:
process
.
cwd
(
)
;
// Skip empty and invalid entries
if
(
typeof
path
!==
'string'
)
{
throw
new
TypeError
(
'Arguments to path.resolve must be strings'
)
;
}
else
if
(
!
path
)
{
continue
;
}
resolvedPath
=
path
+
'/'
+
resolvedPath
;
resolvedAbsolute
=
path
.
charAt
(
0
)
===
'/'
;
}
// 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
resolvedPath
=
normalizeArray
(
resolvedPath
.
split
(
'/'
)
.
filter
(
function
(
p
)
{
return
!
!
p
;
}
)
,
!
resolvedAbsolute
)
.
join
(
'/'
)
;
return
(
(
resolvedAbsolute
?
'/'
:
''
)
+
resolvedPath
)
||
'.'
;
}
;
// path.normalize(path)
// posix version
exports
.
normalize
=
function
(
path
)
{
var
isAbsolute
=
exports
.
isAbsolute
(
path
)
,
trailingSlash
=
path
.
substr
(
-
1
)
===
'/'
;
// Normalize the path
path
=
normalizeArray
(
path
.
split
(
'/'
)
.
filter
(
function
(
p
)
{
return
!
!
p
;
}
)
,
!
isAbsolute
)
.
join
(
'/'
)
;
if
(
!
path
&&
!
isAbsolute
)
{
path
=
'.'
;
}
if
(
path
&&
trailingSlash
)
{
path
+=
'/'
;
}
return
(
isAbsolute
?
'/'
:
''
)
+
path
;
}
;
// posix version
exports
.
isAbsolute
=
function
(
path
)
{
return
path
.
charAt
(
0
)
===
'/'
;
}
;
// posix version
exports
.
join
=
function
(
)
{
var
paths
=
Array
.
prototype
.
slice
.
call
(
arguments
,
0
)
;
return
exports
.
normalize
(
paths
.
filter
(
function
(
p
,
index
)
{
if
(
typeof
p
!==
'string'
)
{
throw
new
TypeError
(
'Arguments to path.join must be strings'
)
;
}
return
p
;
}
)
.
join
(
'/'
)
)
;
}
;
// path.relative(from, to)
// posix version
exports
.
relative
=
function
(
from
,
to
)
{
from
=
exports
.
resolve
(
from
)
.
substr
(
1
)
;
to
=
exports
.
resolve
(
to
)
.
substr
(
1
)
;
function
trim
(
arr
)
{
var
start
=
0
;
for
(
;
start
<
arr
.
length
;
start
++
)
{
if
(
arr
[
start
]
!==
''
)
break
;
}
var
end
=
arr
.
length
-
1
;
for
(
;
end
>=
0
;
end
--
)
{
if
(
arr
[
end
]
!==
''
)
break
;
}
if
(
start
>
end
)
return
[
]
;
return
arr
.
slice
(
start
,
end
-
start
+
1
)
;
}
var
fromParts
=
trim
(
from
.
split
(
'/'
)
)
;
var
toParts
=
trim
(
to
.
split
(
'/'
)
)
;
var
length
=
Math
.
min
(
fromParts
.
length
,
toParts
.
length
)
;
var
samePartsLength
=
length
;
for
(
var
i
=
0
;
i
<
length
;
i
++
)
{
if
(
fromParts
[
i
]
!==
toParts
[
i
]
)
{
samePartsLength
=
i
;
break
;
}
}
var
outputParts
=
[
]
;
for
(
var
i
=
samePartsLength
;
i
<
fromParts
.
length
;
i
++
)
{
outputParts
.
push
(
'..'
)
;
}
outputParts
=
outputParts
.
concat
(
toParts
.
slice
(
samePartsLength
)
)
;
return
outputParts
.
join
(
'/'
)
;
}
;
exports
.
sep
=
'/'
;
exports
.
delimiter
=
':'
;
}
exports
.
dirname
=
function
(
path
)
{
var
result
=
splitPath
(
path
)
,
root
=
result
[
0
]
,
dir
=
result
[
1
]
;
if
(
!
root
&&
!
dir
)
{
// No dirname whatsoever
return
'.'
;
}
if
(
dir
)
{
// It has a dirname, strip trailing slash
dir
=
dir
.
substr
(
0
,
dir
.
length
-
1
)
;
}
return
root
+
dir
;
}
;
exports
.
basename
=
function
(
path
,
ext
)
{
var
f
=
splitPath
(
path
)
[
2
]
;
// TODO: make this comparison case-insensitive on windows?
if
(
ext
&&
f
.
substr
(
-
1
*
ext
.
length
)
===
ext
)
{
f
=
f
.
substr
(
0
,
f
.
length
-
ext
.
length
)
;
}
return
f
;
}
;
exports
.
extname
=
function
(
path
)
{
return
splitPath
(
path
)
[
3
]
;
}
;
exports
.
exists
=
util
.
deprecate
(
function
(
path
,
callback
)
{
require
(
'fs'
)
.
exists
(
path
,
callback
)
;
}
,
'path.exists is now called `fs.exists`.'
)
;
exports
.
existsSync
=
util
.
deprecate
(
function
(
path
)
{
return
require
(
'fs'
)
.
existsSync
(
path
)
;
}
,
'path.existsSync is now called `fs.existsSync`.'
)
;
if
(
isWindows
)
{
exports
.
_makeLong
=
function
(
path
)
{
// Note: this will *probably* throw somewhere.
if
(
typeof
path
!==
'string'
)
return
path
;
if
(
!
path
)
{
return
''
;
}
var
resolvedPath
=
exports
.
resolve
(
path
)
;
if
(
/
^
[
a
-
z
A
-
Z
]
\:
\\
/
.
test
(
resolvedPath
)
)
{
// path is local filesystem path, which needs to be converted
// to long UNC path.
return
'\\\\?\\'
+
resolvedPath
;
}
else
if
(
/
^
\\
\\
[
^
?
.
]
/
.
test
(
resolvedPath
)
)
{
// path is network UNC path, which needs to be converted
// to long UNC path.
return
'\\\\?\\UNC\\'
+
resolvedPath
.
substring
(
2
)
;
}
return
path
;
}
;
}
else
{
exports
.
_makeLong
=
function
(
path
)
{
return
path
;
}
;
}
Back
|
FazBrowse Home
|
New Git URL