From 90266750617adb1ce1ca4ce43fe48f9b0fa11343 Mon Sep 17 00:00:00 2001 From: Ryan Doenges Date: Sun, 14 Apr 2013 12:44:40 -0700 Subject: [PATCH] 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 joyent/node#5299. --- doc/api/path.markdown | 19 +++++++++++++++++++ lib/path.js | 20 ++++++++++++++++---- test/simple/test-path.js | 17 +++++++++++++++++ 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/doc/api/path.markdown b/doc/api/path.markdown index fe59134d4f..348c7cdae6 100644 --- a/doc/api/path.markdown +++ b/doc/api/path.markdown @@ -78,6 +78,25 @@ Examples: // if currently in /home/myself/node, it returns '/home/myself/node/wwwroot/static_files/gif/image.gif' +## path.isAbsolute(path) + +Determines whether `path` is an absolute path. An absolute path will always +resolve to the same location, regardless of the working directory. + +Posix examples: + + path.isAbsolute('/foo/bar') // true + path.isAbsolute('/baz/..') // true + path.isAbsolute('qux/') // false + path.isAbsolute('.') // false + +Windows examples: + + path.isAbsolute('//server') // true + path.isAbsolute('C:/foo/..') // true + path.isAbsolute('bar\\baz') // false + path.isAbsolute('.') // false + ## path.relative(from, to) Solve the relative path from `from` to `to`. diff --git a/lib/path.js b/lib/path.js index db0cdea2dd..5cef72fac7 100644 --- a/lib/path.js +++ b/lib/path.js @@ -121,7 +121,7 @@ if (isWindows) { var result = splitDeviceRe.exec(path), device = result[1] || '', isUnc = device && device.charAt(1) !== ':', - isAbsolute = !!result[2] || isUnc, // UNC paths are always absolute + isAbsolute = exports.isAbsolute(path), tail = result[3]; if (device && @@ -172,7 +172,7 @@ if (isWindows) { var result = splitDeviceRe.exec(path), device = result[1] || '', isUnc = device && device.charAt(1) !== ':', - isAbsolute = !!result[2] || isUnc, // UNC paths are always absolute + isAbsolute = exports.isAbsolute(path), tail = result[3], trailingSlash = /[\\\/]$/.test(tail); @@ -202,6 +202,15 @@ if (isWindows) { 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) { @@ -338,7 +347,7 @@ if (isWindows) { // path.normalize(path) // posix version exports.normalize = function(path) { - var isAbsolute = path.charAt(0) === '/', + var isAbsolute = exports.isAbsolute(path), trailingSlash = path.substr(-1) === '/'; // Normalize the path @@ -356,6 +365,10 @@ if (isWindows) { return (isAbsolute ? '/' : '') + path; }; + // posix version + exports.isAbsolute = function(path) { + return path.charAt(0) === '/'; + }; // posix version exports.join = function() { @@ -416,7 +429,6 @@ if (isWindows) { exports.delimiter = ':'; } - exports.dirname = function(path) { var result = splitPath(path), root = result[0], diff --git a/test/simple/test-path.js b/test/simple/test-path.js index da076d494d..cdeebcd069 100644 --- a/test/simple/test-path.js +++ b/test/simple/test-path.js @@ -351,6 +351,23 @@ resolveTests.forEach(function(test) { }); assert.equal(failures.length, 0, failures.join('')); +// path.isAbsolute tests +if (isWindows) { + assert.equal(path.isAbsolute('//server/file'), true); + assert.equal(path.isAbsolute('\\\\server\\file'), true); + assert.equal(path.isAbsolute('C:/Users/'), true); + assert.equal(path.isAbsolute('C:\\Users\\'), true); + assert.equal(path.isAbsolute('C:cwd/another'), false); + assert.equal(path.isAbsolute('C:cwd\\another'), false); + assert.equal(path.isAbsolute('directory/directory'), false); + assert.equal(path.isAbsolute('directory\\directory'), false); +} else { + assert.equal(path.isAbsolute('/home/foo'), true); + assert.equal(path.isAbsolute('/home/foo/..'), true); + assert.equal(path.isAbsolute('bar/'), false); + assert.equal(path.isAbsolute('./baz'), false); +} + // path.relative tests if (isWindows) { // windows