Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix dirname so that dirname('/a/b/') -> '/a', like sh's does.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
isaacs authored and ry committed Jul 23, 2010
1 parent e0d6f14 commit f0f247d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/module.js
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions lib/path.js
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions test/simple/test-path.js
Expand Up @@ -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("/"), "/");
Expand Down

0 comments on commit f0f247d

Please sign in to comment.