summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Doenges <rhdoenges@gmail.com>2013-04-14 12:44:40 -0700
committerisaacs <i@izs.me>2013-04-19 10:15:22 -0700
commit90266750617adb1ce1ca4ce43fe48f9b0fa11343 (patch)
tree16dcbbefbf4b0d9f8fab8a5fcd8fec9873f39d76
parente4406b76dfc024cf5a4051dc73018fc447b3dc7f (diff)
downloadandroid-node-v8-90266750617adb1ce1ca4ce43fe48f9b0fa11343.tar.gz
android-node-v8-90266750617adb1ce1ca4ce43fe48f9b0fa11343.tar.bz2
android-node-v8-90266750617adb1ce1ca4ce43fe48f9b0fa11343.zip
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.
-rw-r--r--doc/api/path.markdown19
-rw-r--r--lib/path.js20
-rw-r--r--test/simple/test-path.js17
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);
@@ -203,6 +203,15 @@ if (isWindows) {
};
// 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') {
@@ -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