|
|
@ -1,8 +1,8 @@ |
|
|
|
|
|
|
|
function validPathPart (p, keepBlanks) { |
|
|
|
return typeof p === "string" && (p || keepBlanks); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
exports.join = function () { |
|
|
|
var args = Array.prototype.slice.call(arguments); |
|
|
|
// edge case flag to switch into url-resolve-mode
|
|
|
@ -14,32 +14,42 @@ exports.join = function () { |
|
|
|
var joined = exports.normalizeArray(args, keepBlanks).join("/"); |
|
|
|
return joined; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.split = function (path, keepBlanks) { |
|
|
|
// split based on /, but only if that / is not at the start or end.
|
|
|
|
return exports.normalizeArray(path.split(/^|\/(?!$)/), keepBlanks); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
function cleanArray (parts, keepBlanks) { |
|
|
|
var i = 0; |
|
|
|
var l = parts.length - 1; |
|
|
|
var stripped = false; |
|
|
|
|
|
|
|
// strip leading empty args
|
|
|
|
while (i < l && !validPathPart(parts[i], keepBlanks)) { |
|
|
|
stripped = true; |
|
|
|
i ++; |
|
|
|
} |
|
|
|
|
|
|
|
// strip tailing empty args
|
|
|
|
while (l >= i && !validPathPart(parts[l], keepBlanks)) { |
|
|
|
stripped = true; |
|
|
|
l --; |
|
|
|
} |
|
|
|
|
|
|
|
if (stripped) { |
|
|
|
// if l chopped all the way back to i, then this is empty
|
|
|
|
parts = Array.prototype.slice.call(parts, i, l + 1); |
|
|
|
} |
|
|
|
|
|
|
|
return parts.filter(function (p) { return validPathPart(p, keepBlanks) }) |
|
|
|
.join('/') |
|
|
|
.split(/^|\/(?!$)/); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
exports.normalizeArray = function (original, keepBlanks) { |
|
|
|
var parts = cleanArray(original, keepBlanks); |
|
|
|
if (!parts.length || (parts.length === 1 && !parts[0])) return ["."]; |
|
|
@ -48,6 +58,7 @@ exports.normalizeArray = function (original, keepBlanks) { |
|
|
|
// leading/trailing invalids have been stripped off.
|
|
|
|
// if it comes in starting with a slash, or ending with a slash,
|
|
|
|
var leadingSlash = (parts[0].charAt(0) === "/"); |
|
|
|
|
|
|
|
if (leadingSlash) parts[0] = parts[0].substr(1); |
|
|
|
var last = parts.slice(-1)[0]; |
|
|
|
var tailingSlash = (last.substr(-1) === "/"); |
|
|
@ -100,10 +111,12 @@ exports.normalizeArray = function (original, keepBlanks) { |
|
|
|
return directories; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.normalize = function (path, keepBlanks) { |
|
|
|
return exports.join(path, keepBlanks || false); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.dirname = function (path) { |
|
|
|
if (path.length > 1 && '/' === path[path.length-1]) { |
|
|
|
path = path.replace(/\/+$/, ''); |
|
|
@ -119,6 +132,7 @@ exports.dirname = function (path) { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.basename = function (path, ext) { |
|
|
|
var f = path.substr(path.lastIndexOf("/") + 1); |
|
|
|
if (ext && f.substr(-1 * ext.length) === ext) { |
|
|
@ -127,6 +141,7 @@ exports.basename = function (path, ext) { |
|
|
|
return f; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.extname = function (path) { |
|
|
|
var dot = path.lastIndexOf('.'), |
|
|
|
slash = path.lastIndexOf('/'); |
|
|
@ -136,12 +151,14 @@ exports.extname = function (path) { |
|
|
|
return dot <= slash + 1 ? '' : path.substring(dot); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.exists = function (path, callback) { |
|
|
|
process.binding('fs').stat(path, function (err, stats) { |
|
|
|
if (callback) callback(err ? false : true); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
exports.existsSync = function (path) { |
|
|
|
try { |
|
|
|
process.binding('fs').stat(path); |
|
|
|