Skip to content

Commit bfbf638

Browse files
committed
fix: remove last '.' and './'
1 parent f923d5c commit bfbf638

File tree

2 files changed

+61
-14
lines changed

2 files changed

+61
-14
lines changed

index.js

+12
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,19 @@ module.exports = function globParent(str, opts) {
4747
// replace continuous slashes to single slash
4848
str = str.replace(/\/+/g, '/');
4949

50+
// remove last single dot
51+
if (str.slice(-2) === '/.') {
52+
str = str.slice(0, -1)
53+
}
54+
// remove last './'
55+
while (str.slice(-3) === '/./') {
56+
str = str.slice(0, -2)
57+
}
58+
5059
if (isWin32 && winDriveOrUncVolume) {
60+
if (str === '.' || str === './') {
61+
str = '';
62+
}
5163
str = winDriveOrUncVolume + str;
5264
}
5365

test/index.test.js

+49-14
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,41 @@ describe('glob2base test patterns', function () {
252252
gp('/('.repeat(500000) + ')');
253253
done();
254254
});
255+
256+
it('should remove tail \'.\' and \'./\'', function(done) {
257+
expect(gp('foo/./*')).toEqual('foo/');
258+
expect(gp('foo/./././*')).toEqual('foo/');
259+
expect(gp('./././*')).toEqual('./');
260+
expect(gp('/./././*')).toEqual('/');
261+
262+
if (isWin32) {
263+
expect(gp('C:/foo/./*')).toEqual('C:/foo/');
264+
expect(gp('C:/foo/./././*')).toEqual('C:/foo/');
265+
expect(gp('C:/./././*')).toEqual('C:/');
266+
267+
expect(gp('C:\\foo\\.\\*')).toEqual('C:/foo/');
268+
expect(gp('C:\\foo\\.\\.\\.\\*')).toEqual('C:/foo/');
269+
expect(gp('C:\\.\\.\\.\\*')).toEqual('C:/');
270+
271+
expect(gp('C:foo/./*')).toEqual('C:foo/');
272+
expect(gp('C:foo/./././*')).toEqual('C:foo/');
273+
expect(gp('C:./././*')).toEqual('C:');
274+
275+
expect(gp('C:foo\\.\\*')).toEqual('C:foo/');
276+
expect(gp('C:foo\\.\\.\\.\\*')).toEqual('C:foo/');
277+
expect(gp('C:.\\.\\.\\*')).toEqual('C:');
278+
279+
expect(gp('\\\\System07\\C$/foo/./*')).toEqual('\\\\System07\\C$/foo/');
280+
expect(gp('\\\\System07\\C$/foo/./././*')).toEqual('\\\\System07\\C$/foo/');
281+
expect(gp('\\\\System07\\C$/./././*')).toEqual('\\\\System07\\C$/');
282+
283+
expect(gp('\\\\System07\\C$\\foo\\.\\*')).toEqual('\\\\System07\\C$/foo/');
284+
expect(gp('\\\\System07\\C$\\foo\\.\\.\\.\\*')).toEqual('\\\\System07\\C$/foo/');
285+
expect(gp('\\\\System07\\C$\\.\\.\\.\\*')).toEqual('\\\\System07\\C$/');
286+
}
287+
288+
done();
289+
});
255290
});
256291

257292
if (isWin32) {
@@ -268,15 +303,15 @@ if (isWin32) {
268303
expect(gp('C:/')).toEqual('C:/');
269304
expect(gp('C:/.')).toEqual('C:/');
270305
expect(gp('C:/*')).toEqual('C:/');
271-
expect(gp('C:/./*')).toEqual('C:/.');
306+
expect(gp('C:/./*')).toEqual('C:/');
272307
expect(gp('C://')).toEqual('C:/');
273308
expect(gp('C://*')).toEqual('C:/');
274309
expect(gp('C:/path/*.js')).toEqual('C:/path');
275310

276311
expect(gp('C:\\')).toEqual('C:/');
277312
expect(gp('C:\\.')).toEqual('C:/');
278313
expect(gp('C:\\*')).toEqual('C:/');
279-
expect(gp('C:\\.\\*')).toEqual('C:/.');
314+
expect(gp('C:\\.\\*')).toEqual('C:/');
280315
expect(gp('C:\\\\')).toEqual('C:/');
281316
expect(gp('C:\\\\*')).toEqual('C:/');
282317
expect(gp('C:\\path\\*.js')).toEqual('C:/path');
@@ -285,17 +320,17 @@ if (isWin32) {
285320
});
286321

287322
it('should return parent dirname from relative path with drive letter', function(done) {
288-
expect(gp('C:')).toEqual('C:.');
289-
expect(gp('C:.')).toEqual('C:.');
290-
expect(gp('C:*')).toEqual('C:.');
291-
expect(gp('C:./*')).toEqual('C:.');
292-
expect(gp('C:.//')).toEqual('C:./');
293-
expect(gp('C:.//*')).toEqual('C:./');
323+
expect(gp('C:')).toEqual('C:');
324+
expect(gp('C:.')).toEqual('C:');
325+
expect(gp('C:*')).toEqual('C:');
326+
expect(gp('C:./*')).toEqual('C:');
327+
expect(gp('C:.//')).toEqual('C:');
328+
expect(gp('C:.//*')).toEqual('C:');
294329
expect(gp('C:path/*.js')).toEqual('C:path');
295330

296-
expect(gp('C:.\\*')).toEqual('C:.');
297-
expect(gp('C:.\\\\')).toEqual('C:./');
298-
expect(gp('C:.\\\\*')).toEqual('C:./');
331+
expect(gp('C:.\\*')).toEqual('C:');
332+
expect(gp('C:.\\\\')).toEqual('C:');
333+
expect(gp('C:.\\\\*')).toEqual('C:');
299334
expect(gp('C:path\\*.js')).toEqual('C:path');
300335

301336
done();
@@ -305,23 +340,23 @@ if (isWin32) {
305340
expect(gp('\\\\System07\\C$/')).toEqual('\\\\System07\\C$/');
306341
expect(gp('\\\\System07\\C$/.')).toEqual('\\\\System07\\C$/');
307342
expect(gp('\\\\System07\\C$/*')).toEqual('\\\\System07\\C$/');
308-
expect(gp('\\\\System07\\C$/./*')).toEqual('\\\\System07\\C$/.');
343+
expect(gp('\\\\System07\\C$/./*')).toEqual('\\\\System07\\C$/');
309344
expect(gp('\\\\System07\\C$//')).toEqual('\\\\System07\\C$/');
310345
expect(gp('\\\\System07\\C$//*')).toEqual('\\\\System07\\C$/');
311346
expect(gp('\\\\System07\\C$/path/*.js')).toEqual('\\\\System07\\C$/path');
312347

313348
expect(gp('\\\\System07\\C$/', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
314349
expect(gp('\\\\System07\\C$/.', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
315350
expect(gp('\\\\System07\\C$/*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
316-
expect(gp('\\\\System07\\C$/./*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/.');
351+
expect(gp('\\\\System07\\C$/./*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
317352
expect(gp('\\\\System07\\C$//', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
318353
expect(gp('\\\\System07\\C$//*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
319354
expect(gp('\\\\System07\\C$/path/*.js')).toEqual('\\\\System07\\C$/path');
320355

321356
expect(gp('\\\\System07\\C$\\')).toEqual('\\\\System07\\C$/');
322357
expect(gp('\\\\System07\\C$\\.')).toEqual('\\\\System07\\C$/');
323358
expect(gp('\\\\System07\\C$\\*')).toEqual('\\\\System07\\C$/');
324-
expect(gp('\\\\System07\\C$\\.\\*')).toEqual('\\\\System07\\C$/.');
359+
expect(gp('\\\\System07\\C$\\.\\*')).toEqual('\\\\System07\\C$/');
325360
expect(gp('\\\\System07\\C$\\\\')).toEqual('\\\\System07\\C$/');
326361
expect(gp('\\\\System07\\C$\\\\*')).toEqual('\\\\System07\\C$/');
327362
expect(gp('\\\\System07\\C$\\path\\*.js')).toEqual('\\\\System07\\C$/path');

0 commit comments

Comments
 (0)