Skip to content

Commit 80e866b

Browse files
committed
Port test-path-extname.
1 parent c589c0f commit 80e866b

File tree

3 files changed

+106
-6
lines changed

3 files changed

+106
-6
lines changed

index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,22 @@ var posix = {
381381
}
382382
if (code === 46 /*.*/) {
383383
// If this is our first dot, mark it as the start of our extension
384-
if (startDot === -1) startDot = i;else if (preDotState !== 1) preDotState = 1;
385-
} else if (startDot !== -1) {
384+
if (startDot === -1)
385+
startDot = i;
386+
else if (preDotState !== 1)
387+
preDotState = 1;
388+
} else if (startDot !== -1) {
386389
// We saw a non-dot and non-path separator before our dot, so we should
387390
// have a good chance at having a non-empty extension
388391
preDotState = -1;
389392
}
390393
}
391394

392395
if (startDot === -1 || end === -1 ||
393-
// We saw a non-dot character immediately before the dot
394-
preDotState === 0 ||
395-
// The (right-most) trimmed path component is exactly '..'
396-
preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
396+
// We saw a non-dot character immediately before the dot
397+
preDotState === 0 ||
398+
// The (right-most) trimmed path component is exactly '..'
399+
preDotState === 1 && startDot === end - 1 && startDot === startPart + 1) {
397400
return '';
398401
}
399402
return path.slice(startDot, end);

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require('./test-path-basename');
22
require('./test-path-dirname');
3+
require('./test-path-extname');

test/test-path-extname.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
'use strict';
2+
var tape = require('tape');
3+
var path = require('../');
4+
5+
var slashRE = /\//g;
6+
7+
var pairs = [
8+
[__filename, '.js'],
9+
['', ''],
10+
['/path/to/file', ''],
11+
['/path/to/file.ext', '.ext'],
12+
['/path.to/file.ext', '.ext'],
13+
['/path.to/file', ''],
14+
['/path.to/.file', ''],
15+
['/path.to/.file.ext', '.ext'],
16+
['/path/to/f.ext', '.ext'],
17+
['/path/to/..ext', '.ext'],
18+
['/path/to/..', ''],
19+
['file', ''],
20+
['file.ext', '.ext'],
21+
['.file', ''],
22+
['.file.ext', '.ext'],
23+
['/file', ''],
24+
['/file.ext', '.ext'],
25+
['/.file', ''],
26+
['/.file.ext', '.ext'],
27+
['.path/file.ext', '.ext'],
28+
['file.ext.ext', '.ext'],
29+
['file.', '.'],
30+
['.', ''],
31+
['./', ''],
32+
['.file.ext', '.ext'],
33+
['.file', ''],
34+
['.file.', '.'],
35+
['.file..', '.'],
36+
['..', ''],
37+
['../', ''],
38+
['..file.ext', '.ext'],
39+
['..file', '.file'],
40+
['..file.', '.'],
41+
['..file..', '.'],
42+
['...', '.'],
43+
['...ext', '.ext'],
44+
['....', '.'],
45+
['file.ext/', '.ext'],
46+
['file.ext//', '.ext'],
47+
['file/', ''],
48+
['file//', ''],
49+
['file./', '.'],
50+
['file.//', '.'] ];
51+
52+
tape('path.posix.extname', function (t) {
53+
pairs.forEach(function (p) {
54+
var input = p[0];
55+
var expected = p[1];
56+
t.strictEqual(expected, path.posix.extname(input));
57+
});
58+
t.end();
59+
});
60+
61+
tape('path.win32.extname', { skip: true }, function (t) {
62+
pairs.forEach(function (p) {
63+
var input = p[0].replace(slashRE, '\\');
64+
var expected = p[1];
65+
t.strictEqual(expected, path.win32.extname(input));
66+
t.strictEqual(expected, path.win32.extname("C:" + input));
67+
});
68+
t.end();
69+
});
70+
71+
tape('path.win32.extname backslash', { skip: true }, function (t) {
72+
// On Windows, backslash is a path separator.
73+
t.strictEqual(path.win32.extname('.\\'), '');
74+
t.strictEqual(path.win32.extname('..\\'), '');
75+
t.strictEqual(path.win32.extname('file.ext\\'), '.ext');
76+
t.strictEqual(path.win32.extname('file.ext\\\\'), '.ext');
77+
t.strictEqual(path.win32.extname('file\\'), '');
78+
t.strictEqual(path.win32.extname('file\\\\'), '');
79+
t.strictEqual(path.win32.extname('file.\\'), '.');
80+
t.strictEqual(path.win32.extname('file.\\\\'), '.');
81+
t.end();
82+
});
83+
84+
tape('path.posix.extname backslash', function (t) {
85+
// On *nix, backslash is a valid name component like any other character.
86+
t.strictEqual(path.posix.extname('.\\'), '');
87+
t.strictEqual(path.posix.extname('..\\'), '.\\');
88+
t.strictEqual(path.posix.extname('file.ext\\'), '.ext\\');
89+
t.strictEqual(path.posix.extname('file.ext\\\\'), '.ext\\\\');
90+
t.strictEqual(path.posix.extname('file\\'), '');
91+
t.strictEqual(path.posix.extname('file\\\\'), '');
92+
t.strictEqual(path.posix.extname('file.\\'), '.\\');
93+
t.strictEqual(path.posix.extname('file.\\\\'), '.\\\\');
94+
t.end();
95+
});
96+

0 commit comments

Comments
 (0)