Skip to content

Commit

Permalink
fix: clean up duplicated slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrmlnc committed Jan 2, 2022
1 parent 1f80e47 commit 850d74d
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ describe('Package', () => {

assert.deepStrictEqual(actual, expected);
});

it('should clean up patterns', () => {
const expected = [
// Clean up duplicated slashes
tests.task.builder().base('fixtures').positive('fixtures/*').build()
];

const actual = fg.generateTasks(['fixtures//*']);

assert.deepStrictEqual(actual, expected);
});
});

describe('.isDynamicPattern', () => {
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as taskManager from './managers/tasks';
import * as patternManager from './managers/patterns';
import ProviderAsync from './providers/async';
import Provider from './providers/provider';
import ProviderStream from './providers/stream';
Expand Down Expand Up @@ -58,7 +59,7 @@ namespace FastGlob {
export function generateTasks(source: PatternInternal | PatternInternal[], options?: OptionsInternal): Task[] {
assertPatternsInput(source);

const patterns = ([] as PatternInternal[]).concat(source);
const patterns = patternManager.transform(([] as PatternInternal[]).concat(source));
const settings = new Settings(options);

return taskManager.generate(patterns, settings);
Expand All @@ -80,7 +81,7 @@ namespace FastGlob {
}

function getWorks<T>(source: PatternInternal | PatternInternal[], _Provider: new (settings: Settings) => Provider<T>, options?: OptionsInternal): T[] {
const patterns = ([] as PatternInternal[]).concat(source);
const patterns = patternManager.transform(([] as PatternInternal[]).concat(source));
const settings = new Settings(options);

const tasks = taskManager.generate(patterns, settings);
Expand Down
57 changes: 57 additions & 0 deletions src/managers/patterns.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as assert from 'assert';

import * as manager from './patterns';

describe('Managers → Pattern', () => {
describe('.transform', () => {
it('should do not change patterns', () => {
const expected = [
'directory/file.md',
'files{.txt,/file.md}'
];

const actual = manager.transform([
'directory/file.md',
'files{.txt,/file.md}'
]);

assert.deepStrictEqual(actual, expected);
});

it('should do not change the device path in patterns with UNC parts', () => {
const expected = [
'//?/D:/',
'//./D:/',
'//LOCALHOST/d$/',
'//127.0.0.1/d$/',
'//./UNC/LOCALHOST/d$/'
];

const actual = manager.transform([
'//?//D://',
'//.//D:///',
'//LOCALHOST//d$//',
'//127.0.0.1///d$//',
'//./UNC////LOCALHOST///d$//'
]);

assert.deepStrictEqual(actual, expected);
});

it('should remove duplicate slashes in the middle and the of the pattern', () => {
const expected = ['a/b', 'b/c', 'c/d/', '//?/D:/'];

const actual = manager.transform(['a//b', 'b///c', 'c/d///', '//?//D://']);

assert.deepStrictEqual(actual, expected);
});

it('should form double slashes at the beginning of the pattern', () => {
const expected = ['//*', '//?', '//?/D:/'];

const actual = manager.transform(['///*', '////?', '///?/D:/']);

assert.deepStrictEqual(actual, expected);
});
});
});
17 changes: 17 additions & 0 deletions src/managers/patterns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Matches a sequence of two or more consecutive slashes, excluding the first two slashes at the beginning of the string.
* The latter is due to the presence of the device path at the beginning of the UNC path.
*/
const DOUBLE_SLASH_RE = /(?<!^)\/{2,}/g;

export function transform(patterns: string[]): string[] {
return patterns.map((pattern) => removeDuplicatedSlashes(pattern));
}

/**
* This package only works with forward slashes as a path separator.
* Because of this, we cannot use the standard `path.normalize` method, because on Windows platform it will use of backslashes.
*/
function removeDuplicatedSlashes(pattern: string): string {
return pattern.replace(DOUBLE_SLASH_RE, '/');
}

0 comments on commit 850d74d

Please sign in to comment.