Skip to content
Snippets Groups Projects
Unverified Commit f4fe36e9 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor: Extract generic predicate constructor for string match utils (#27130)

parent e2e30b8a
No related branches found
No related tags found
No related merge requests found
......@@ -2,21 +2,31 @@ import is from '@sindresorhus/is';
import { minimatch } from './minimatch';
import { regEx } from './regex';
export type StringMatchPredicate = (s: string) => boolean;
export function isDockerDigest(input: string): boolean {
return /^sha256:[a-f0-9]{64}$/i.test(input);
}
export function matchRegexOrMinimatch(input: string, pattern: string): boolean {
export function makeRegexOrMinimatchPredicate(
pattern: string,
): StringMatchPredicate | null {
if (pattern.length > 2 && pattern.startsWith('/') && pattern.endsWith('/')) {
try {
const regex = regEx(pattern.slice(1, -1));
return regex.test(input);
return (x: string): boolean => regex.test(x);
} catch (err) {
return false;
return null;
}
}
return minimatch(pattern, { dot: true }).match(input);
const mm = minimatch(pattern, { dot: true });
return (x: string): boolean => mm.match(x);
}
export function matchRegexOrMinimatch(input: string, pattern: string): boolean {
const predicate = makeRegexOrMinimatchPredicate(pattern);
return predicate ? predicate(input) : false;
}
export function anyMatchRegexOrMinimatch(
......@@ -51,11 +61,9 @@ function parseConfigRegex(input: string): RegExp | null {
return null;
}
type ConfigRegexPredicate = (s: string) => boolean;
export function configRegexPredicate(
input: string,
): ConfigRegexPredicate | null {
): StringMatchPredicate | null {
if (isConfigRegex(input)) {
const configRegex = parseConfigRegex(input);
if (configRegex) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment