Skip to content

Instantly share code, notes, and snippets.

@rentzsch
Created May 19, 2010 15:12
Show Gist options
  • Save rentzsch/406401 to your computer and use it in GitHub Desktop.
Save rentzsch/406401 to your computer and use it in GitHub Desktop.
From 509a940e4c9421bb75aadfdc185f196d4253b62e Mon Sep 17 00:00:00 2001
From: rentzsch <[email protected]>
Date: Sun, 16 May 2010 14:29:29 -0500
Subject: [PATCH] FIX path.dirname('/tmp') => '/'.
Previously path.dirname('/tmp') incorrectly returned '.'.
Unfortunately module.js incorrectly thinks dirname('/a/b/') should
yield '/a/b', so I can't strip trailing slashes yet. Once module.js
is fixed, then the commented-out code should be activated and a test
written for it.
---
lib/path.js | 15 ++++++++++++++-
test/simple/test-path.js | 3 +++
2 files changed, 17 insertions(+), 1 deletions(-)
diff --git a/lib/path.js b/lib/path.js
index c7bfa7e..ce21469 100644
--- a/lib/path.js
+++ b/lib/path.js
@@ -38,7 +38,20 @@ exports.normalize = function (path, keepBlanks) {
};
exports.dirname = function (path) {
- return path && path.substr(0, path.lastIndexOf("/")) || ".";
+ // 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(/\/+$/, '');
+ // }
+ var lastSlash = path.lastIndexOf('/');
+ switch (lastSlash) {
+ case -1:
+ return '.';
+ case 0:
+ return '/';
+ default:
+ return path.substring(0, lastSlash);
+ }
};
exports.filename = function () {
diff --git a/test/simple/test-path.js b/test/simple/test-path.js
index 4e5a703..e64ab7c 100644
--- a/test/simple/test-path.js
+++ b/test/simple/test-path.js
@@ -7,6 +7,9 @@ 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"), "/");
+assert.equal(path.dirname("/"), "/");
path.exists(f, function (y) { assert.equal(y, true) });
assert.equal(path.join(".", "fixtures/b", "..", "/b/c.js"), "fixtures/b/c.js");
--
1.7.0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment