Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
From 7af10cf9eaf25b9fcae04704936aef50db1d2577 Mon Sep 17 00:00:00 2001
From: Bert Belder <bertbelder@gmail.com>
Date: Wed, 18 Jan 2012 15:09:42 +0100
Subject: [PATCH 1/1] Make path.extname do the right thing when the last path
component is . or ..
Closes GH-2526
---
lib/path.js | 4 ++--
test/simple/test-path.js | 28 ++++++++++++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/lib/path.js b/lib/path.js
index b70225b..c10ff7e 100644
--- a/lib/path.js
+++ b/lib/path.js
@@ -61,7 +61,7 @@ if (isWindows) {
/^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/][^\\\/]+)?([\\\/])?([\s\S]*?)$/;
// Regex to split the tail part of the above into [*, dir, basename, ext]
- var splitTailRe = /^([\s\S]+[\\\/](?!$)|[\\\/])?((?:[\s\S]+?)?(\.[^.]*)?)$/;
+ var splitTailRe = /^([\s\S]+[\\\/](?!$)|[\\\/])?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/\\]*)?)$/;
// Function to split a filename into [root, dir, basename, ext]
// windows version
@@ -255,7 +255,7 @@ if (isWindows) {
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
- var splitPathRe = /^(\/?)([\s\S]+\/(?!$)|\/)?((?:[\s\S]+?)?(\.[^.]*)?)$/;
+ var splitPathRe = /^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
var splitPath = function(filename) {
var result = splitPathRe.exec(filename);
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
diff --git a/test/simple/test-path.js b/test/simple/test-path.js
index 1ee78da..92c2647 100644
--- a/test/simple/test-path.js
+++ b/test/simple/test-path.js
@@ -102,6 +102,34 @@ 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.extname('.'), '');
+assert.equal(path.extname('./'), '');
+assert.equal(path.extname('.file.ext'), '.ext');
+assert.equal(path.extname('.file'), '');
+assert.equal(path.extname('.file.'), '.');
+assert.equal(path.extname('.file..'), '.');
+assert.equal(path.extname('..'), '');
+assert.equal(path.extname('../'), '');
+assert.equal(path.extname('..file.ext'), '.ext');
+assert.equal(path.extname('..file'), '.file');
+assert.equal(path.extname('..file.'), '.');
+assert.equal(path.extname('..file..'), '.');
+assert.equal(path.extname('...'), '.');
+assert.equal(path.extname('...ext'), '.ext');
+assert.equal(path.extname('....'), '.');
+assert.equal(path.extname('file.ext/'), '');
+
+if (isWindows) {
+ // On windows, backspace is a path separator.
+ assert.equal(path.extname('.\\'), '');
+ assert.equal(path.extname('..\\'), '');
+ assert.equal(path.extname('file.ext\\'), '');
+} else {
+ // On unix, backspace is a valid name component like any other character.
+ assert.equal(path.extname('.\\'), '');
+ assert.equal(path.extname('..\\'), '.\\');
+ assert.equal(path.extname('file.ext\\'), '.ext\\');
+}
// path.join tests
var failures = [];
--
1.7.7.1.msysgit.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.