From 41e53e557992a7d552a8e23de035f9463da25c99 Mon Sep 17 00:00:00 2001 From: Paul Serby Date: Mon, 1 Oct 2012 22:10:32 +0200 Subject: [PATCH] path: add platform specific path delimiter Closes #3728 Closes #4071 --- doc/api/path.markdown | 30 ++++++++++++++++++++++++++---- lib/path.js | 2 ++ test/simple/test-path.js | 17 +++++++++++++---- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/doc/api/path.markdown b/doc/api/path.markdown index d178b53f1fe..2541c62d828 100644 --- a/doc/api/path.markdown +++ b/doc/api/path.markdown @@ -14,7 +14,7 @@ Normalize a string path, taking care of `'..'` and `'.'` parts. When multiple slashes are found, they're replaced by a single one; when the path contains a trailing slash, it is preserved. -On windows backslashes are used. +On Windows backslashes are used. Example: @@ -44,7 +44,7 @@ Resolves `to` to an absolute path. If `to` isn't already absolute `from` arguments are prepended in right to left order, until an absolute path is found. If after using all `from` paths still no absolute path is found, the current working directory is used as well. The -resulting path is normalized, and trailing slashes are removed unless the path +resulting path is normalized, and trailing slashes are removed unless the path gets resolved to the root directory. Non-string arguments are ignored. Another way to think of it is as a sequence of `cd` commands in a shell. @@ -143,14 +143,36 @@ an empty string. Examples: The platform-specific file separator. `'\\'` or `'/'`. -An example on linux: +An example on *nix: 'foo/bar/baz'.split(path.sep) // returns ['foo', 'bar', 'baz'] -An example on windows: +An example on Windows: 'foo\\bar\\baz'.split(path.sep) // returns ['foo', 'bar', 'baz'] + +## path.delimiter + +The platform-specific path delimiter, `;` or `':'`. + +An example on *nix: + + console.log(process.env.PATH) + // '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin' + + process.env.PATH.split(path.delimiter) + // returns + ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin'] + +An example on Windows: + + console.log(process.env.PATH) + // 'C:\Windows\system32;C:\Windows;C:\Program Files\nodejs\' + + process.env.PATH.split(path.delimiter) + // returns + ['C:\Windows\system32', 'C:\Windows', 'C:\Program Files\nodejs\'] diff --git a/lib/path.js b/lib/path.js index 98155de2c25..7e521f20dee 100644 --- a/lib/path.js +++ b/lib/path.js @@ -262,6 +262,7 @@ if (isWindows) { }; exports.sep = '\\'; + exports.delimiter = ';'; } else /* posix */ { @@ -378,6 +379,7 @@ if (isWindows) { }; exports.sep = '/'; + exports.delimiter = ':'; } diff --git a/test/simple/test-path.js b/test/simple/test-path.js index 38a8dab89ca..ca0afc26d55 100644 --- a/test/simple/test-path.js +++ b/test/simple/test-path.js @@ -277,9 +277,18 @@ assert.equal(failures.length, 0, failures.join('')); // path.sep tests if (isWindows) { - // windows - assert.equal(path.sep, '\\'); + // windows + assert.equal(path.sep, '\\'); } else { - // posix - assert.equal(path.sep, '/'); + // posix + assert.equal(path.sep, '/'); +} + +// path.delimiter tests +if (isWindows) { + // windows + assert.equal(path.delimiter, ';'); +} else { + // posix + assert.equal(path.delimiter, ':'); }