Skip to content

Commit

Permalink
fix: remove last '.' and './'
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Jul 17, 2023
1 parent f923d5c commit bfbf638
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
12 changes: 12 additions & 0 deletions index.js
Expand Up @@ -47,7 +47,19 @@ module.exports = function globParent(str, opts) {
// replace continuous slashes to single slash
str = str.replace(/\/+/g, '/');

// remove last single dot
if (str.slice(-2) === '/.') {
str = str.slice(0, -1)
}
// remove last './'
while (str.slice(-3) === '/./') {
str = str.slice(0, -2)
}

if (isWin32 && winDriveOrUncVolume) {
if (str === '.' || str === './') {
str = '';
}
str = winDriveOrUncVolume + str;
}

Expand Down
63 changes: 49 additions & 14 deletions test/index.test.js
Expand Up @@ -252,6 +252,41 @@ describe('glob2base test patterns', function () {
gp('/('.repeat(500000) + ')');
done();
});

it('should remove tail \'.\' and \'./\'', function(done) {
expect(gp('foo/./*')).toEqual('foo/');
expect(gp('foo/./././*')).toEqual('foo/');
expect(gp('./././*')).toEqual('./');
expect(gp('/./././*')).toEqual('/');

if (isWin32) {
expect(gp('C:/foo/./*')).toEqual('C:/foo/');
expect(gp('C:/foo/./././*')).toEqual('C:/foo/');
expect(gp('C:/./././*')).toEqual('C:/');

expect(gp('C:\\foo\\.\\*')).toEqual('C:/foo/');
expect(gp('C:\\foo\\.\\.\\.\\*')).toEqual('C:/foo/');
expect(gp('C:\\.\\.\\.\\*')).toEqual('C:/');

expect(gp('C:foo/./*')).toEqual('C:foo/');
expect(gp('C:foo/./././*')).toEqual('C:foo/');
expect(gp('C:./././*')).toEqual('C:');

expect(gp('C:foo\\.\\*')).toEqual('C:foo/');
expect(gp('C:foo\\.\\.\\.\\*')).toEqual('C:foo/');
expect(gp('C:.\\.\\.\\*')).toEqual('C:');

expect(gp('\\\\System07\\C$/foo/./*')).toEqual('\\\\System07\\C$/foo/');
expect(gp('\\\\System07\\C$/foo/./././*')).toEqual('\\\\System07\\C$/foo/');
expect(gp('\\\\System07\\C$/./././*')).toEqual('\\\\System07\\C$/');

expect(gp('\\\\System07\\C$\\foo\\.\\*')).toEqual('\\\\System07\\C$/foo/');
expect(gp('\\\\System07\\C$\\foo\\.\\.\\.\\*')).toEqual('\\\\System07\\C$/foo/');
expect(gp('\\\\System07\\C$\\.\\.\\.\\*')).toEqual('\\\\System07\\C$/');
}

done();
});
});

if (isWin32) {
Expand All @@ -268,15 +303,15 @@ if (isWin32) {
expect(gp('C:/')).toEqual('C:/');
expect(gp('C:/.')).toEqual('C:/');
expect(gp('C:/*')).toEqual('C:/');
expect(gp('C:/./*')).toEqual('C:/.');
expect(gp('C:/./*')).toEqual('C:/');
expect(gp('C://')).toEqual('C:/');
expect(gp('C://*')).toEqual('C:/');
expect(gp('C:/path/*.js')).toEqual('C:/path');

expect(gp('C:\\')).toEqual('C:/');
expect(gp('C:\\.')).toEqual('C:/');
expect(gp('C:\\*')).toEqual('C:/');
expect(gp('C:\\.\\*')).toEqual('C:/.');
expect(gp('C:\\.\\*')).toEqual('C:/');
expect(gp('C:\\\\')).toEqual('C:/');
expect(gp('C:\\\\*')).toEqual('C:/');
expect(gp('C:\\path\\*.js')).toEqual('C:/path');
Expand All @@ -285,17 +320,17 @@ if (isWin32) {
});

it('should return parent dirname from relative path with drive letter', function(done) {
expect(gp('C:')).toEqual('C:.');
expect(gp('C:.')).toEqual('C:.');
expect(gp('C:*')).toEqual('C:.');
expect(gp('C:./*')).toEqual('C:.');
expect(gp('C:.//')).toEqual('C:./');
expect(gp('C:.//*')).toEqual('C:./');
expect(gp('C:')).toEqual('C:');
expect(gp('C:.')).toEqual('C:');
expect(gp('C:*')).toEqual('C:');
expect(gp('C:./*')).toEqual('C:');
expect(gp('C:.//')).toEqual('C:');
expect(gp('C:.//*')).toEqual('C:');
expect(gp('C:path/*.js')).toEqual('C:path');

expect(gp('C:.\\*')).toEqual('C:.');
expect(gp('C:.\\\\')).toEqual('C:./');
expect(gp('C:.\\\\*')).toEqual('C:./');
expect(gp('C:.\\*')).toEqual('C:');
expect(gp('C:.\\\\')).toEqual('C:');
expect(gp('C:.\\\\*')).toEqual('C:');
expect(gp('C:path\\*.js')).toEqual('C:path');

done();
Expand All @@ -305,23 +340,23 @@ if (isWin32) {
expect(gp('\\\\System07\\C$/')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/.')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/*')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/./*')).toEqual('\\\\System07\\C$/.');
expect(gp('\\\\System07\\C$/./*')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$//')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$//*')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/path/*.js')).toEqual('\\\\System07\\C$/path');

expect(gp('\\\\System07\\C$/', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/.', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/./*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/.');
expect(gp('\\\\System07\\C$/./*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$//', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$//*', { flipBackslashes: false })).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$/path/*.js')).toEqual('\\\\System07\\C$/path');

expect(gp('\\\\System07\\C$\\')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$\\.')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$\\*')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$\\.\\*')).toEqual('\\\\System07\\C$/.');
expect(gp('\\\\System07\\C$\\.\\*')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$\\\\')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$\\\\*')).toEqual('\\\\System07\\C$/');
expect(gp('\\\\System07\\C$\\path\\*.js')).toEqual('\\\\System07\\C$/path');
Expand Down

0 comments on commit bfbf638

Please sign in to comment.