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

Commit

Permalink
Change the behavior of path.extname
Browse files Browse the repository at this point in the history
Make path.extname return an empty string also if:
- The last dot is not in the last path component
- The last dot starts the last path component
  • Loading branch information
bpl authored and ry committed May 24, 2010
1 parent ab5c0dd commit c4876d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions doc/api.markdown
Expand Up @@ -2611,8 +2611,9 @@ Return the last portion of a path. Similar to the Unix `basename` command. Exa

### path.extname(p)

Return the extension of the path. Everything after the last '.', if there
is no '.' then it returns an empty string. Examples:
Return the extension of the path. Everything after the last '.' in the last portion
of the path. If there is no '.' in the last portion of the path or the only '.' is
the first character, then it returns an empty string. Examples:

path.extname('index.html')
// returns
Expand Down
8 changes: 6 additions & 2 deletions lib/path.js
Expand Up @@ -66,8 +66,12 @@ exports.basename = function (path, ext) {
};

exports.extname = function (path) {
var index = path.lastIndexOf('.');
return index < 0 ? '' : path.substring(index);
var dot = path.lastIndexOf('.'),
slash = path.lastIndexOf('/');
// The last dot must be in the last path component, and it (the last dot) must
// not start the last path component (i.e. be a dot that signifies a hidden
// file in UNIX).
return dot <= slash + 1 ? '' : path.substring(dot);
};

exports.exists = function (path, callback) {
Expand Down
21 changes: 21 additions & 0 deletions test/simple/test-path.js
Expand Up @@ -12,6 +12,27 @@ assert.equal(path.dirname("/a"), "/");
assert.equal(path.dirname("/"), "/");
path.exists(f, function (y) { assert.equal(y, true) });

assert.equal(path.extname(""), "");
assert.equal(path.extname("/path/to/file"), "");
assert.equal(path.extname("/path/to/file.ext"), ".ext");
assert.equal(path.extname("/path.to/file.ext"), ".ext");
assert.equal(path.extname("/path.to/file"), "");
assert.equal(path.extname("/path.to/.file"), "");
assert.equal(path.extname("/path.to/.file.ext"), ".ext");
assert.equal(path.extname("/path/to/f.ext"), ".ext");
assert.equal(path.extname("/path/to/..ext"), ".ext");
assert.equal(path.extname("file"), "");
assert.equal(path.extname("file.ext"), ".ext");
assert.equal(path.extname(".file"), "");
assert.equal(path.extname(".file.ext"), ".ext");
assert.equal(path.extname("/file"), "");
assert.equal(path.extname("/file.ext"), ".ext");
assert.equal(path.extname("/.file"), "");
assert.equal(path.extname("/.file.ext"), ".ext");
assert.equal(path.extname(".path/file.ext"), ".ext");
assert.equal(path.extname("file.ext.ext"), ".ext");
assert.equal(path.extname("file."), ".");

assert.equal(path.join(".", "fixtures/b", "..", "/b/c.js"), "fixtures/b/c.js");

assert.equal(path.normalize("./fixtures///b/../b/c.js"), "fixtures/b/c.js");
Expand Down

0 comments on commit c4876d0

Please sign in to comment.