Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy J Fontaine <tjfontaine@gmail.com>2013-06-11 04:09:54 +0400
committerChris Dickinson <christopher.s.dickinson@gmail.com>2014-11-21 03:10:55 +0300
commit6a90a060023dac4fc827613243e496237403f29f (patch)
treedce62d98b202a32b35d9b86faaccc3599e2624b4 /lib/path.js
parent4dc8b26bbefb21fd313b0c003cbc9102fb1c2205 (diff)
path: allow calling platform specific methods
Add path.posix and path.win32 which have the specific methods like resolve and normalize so you can specifically normalize or resolve based on the target platform. PR-URL: https://github.com/joyent/node/pull/5661 Reviewed-by: Chris Dickinson <christopher.s.dickinson@gmail.com>
Diffstat (limited to 'lib/path.js')
-rw-r--r--lib/path.js779
1 files changed, 407 insertions, 372 deletions
diff --git a/lib/path.js b/lib/path.js
index 6af430ec478..2cab82acd5e 100644
--- a/lib/path.js
+++ b/lib/path.js
@@ -55,401 +55,464 @@ function normalizeArray(parts, allowAboveRoot) {
}
-if (isWindows) {
- // Regex to split a windows path into three parts: [*, device, slash,
- // tail] windows-only
- var splitDeviceRe =
- /^([a-zA-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 + '\\';
- }
- }
+// Regex to split a windows path into three parts: [*, device, slash,
+// tail] windows-only
+var splitDeviceRe =
+ /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
+
+// Regex to split the tail part of the above into [*, dir, basename, ext]
+var splitTailRe =
+ /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
+
+var win32 = {};
+
+// Function to split a filename into [root, dir, basename, ext]
+win32.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];
+};
- // Skip empty and invalid entries
- if (!util.isString(path)) {
- throw new TypeError('Arguments to path.resolve must be strings');
- } else if (!path) {
- continue;
- }
+var normalizeUNCRoot = function(device) {
+ return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
+};
- 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;
+// path.resolve([from ...], to)
+win32.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 + '\\';
}
+ }
- if (!resolvedDevice) {
- resolvedDevice = device;
- }
- if (!resolvedAbsolute) {
- resolvedTail = tail + '\\' + resolvedTail;
- resolvedAbsolute = isAbsolute;
- }
+ // Skip empty and invalid entries
+ if (!util.isString(path)) {
+ throw new TypeError('Arguments to path.resolve must be strings');
+ } else if (!path) {
+ continue;
+ }
- if (resolvedDevice && resolvedAbsolute) {
- break;
- }
+ var result = splitDeviceRe.exec(path),
+ device = result[1] || '',
+ isUnc = device && device.charAt(1) !== ':',
+ isAbsolute = win32.isAbsolute(path),
+ tail = result[3];
+
+ if (device &&
+ resolvedDevice &&
+ device.toLowerCase() !== resolvedDevice.toLowerCase()) {
+ // This path points to another device so it is not applicable
+ continue;
}
- // 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);
+ if (!resolvedDevice) {
+ resolvedDevice = device;
+ }
+ if (!resolvedAbsolute) {
+ resolvedTail = tail + '\\' + resolvedTail;
+ resolvedAbsolute = isAbsolute;
}
- // 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)
+ if (resolvedDevice && resolvedAbsolute) {
+ break;
+ }
+ }
- // Normalize the tail path
+ // 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);
+ }
- function f(p) {
- return !!p;
- }
+ // 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)
- resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f),
- !resolvedAbsolute).join('\\');
+ // Normalize the tail path
- // If device is a drive letter, we'll normalize to lower case.
- if (resolvedDevice && resolvedDevice.charAt(1) === ':') {
- resolvedDevice = resolvedDevice[0].toLowerCase() +
- resolvedDevice.substr(1);
- }
+ function f(p) {
+ return !!p;
+ }
- return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
- '.';
- };
+ resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f),
+ !resolvedAbsolute).join('\\');
- // 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 (resolvedDevice && resolvedDevice.charAt(1) === ':') {
+ resolvedDevice = resolvedDevice[0].toLowerCase() +
+ resolvedDevice.substr(1);
+ }
- // If device is a drive letter, we'll normalize to lower case.
- if (device && device.charAt(1) === ':') {
- device = device[0].toLowerCase() + device.substr(1);
- }
+ return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
+ '.';
+};
- // Normalize the tail path
- tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {
- return !!p;
- }), !isAbsolute).join('\\');
- if (!tail && !isAbsolute) {
- tail = '.';
- }
- if (tail && trailingSlash) {
- tail += '\\';
- }
+win32.normalize = function(path) {
+ var result = splitDeviceRe.exec(path),
+ device = result[1] || '',
+ isUnc = device && device.charAt(1) !== ':',
+ isAbsolute = win32.isAbsolute(path),
+ tail = result[3],
+ trailingSlash = /[\\\/]$/.test(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);
- }
+ // If device is a drive letter, we'll normalize to lower case.
+ if (device && device.charAt(1) === ':') {
+ device = device[0].toLowerCase() + device.substr(1);
+ }
- return device + (isAbsolute ? '\\' : '') + tail;
- };
+ // Normalize the tail path
+ tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) {
+ return !!p;
+ }), !isAbsolute).join('\\');
- // 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 (!util.isString(p)) {
- throw new TypeError('Arguments to path.join must be strings');
- }
- return p;
- }
+ if (!tail && !isAbsolute) {
+ tail = '.';
+ }
+ if (tail && trailingSlash) {
+ tail += '\\';
+ }
- 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,}/, '\\');
- }
+ // 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 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;
- }
+ return device + (isAbsolute ? '\\' : '') + tail;
+};
- var end = arr.length - 1;
- for (; end >= 0; end--) {
- if (arr[end] !== '') break;
- }
- if (start > end) return [];
- return arr.slice(start, end + 1);
+win32.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;
+};
+
+win32.join = function() {
+ function f(p) {
+ if (!util.isString(p)) {
+ throw new TypeError('Arguments to path.join must be strings');
}
+ return p;
+ }
- var toParts = trim(to.split('\\'));
+ 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,}/, '\\');
+ }
- var lowerFromParts = trim(lowerFrom.split('\\'));
- var lowerToParts = trim(lowerTo.split('\\'));
+ return win32.normalize(joined);
+};
- 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;
- }
+
+// 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'
+win32.relative = function(from, to) {
+ from = win32.resolve(from);
+ to = win32.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;
}
- if (samePartsLength == 0) {
- return to;
+ var end = arr.length - 1;
+ for (; end >= 0; end--) {
+ if (arr[end] !== '') break;
}
- var outputParts = [];
- for (var i = samePartsLength; i < lowerFromParts.length; i++) {
- outputParts.push('..');
+ if (start > end) return [];
+ return arr.slice(start, end + 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;
}
+ }
- outputParts = outputParts.concat(toParts.slice(samePartsLength));
+ if (samePartsLength == 0) {
+ return to;
+ }
- return outputParts.join('\\');
- };
+ var outputParts = [];
+ for (var i = samePartsLength; i < lowerFromParts.length; i++) {
+ outputParts.push('..');
+ }
- exports.sep = '\\';
- exports.delimiter = ';';
+ outputParts = outputParts.concat(toParts.slice(samePartsLength));
-} else /* posix */ {
+ return outputParts.join('\\');
+};
- // 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;
+win32._makeLong = function(path) {
+ // Note: this will *probably* throw somewhere.
+ if (!util.isString(path))
+ return path;
- for (var i = arguments.length - 1; i >= -1 && !resolvedAbsolute; i--) {
- var path = (i >= 0) ? arguments[i] : process.cwd();
+ if (!path) {
+ return '';
+ }
- // Skip empty and invalid entries
- if (!util.isString(path)) {
- throw new TypeError('Arguments to path.resolve must be strings');
- } else if (!path) {
- continue;
- }
+ var resolvedPath = win32.resolve(path);
- resolvedPath = path + '/' + resolvedPath;
- resolvedAbsolute = path.charAt(0) === '/';
- }
+ if (/^[a-zA-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);
+ }
- // 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[path.length - 1] === '/',
- segments = path.split('/'),
- nonEmptySegments = [];
-
- // Normalize the path
- for (var i = 0; i < segments.length; i++) {
- if (segments[i]) {
- nonEmptySegments.push(segments[i]);
- }
- }
- path = normalizeArray(nonEmptySegments, !isAbsolute).join('/');
+ return path;
+};
- 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 path = '';
- for (var i = 0; i < arguments.length; i++) {
- var segment = arguments[i];
- if (!util.isString(segment)) {
- throw new TypeError('Arguments to path.join must be strings');
- }
- if (segment) {
- if (!path) {
- path += segment;
- } else {
- path += '/' + segment;
- }
- }
+win32.dirname = function(path) {
+ var result = win32.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;
+};
+
+
+win32.basename = function(path, ext) {
+ var f = win32.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;
+};
+
+
+win32.extname = function(path) {
+ return win32.splitPath(path)[3];
+};
+
+
+win32.sep = '\\';
+win32.delimiter = ';';
+
+
+// Split a filename into [root, dir, basename, ext], unix version
+// 'root' is just a slash, or nothing.
+var splitPathRe =
+ /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
+var posix = {};
+
+
+posix.splitPath = function(filename) {
+ return splitPathRe.exec(filename).slice(1);
+};
+
+
+// path.resolve([from ...], to)
+// posix version
+posix.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 (!util.isString(path)) {
+ throw new TypeError('Arguments to path.resolve must be strings');
+ } else if (!path) {
+ continue;
}
- return exports.normalize(path);
- };
+ resolvedPath = path + '/' + resolvedPath;
+ resolvedAbsolute = path.charAt(0) === '/';
+ }
- // path.relative(from, to)
- // posix version
- exports.relative = function(from, to) {
- from = exports.resolve(from).substr(1);
- to = exports.resolve(to).substr(1);
+ // 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)
- function trim(arr) {
- var start = 0;
- for (; start < arr.length; start++) {
- if (arr[start] !== '') break;
- }
+ // Normalize the path
+ resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) {
+ return !!p;
+ }), !resolvedAbsolute).join('/');
- var end = arr.length - 1;
- for (; end >= 0; end--) {
- if (arr[end] !== '') break;
- }
+ return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.';
+};
- if (start > end) return [];
- return arr.slice(start, end + 1);
+// path.normalize(path)
+// posix version
+posix.normalize = function(path) {
+ var isAbsolute = posix.isAbsolute(path),
+ trailingSlash = path.substr(-1) === '/',
+ segments = path.split('/'),
+ nonEmptySegments = [];
+
+ // Normalize the path
+ for (var i = 0; i < segments.length; i++) {
+ if (segments[i]) {
+ nonEmptySegments.push(segments[i]);
}
+ }
+ path = normalizeArray(nonEmptySegments, !isAbsolute).join('/');
- var fromParts = trim(from.split('/'));
- var toParts = trim(to.split('/'));
+ if (!path && !isAbsolute) {
+ path = '.';
+ }
+ if (path && trailingSlash) {
+ path += '/';
+ }
+
+ return (isAbsolute ? '/' : '') + path;
+};
+
+// posix version
+posix.isAbsolute = function(path) {
+ return path.charAt(0) === '/';
+};
- 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;
+// posix version
+posix.join = function() {
+ var path = '';
+ for (var i = 0; i < arguments.length; i++) {
+ var segment = arguments[i];
+ if (!util.isString(segment)) {
+ throw new TypeError('Arguments to path.join must be strings');
+ }
+ if (segment) {
+ if (!path) {
+ path += segment;
+ } else {
+ path += '/' + segment;
}
}
+ }
+ return posix.normalize(path);
+};
- var outputParts = [];
- for (var i = samePartsLength; i < fromParts.length; i++) {
- outputParts.push('..');
+
+// path.relative(from, to)
+// posix version
+posix.relative = function(from, to) {
+ from = posix.resolve(from).substr(1);
+ to = posix.resolve(to).substr(1);
+
+ function trim(arr) {
+ var start = 0;
+ for (; start < arr.length; start++) {
+ if (arr[start] !== '') break;
}
- outputParts = outputParts.concat(toParts.slice(samePartsLength));
+ var end = arr.length - 1;
+ for (; end >= 0; end--) {
+ if (arr[end] !== '') break;
+ }
- return outputParts.join('/');
- };
+ if (start > end) return [];
+ return arr.slice(start, end + 1);
+ }
- exports.sep = '/';
- exports.delimiter = ':';
-}
+ var fromParts = trim(from.split('/'));
+ var toParts = trim(to.split('/'));
-exports.dirname = function(path) {
- var result = splitPath(path),
+ 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('/');
+};
+
+
+posix._makeLong = function(path) {
+ return path;
+};
+
+
+posix.dirname = function(path) {
+ var result = posix.splitPath(path),
root = result[0],
dir = result[1];
@@ -467,8 +530,8 @@ exports.dirname = function(path) {
};
-exports.basename = function(path, ext) {
- var f = splitPath(path)[2];
+posix.basename = function(path, ext) {
+ var f = posix.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);
@@ -477,47 +540,19 @@ exports.basename = function(path, ext) {
};
-exports.extname = function(path) {
- return splitPath(path)[3];
+posix.extname = function(path) {
+ return posix.splitPath(path)[3];
};
-exports.exists = util.deprecate(function(path, callback) {
- require('fs').exists(path, callback);
-}, 'path.exists is now called `fs.exists`.');
-
+posix.sep = '/';
+posix.delimiter = ':';
-exports.existsSync = util.deprecate(function(path) {
- return require('fs').existsSync(path);
-}, 'path.existsSync is now called `fs.existsSync`.');
+if (isWindows)
+ module.exports = win32;
+else /* posix */
+ module.exports = posix;
-if (isWindows) {
- exports._makeLong = function(path) {
- // Note: this will *probably* throw somewhere.
- if (!util.isString(path))
- return path;
-
- if (!path) {
- return '';
- }
-
- var resolvedPath = exports.resolve(path);
-
- if (/^[a-zA-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;
- };
-}
+module.exports.posix = posix;
+module.exports.win32 = win32;