Skip to content

Commit 9026675

Browse files
hackedyisaacs
authored andcommitted
path: add path.isAbsolute(path)
An absolute path will always open the same location regardless of your current working directory. For posix, this just means path.charAt(0) === '/', but on Windows it's a little more complicated. Fixes nodejs/node-v0.x-archive#5299.
1 parent e4406b7 commit 9026675

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

doc/api/path.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ Examples:
7878
// if currently in /home/myself/node, it returns
7979
'/home/myself/node/wwwroot/static_files/gif/image.gif'
8080

81+
## path.isAbsolute(path)
82+
83+
Determines whether `path` is an absolute path. An absolute path will always
84+
resolve to the same location, regardless of the working directory.
85+
86+
Posix examples:
87+
88+
path.isAbsolute('/foo/bar') // true
89+
path.isAbsolute('/baz/..') // true
90+
path.isAbsolute('qux/') // false
91+
path.isAbsolute('.') // false
92+
93+
Windows examples:
94+
95+
path.isAbsolute('//server') // true
96+
path.isAbsolute('C:/foo/..') // true
97+
path.isAbsolute('bar\\baz') // false
98+
path.isAbsolute('.') // false
99+
81100
## path.relative(from, to)
82101

83102
Solve the relative path from `from` to `to`.

lib/path.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ if (isWindows) {
121121
var result = splitDeviceRe.exec(path),
122122
device = result[1] || '',
123123
isUnc = device && device.charAt(1) !== ':',
124-
isAbsolute = !!result[2] || isUnc, // UNC paths are always absolute
124+
isAbsolute = exports.isAbsolute(path),
125125
tail = result[3];
126126

127127
if (device &&
@@ -172,7 +172,7 @@ if (isWindows) {
172172
var result = splitDeviceRe.exec(path),
173173
device = result[1] || '',
174174
isUnc = device && device.charAt(1) !== ':',
175-
isAbsolute = !!result[2] || isUnc, // UNC paths are always absolute
175+
isAbsolute = exports.isAbsolute(path),
176176
tail = result[3],
177177
trailingSlash = /[\\\/]$/.test(tail);
178178

@@ -202,6 +202,15 @@ if (isWindows) {
202202
return device + (isAbsolute ? '\\' : '') + tail;
203203
};
204204

205+
// windows version
206+
exports.isAbsolute = function(path) {
207+
var result = splitDeviceRe.exec(path),
208+
device = result[1] || '',
209+
isUnc = device && device.charAt(1) !== ':';
210+
// UNC paths are always absolute
211+
return !!result[2] || isUnc;
212+
};
213+
205214
// windows version
206215
exports.join = function() {
207216
function f(p) {
@@ -338,7 +347,7 @@ if (isWindows) {
338347
// path.normalize(path)
339348
// posix version
340349
exports.normalize = function(path) {
341-
var isAbsolute = path.charAt(0) === '/',
350+
var isAbsolute = exports.isAbsolute(path),
342351
trailingSlash = path.substr(-1) === '/';
343352

344353
// Normalize the path
@@ -356,6 +365,10 @@ if (isWindows) {
356365
return (isAbsolute ? '/' : '') + path;
357366
};
358367

368+
// posix version
369+
exports.isAbsolute = function(path) {
370+
return path.charAt(0) === '/';
371+
};
359372

360373
// posix version
361374
exports.join = function() {
@@ -416,7 +429,6 @@ if (isWindows) {
416429
exports.delimiter = ':';
417430
}
418431

419-
420432
exports.dirname = function(path) {
421433
var result = splitPath(path),
422434
root = result[0],

test/simple/test-path.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,23 @@ resolveTests.forEach(function(test) {
351351
});
352352
assert.equal(failures.length, 0, failures.join(''));
353353

354+
// path.isAbsolute tests
355+
if (isWindows) {
356+
assert.equal(path.isAbsolute('//server/file'), true);
357+
assert.equal(path.isAbsolute('\\\\server\\file'), true);
358+
assert.equal(path.isAbsolute('C:/Users/'), true);
359+
assert.equal(path.isAbsolute('C:\\Users\\'), true);
360+
assert.equal(path.isAbsolute('C:cwd/another'), false);
361+
assert.equal(path.isAbsolute('C:cwd\\another'), false);
362+
assert.equal(path.isAbsolute('directory/directory'), false);
363+
assert.equal(path.isAbsolute('directory\\directory'), false);
364+
} else {
365+
assert.equal(path.isAbsolute('/home/foo'), true);
366+
assert.equal(path.isAbsolute('/home/foo/..'), true);
367+
assert.equal(path.isAbsolute('bar/'), false);
368+
assert.equal(path.isAbsolute('./baz'), false);
369+
}
370+
354371
// path.relative tests
355372
if (isWindows) {
356373
// windows

0 commit comments

Comments
 (0)