path-platform
Advanced tools
Comparing version 0.0.1 to 0.11.15
{ | ||
"name": "path-platform", | ||
"version": "0.0.1", | ||
"version": "0.11.15", | ||
"license": "MIT", | ||
"author": "Timothy J Fontaine <tjfontaine@gmail.com> (http://atxconsulting.com)", | ||
"description": "Provide access to win32 and posix path operations", | ||
"description": "Provide access to win32 and posix path operations; sourced directly from upstream Node.js", | ||
"keywords": [ | ||
@@ -7,0 +8,0 @@ "path", |
235
path.js
@@ -23,2 +23,5 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
var isWindows = process.platform === 'win32'; | ||
var util = require('util'); | ||
var _path = require('path'); | ||
@@ -33,37 +36,29 @@ | ||
var isWindows = process.platform === 'win32'; | ||
var util = require('util'); | ||
// resolves . and .. elements in a path array with directory names there | ||
// must be no slashes, empty elements, or device names (c:\) in the array | ||
// must be no slashes or device names (c:\) in the array | ||
// (so also no leading and trailing slashes - it does not distinguish | ||
// relative and absolute paths) | ||
function normalizeArray(parts, allowAboveRoot) { | ||
// if the path tries to go above the root, `up` ends up > 0 | ||
var up = 0; | ||
for (var i = parts.length - 1; i >= 0; i--) { | ||
var last = parts[i]; | ||
if (last === '.') { | ||
parts.splice(i, 1); | ||
} else if (last === '..') { | ||
parts.splice(i, 1); | ||
up++; | ||
} else if (up) { | ||
parts.splice(i, 1); | ||
up--; | ||
} | ||
} | ||
var res = []; | ||
for (var i = 0; i < parts.length; i++) { | ||
var p = parts[i]; | ||
// if the path is allowed to go above the root, restore leading ..s | ||
if (allowAboveRoot) { | ||
for (; up--; up) { | ||
parts.unshift('..'); | ||
// ignore empty parts | ||
if (!p || p === '.') | ||
continue; | ||
if (p === '..') { | ||
if (res.length && res[res.length - 1] !== '..') { | ||
res.pop(); | ||
} else if (allowAboveRoot) { | ||
res.push('..'); | ||
} | ||
} else { | ||
res.push(p); | ||
} | ||
} | ||
return parts; | ||
return res; | ||
} | ||
// Regex to split a windows path into three parts: [*, device, slash, | ||
@@ -78,11 +73,6 @@ // tail] windows-only | ||
var normalizeUNCRoot = function(device) { | ||
return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\'); | ||
}; | ||
var win32 = {}; | ||
// Function to split a filename into [root, dir, basename, ext] | ||
win32.splitPath = function(filename) { | ||
function win32SplitPath(filename) { | ||
// Separate device+slash from tail | ||
@@ -98,5 +88,8 @@ var result = splitDeviceRe.exec(filename), | ||
return [device, dir, basename, ext]; | ||
} | ||
var normalizeUNCRoot = function(device) { | ||
return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\'); | ||
}; | ||
// path.resolve([from ...], to) | ||
@@ -129,3 +122,3 @@ win32.resolve = function() { | ||
// Skip empty and invalid entries | ||
if (typeof path !== 'string') { | ||
if (!util.isString(path)) { | ||
throw new TypeError('Arguments to path.resolve must be strings'); | ||
@@ -173,10 +166,11 @@ } else if (!path) { | ||
// Normalize the tail path | ||
resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/), | ||
!resolvedAbsolute).join('\\'); | ||
function f(p) { | ||
return !!p; | ||
// If device is a drive letter, we'll normalize to lower case. | ||
if (resolvedDevice && resolvedDevice.charAt(1) === ':') { | ||
resolvedDevice = resolvedDevice[0].toLowerCase() + | ||
resolvedDevice.substr(1); | ||
} | ||
resolvedTail = normalizeArray(resolvedTail.split(/[\\\/]+/).filter(f), | ||
!resolvedAbsolute).join('\\'); | ||
return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || | ||
@@ -201,5 +195,3 @@ '.'; | ||
// Normalize the tail path | ||
tail = normalizeArray(tail.split(/[\\\/]+/).filter(function(p) { | ||
return !!p; | ||
}), !isAbsolute).join('\\'); | ||
tail = normalizeArray(tail.split(/[\\\/]+/), !isAbsolute).join('\\'); | ||
@@ -226,3 +218,3 @@ if (!tail && !isAbsolute) { | ||
device = result[1] || '', | ||
isUnc = device && device.charAt(1) !== ':'; | ||
isUnc = !!device && device.charAt(1) !== ':'; | ||
// UNC paths are always absolute | ||
@@ -232,6 +224,5 @@ return !!result[2] || isUnc; | ||
win32.join = function() { | ||
function f(p) { | ||
if (typeof p !== 'string') { | ||
if (!util.isString(p)) { | ||
throw new TypeError('Arguments to path.join must be strings'); | ||
@@ -291,3 +282,3 @@ } | ||
if (start > end) return []; | ||
return arr.slice(start, end - start + 1); | ||
return arr.slice(start, end + 1); | ||
} | ||
@@ -326,3 +317,3 @@ | ||
// Note: this will *probably* throw somewhere. | ||
if (typeof path !== 'string') | ||
if (!util.isString(path)) | ||
return path; | ||
@@ -351,3 +342,3 @@ | ||
win32.dirname = function(path) { | ||
var result = win32.splitPath(path), | ||
var result = win32SplitPath(path), | ||
root = result[0], | ||
@@ -371,3 +362,3 @@ dir = result[1]; | ||
win32.basename = function(path, ext) { | ||
var f = win32.splitPath(path)[2]; | ||
var f = win32SplitPath(path)[2]; | ||
// TODO: make this comparison case-insensitive on windows? | ||
@@ -382,6 +373,56 @@ if (ext && f.substr(-1 * ext.length) === ext) { | ||
win32.extname = function(path) { | ||
return win32.splitPath(path)[3]; | ||
return win32SplitPath(path)[3]; | ||
}; | ||
win32.format = function(pathObject) { | ||
if (!util.isObject(pathObject)) { | ||
throw new TypeError( | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
); | ||
} | ||
var root = pathObject.root || ''; | ||
if (!util.isString(root)) { | ||
throw new TypeError( | ||
"'pathObject.root' must be a string or undefined, not " + | ||
typeof pathObject.root | ||
); | ||
} | ||
var dir = pathObject.dir; | ||
var base = pathObject.base || ''; | ||
if (dir.slice(dir.length - 1, dir.length) === win32.sep) { | ||
return dir + base; | ||
} | ||
if (dir) { | ||
return dir + win32.sep + base; | ||
} | ||
return base; | ||
}; | ||
win32.parse = function(pathString) { | ||
if (!util.isString(pathString)) { | ||
throw new TypeError( | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
); | ||
} | ||
var allParts = win32SplitPath(pathString); | ||
if (!allParts || allParts.length !== 4) { | ||
throw new TypeError("Invalid path '" + pathString + "'"); | ||
} | ||
return { | ||
root: allParts[0], | ||
dir: allParts[0] + allParts[1].slice(0, allParts[1].length - 1), | ||
base: allParts[2], | ||
ext: allParts[3], | ||
name: allParts[2].slice(0, allParts[2].length - allParts[3].length) | ||
}; | ||
}; | ||
win32.sep = '\\'; | ||
@@ -398,5 +439,5 @@ win32.delimiter = ';'; | ||
posix.splitPath = function(filename) { | ||
function posixSplitPath(filename) { | ||
return splitPathRe.exec(filename).slice(1); | ||
}; | ||
} | ||
@@ -414,3 +455,3 @@ | ||
// Skip empty and invalid entries | ||
if (typeof path !== 'string') { | ||
if (!util.isString(path)) { | ||
throw new TypeError('Arguments to path.resolve must be strings'); | ||
@@ -429,5 +470,4 @@ } else if (!path) { | ||
// Normalize the path | ||
resolvedPath = normalizeArray(resolvedPath.split('/').filter(function(p) { | ||
return !!p; | ||
}), !resolvedAbsolute).join('/'); | ||
resolvedPath = normalizeArray(resolvedPath.split('/'), | ||
!resolvedAbsolute).join('/'); | ||
@@ -444,5 +484,3 @@ return ((resolvedAbsolute ? '/' : '') + resolvedPath) || '.'; | ||
// Normalize the path | ||
path = normalizeArray(path.split('/').filter(function(p) { | ||
return !!p; | ||
}), !isAbsolute).join('/'); | ||
path = normalizeArray(path.split('/'), !isAbsolute).join('/'); | ||
@@ -466,9 +504,17 @@ if (!path && !isAbsolute) { | ||
posix.join = function() { | ||
var paths = Array.prototype.slice.call(arguments, 0); | ||
return posix.normalize(paths.filter(function(p, index) { | ||
if (typeof p !== 'string') { | ||
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'); | ||
} | ||
return p; | ||
}).join('/')); | ||
if (segment) { | ||
if (!path) { | ||
path += segment; | ||
} else { | ||
path += '/' + segment; | ||
} | ||
} | ||
} | ||
return posix.normalize(path); | ||
}; | ||
@@ -495,3 +541,3 @@ | ||
if (start > end) return []; | ||
return arr.slice(start, end - start + 1); | ||
return arr.slice(start, end + 1); | ||
} | ||
@@ -528,3 +574,3 @@ | ||
posix.dirname = function(path) { | ||
var result = posix.splitPath(path), | ||
var result = posixSplitPath(path), | ||
root = result[0], | ||
@@ -548,3 +594,3 @@ dir = result[1]; | ||
posix.basename = function(path, ext) { | ||
var f = posix.splitPath(path)[2]; | ||
var f = posixSplitPath(path)[2]; | ||
// TODO: make this comparison case-insensitive on windows? | ||
@@ -559,6 +605,52 @@ if (ext && f.substr(-1 * ext.length) === ext) { | ||
posix.extname = function(path) { | ||
return posix.splitPath(path)[3]; | ||
return posixSplitPath(path)[3]; | ||
}; | ||
posix.format = function(pathObject) { | ||
if (!util.isObject(pathObject)) { | ||
throw new TypeError( | ||
"Parameter 'pathObject' must be an object, not " + typeof pathObject | ||
); | ||
} | ||
var root = pathObject.root || ''; | ||
if (!util.isString(root)) { | ||
throw new TypeError( | ||
"'pathObject.root' must be a string or undefined, not " + | ||
typeof pathObject.root | ||
); | ||
} | ||
var dir = pathObject.dir ? pathObject.dir + posix.sep : ''; | ||
var base = pathObject.base || ''; | ||
return dir + base; | ||
}; | ||
posix.parse = function(pathString) { | ||
if (!util.isString(pathString)) { | ||
throw new TypeError( | ||
"Parameter 'pathString' must be a string, not " + typeof pathString | ||
); | ||
} | ||
var allParts = posixSplitPath(pathString); | ||
if (!allParts || allParts.length !== 4) { | ||
throw new TypeError("Invalid path '" + pathString + "'"); | ||
} | ||
allParts[1] = allParts[1] || ''; | ||
allParts[2] = allParts[2] || ''; | ||
allParts[3] = allParts[3] || ''; | ||
return { | ||
root: allParts[0], | ||
dir: allParts[0] + allParts[1].slice(0, allParts[1].length - 1), | ||
base: allParts[2], | ||
ext: allParts[3], | ||
name: allParts[2].slice(0, allParts[2].length - allParts[3].length) | ||
}; | ||
}; | ||
posix.sep = '/'; | ||
@@ -568,13 +660,8 @@ posix.delimiter = ':'; | ||
var splitPath; | ||
if (isWindows) { | ||
splitPath = win32.splitPath; | ||
if (isWindows) | ||
module.exports = win32; | ||
} else /* posix */ { | ||
splitPath = posix.splitPath; | ||
else /* posix */ | ||
module.exports = posix; | ||
} | ||
module.exports.posix = posix; | ||
module.exports.win32 = win32; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
17984
4
514
80