Improve path module slightly:
1. Provide a switch to tell it to not remove empty path parts when normalizing. 2. Correct the handling of some edge cases when you have lots of dots and empty bits, such as paths like "././/./..//." and so on.
This commit is contained in:
parent
fd184ee2fa
commit
7342fecaec
26
src/node.js
26
src/node.js
@ -670,28 +670,36 @@ var pathModule = createInternalModule("path", function (exports) {
|
||||
return exports.normalize(Array.prototype.join.call(arguments, "/"));
|
||||
};
|
||||
|
||||
exports.normalizeArray = function (parts) {
|
||||
var directories = [];
|
||||
for (var i = 0; i < parts.length; i++) {
|
||||
exports.normalizeArray = function (parts, keepBlanks) {
|
||||
var directories = [], prev;
|
||||
for (var i = 0, l = parts.length - 1; i <= l; i++) {
|
||||
var directory = parts[i];
|
||||
if (directory === "." || (directory === "" && directories.length)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// if it's blank, but it's not the first thing, and not the last thing, skip it.
|
||||
if (directory === "" && i !== 0 && i !== l && !keepBlanks) continue;
|
||||
|
||||
// if it's a dot, and there was some previous dir already, then skip it.
|
||||
if (directory === "." && prev) continue;
|
||||
|
||||
if (
|
||||
directory === ".."
|
||||
&& directories.length
|
||||
&& directories[directories.length - 1] != '..'
|
||||
&& prev != '..'
|
||||
&& prev != ''
|
||||
) {
|
||||
directories.pop();
|
||||
prev = directories.slice(-1)[0]
|
||||
} else {
|
||||
if (prev === ".") directories.pop();
|
||||
directories.push(directory);
|
||||
prev = directory;
|
||||
}
|
||||
}
|
||||
return directories;
|
||||
};
|
||||
|
||||
exports.normalize = function (path) {
|
||||
return exports.normalizeArray(path.split("/")).join("/");
|
||||
exports.normalize = function (path, keepBlanks) {
|
||||
return exports.normalizeArray(path.split("/"), keepBlanks).join("/");
|
||||
};
|
||||
|
||||
exports.dirname = function (path) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user