From f0f247d7e5bd9368fd4cb8e46dbd3882465086ff Mon Sep 17 00:00:00 2001 From: isaacs Date: Fri, 23 Jul 2010 00:41:28 -0700 Subject: [PATCH] Fix dirname so that dirname('/a/b/') -> '/a', like sh's does. Before there was this comment: Can't strip trailing slashes since module.js incorrectly thinks dirname('/a/b/') should yield '/a/b' instead of '/a'. But now, such thinking is corrected. --- lib/module.js | 2 +- lib/path.js | 8 +++----- test/simple/test-path.js | 1 + 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/module.js b/lib/module.js index 0211197042..8e07bd73b0 100644 --- a/lib/module.js +++ b/lib/module.js @@ -197,7 +197,7 @@ function resolveModulePath(request, parent) { } var parentIdPath = path.dirname(parent.id + - (path.basename(parent.filename).match(new RegExp('^index\\.(' + exts.join('|') + ')$')) ? "/" : "")); + (path.basename(parent.filename).match(new RegExp('^index\\.(' + exts.join('|') + ')$')) ? "/." : "")); id = path.join(parentIdPath, request); // make sure require('./path') and require('path') get distinct ids, even // when called from the toplevel js file diff --git a/lib/path.js b/lib/path.js index 0316f96614..60c7850d45 100644 --- a/lib/path.js +++ b/lib/path.js @@ -38,11 +38,9 @@ exports.normalize = function (path, keepBlanks) { }; exports.dirname = function (path) { - // Can't strip trailing slashes since module.js incorrectly thinks - // dirname('/a/b/') should yield '/a/b' instead of '/a'. - // if (path.length > 1 && '/' === path[path.length-1]) { - // path = path.replace(/\/+$/, ''); - // } + if (path.length > 1 && '/' === path[path.length-1]) { + path = path.replace(/\/+$/, ''); + } var lastSlash = path.lastIndexOf('/'); switch (lastSlash) { case -1: diff --git a/test/simple/test-path.js b/test/simple/test-path.js index a2c07a8d8d..7b16f9627a 100644 --- a/test/simple/test-path.js +++ b/test/simple/test-path.js @@ -8,6 +8,7 @@ assert.equal(path.basename(f), "test-path.js"); assert.equal(path.basename(f, ".js"), "test-path"); assert.equal(path.extname(f), ".js"); assert.equal(path.dirname(f).substr(-11), "test/simple"); +assert.equal(path.dirname("/a/b/"), "/a"); assert.equal(path.dirname("/a/b"), "/a"); assert.equal(path.dirname("/a"), "/"); assert.equal(path.dirname("/"), "/");