Skip to content

Commit

Permalink
chore(util/string-match): add massagePattern option (#28552)
Browse files Browse the repository at this point in the history
  • Loading branch information
secustor committed Apr 21, 2024
1 parent 49afe4e commit d14ea60
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
18 changes: 18 additions & 0 deletions lib/util/string-match.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,23 @@ describe('util/string-match', () => {
it('returns false if negative pattern is matched', () => {
expect(matchRegexOrGlob('test', '!/te/')).toBeFalse();
});

it('returns true for wildcard is massaged', () => {
expect(
matchRegexOrGlob('test', '^*$', { massagePattern: true }),
).toBeTrue();
});

it('returns true for glob wildcard is massaged', () => {
expect(
matchRegexOrGlob('test', '*', { massagePattern: true }),
).toBeTrue();
});

it('returns true for massaged regex pattern is massaged', () => {
expect(
matchRegexOrGlob('test', '.*', { massagePattern: true }),
).toBeTrue();
});
});
});
27 changes: 24 additions & 3 deletions lib/util/string-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { regEx } from './regex';

export type StringMatchPredicate = (s: string) => boolean;

export interface matchRegexOrGlobOptions {
massagePattern?: boolean;
}

export function isDockerDigest(input: string): boolean {
return /^sha256:[a-f0-9]{64}$/i.test(input);
}
Expand All @@ -18,14 +22,23 @@ export function getRegexOrGlobPredicate(pattern: string): StringMatchPredicate {
return (x: string): boolean => mm.match(x);
}

export function matchRegexOrGlob(input: string, pattern: string): boolean {
export function matchRegexOrGlob(
input: string,
rawPattern: string,
options?: matchRegexOrGlobOptions,
): boolean {
const pattern = options?.massagePattern
? massagePattern(rawPattern)
: rawPattern;

const predicate = getRegexOrGlobPredicate(pattern);
return predicate(input);
}

export function matchRegexOrGlobList(
input: string,
patterns: string[],
options?: matchRegexOrGlobOptions,
): boolean {
if (!patterns.length) {
return false;
Expand All @@ -37,7 +50,9 @@ export function matchRegexOrGlobList(
);
if (
positivePatterns.length &&
!positivePatterns.some((pattern) => matchRegexOrGlob(input, pattern))
!positivePatterns.some((pattern) =>
matchRegexOrGlob(input, pattern, options),
)
) {
return false;
}
Expand All @@ -48,7 +63,9 @@ export function matchRegexOrGlobList(
);
if (
negativePatterns.length &&
!negativePatterns.every((pattern) => matchRegexOrGlob(input, pattern))
!negativePatterns.every((pattern) =>
matchRegexOrGlob(input, pattern, options),
)
) {
return false;
}
Expand Down Expand Up @@ -94,3 +111,7 @@ export function getRegexPredicate(input: string): StringMatchPredicate | null {
}
return null;
}

function massagePattern(pattern: string): string {
return pattern === '^*$' || pattern === '*' ? '**' : `/${pattern}/`;
}

0 comments on commit d14ea60

Please sign in to comment.