From f4fe36e9b314e1eb9a474cdb4f99d435cacaa27b Mon Sep 17 00:00:00 2001 From: Sergei Zharinov <zharinov@users.noreply.github.com> Date: Thu, 8 Feb 2024 04:22:20 -0300 Subject: [PATCH] refactor: Extract generic predicate constructor for string match utils (#27130) --- lib/util/string-match.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/util/string-match.ts b/lib/util/string-match.ts index 42c7b234fa..5576ba3d17 100644 --- a/lib/util/string-match.ts +++ b/lib/util/string-match.ts @@ -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) { -- GitLab