Skip to content

Commit

Permalink
fix(matcher): * should match at least one character
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed May 22, 2020
1 parent a56a820 commit 5a42b66
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/Matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ describe('Matcher Class', () => {

it('returns false', () => {
expect(new Matcher('secrets:123').match('secrets:124')).toBe(false);
expect(new Matcher('secrets:123:*').match('secrets:123')).toBe(false);
expect(new Matcher('secrets:123:*').match('secrets:124:something')).toBe(
false
);
expect(
new Matcher('secrets:*:something').match('secrets:123:other')
).toBe(false);
expect(
new Matcher('secrets:*:something').match('secrets::something')
).toBe(false);
});
});
});
31 changes: 7 additions & 24 deletions src/Matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ export class Matcher {
private readonly pattern: string;
private readonly set: (string | RegExp)[];
private readonly empty: boolean;
private hasSpecialCharacter: boolean;

constructor(pattern: string) {
this.set = [];
this.pattern = pattern.trim();
this.empty = !this.pattern ? true : false;
this.hasSpecialCharacter = false;

const set = this.braceExpand();
this.set = set.map(val => this.parse(val));
Expand Down Expand Up @@ -41,34 +39,19 @@ export class Matcher {
if (pattern.length > 1024 * 64) {
throw new TypeError('pattern is too long');
}
let regExp,
re = '';
let regExp;
let hasSpecialCharacter = false;
if (pattern === '') return '';

for (
let i = 0, len = pattern.length, c;
i < len && (c = pattern.charAt(i));
i++
) {
if (c === '*') {
this.hasSpecialCharacter = true;
re += '.*?';
} else {
re += c;
}
}

// if the re is not '' at this point, then we need to make sure
// it doesn't match against an empty path part.
// Otherwise a/* will match a/, which it should not.
if (re !== '' && this.hasSpecialCharacter) {
re = '(?=.)' + re;
}
const re = pattern.replace(/\*/g, () => {
hasSpecialCharacter = true;
return '.+?';
});

// skip the regexp for non-* patterns
// unescape anything in it, though, so that it'll be
// an exact match.
if (!this.hasSpecialCharacter) {
if (!hasSpecialCharacter) {
return pattern.replace(/\\(.)/g, '$1');
}

Expand Down

0 comments on commit 5a42b66

Please sign in to comment.