Skip to content

Commit

Permalink
ignore trailing slashes on patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperchupuDev committed Aug 6, 2024
1 parent c8853d1 commit f67f173
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,28 @@ export interface GlobOptions {
onlyFiles?: boolean;
}

// using a directory as entry should match all files inside it
function expandDir(pattern: string) {
if (pattern.endsWith('/')) {
return `${pattern}**`;
function normalizePattern(pattern: string, expandDirectories: boolean) {
let result: string = pattern;
if (pattern.endsWith('/') || pattern.endsWith('\\')) {
result = pattern.slice(0, -1);
}
if (pattern.endsWith('\\')) {
return `${pattern.slice(0, -1)}/**`;
// using a directory as entry should match all files inside it
if (!pattern.endsWith('*') && expandDirectories) {
result += '/**';
}
return `${pattern}/**`;
return result;
}

function processPatterns({ patterns, ignore = [], expandDirectories = true }: GlobOptions) {
const matchPatterns: string[] = [];
const ignorePatterns: string[] = ignore.map(p => (!p.endsWith('*') && expandDirectories ? expandDir(p) : p));
const ignorePatterns: string[] = ignore.map(p => normalizePattern(p, expandDirectories));

if (!patterns) {
return { match: ['**/*'], ignore: ignorePatterns };
}

for (let pattern of patterns) {
// using a directory as entry should match all files inside it
if (!pattern.endsWith('*') && expandDirectories) {
pattern = expandDir(pattern);
}
pattern = normalizePattern(pattern, expandDirectories);
if (pattern.startsWith('!') && pattern[1] !== '(') {
ignorePatterns.push(pattern.slice(1));
} else {
Expand Down

0 comments on commit f67f173

Please sign in to comment.