Browse Source

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.
v0.7.4-release
isaacs 15 years ago
committed by Ryan Dahl
parent
commit
f0f247d7e5
  1. 2
      lib/module.js
  2. 8
      lib/path.js
  3. 1
      test/simple/test-path.js

2
lib/module.js

@ -197,7 +197,7 @@ function resolveModulePath(request, parent) {
} }
var parentIdPath = path.dirname(parent.id + 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); id = path.join(parentIdPath, request);
// make sure require('./path') and require('path') get distinct ids, even // make sure require('./path') and require('path') get distinct ids, even
// when called from the toplevel js file // when called from the toplevel js file

8
lib/path.js

@ -38,11 +38,9 @@ exports.normalize = function (path, keepBlanks) {
}; };
exports.dirname = function (path) { exports.dirname = function (path) {
// Can't strip trailing slashes since module.js incorrectly thinks if (path.length > 1 && '/' === path[path.length-1]) {
// dirname('/a/b/') should yield '/a/b' instead of '/a'. path = path.replace(/\/+$/, '');
// if (path.length > 1 && '/' === path[path.length-1]) { }
// path = path.replace(/\/+$/, '');
// }
var lastSlash = path.lastIndexOf('/'); var lastSlash = path.lastIndexOf('/');
switch (lastSlash) { switch (lastSlash) {
case -1: case -1:

1
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.basename(f, ".js"), "test-path");
assert.equal(path.extname(f), ".js"); assert.equal(path.extname(f), ".js");
assert.equal(path.dirname(f).substr(-11), "test/simple"); 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/b"), "/a");
assert.equal(path.dirname("/a"), "/"); assert.equal(path.dirname("/a"), "/");
assert.equal(path.dirname("/"), "/"); assert.equal(path.dirname("/"), "/");

Loading…
Cancel
Save