From 7342fecaec20e9dc385ce1763c0f443b4bed360c Mon Sep 17 00:00:00 2001 From: isaacs Date: Sun, 3 Jan 2010 23:24:25 -0800 Subject: [PATCH] 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. --- src/node.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/node.js b/src/node.js index c6c3a6a39f..e9a1e77b93 100644 --- a/src/node.js +++ b/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) {