Skip to content

Commit

Permalink
refactor: move nested function to module scope (#17361)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
viceice and rarkins committed Aug 23, 2022
1 parent 57ade2b commit 33e3bf7
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions lib/modules/manager/regex/utils.ts
Expand Up @@ -20,6 +20,27 @@ export const validMatchFields = [

type ValidMatchFields = typeof validMatchFields[number];

function updateDependency(
dependency: PackageDependency,
field: ValidMatchFields,
value: string
): void {
switch (field) {
case 'registryUrl':
// check if URL is valid and pack inside an array
try {
const url = new URL(value).toString();
dependency.registryUrls = [url];
} catch (err) {
logger.warn({ value }, 'Invalid regex manager registryUrl');
}
break;
default:
dependency[field] = value;
break;
}
}

export function createDependency(
extractionTemplate: ExtractionTemplate,
config: CustomExtractConfig,
Expand All @@ -28,30 +49,13 @@ export function createDependency(
const dependency = dep ?? {};
const { groups, replaceString } = extractionTemplate;

function updateDependency(field: ValidMatchFields, value: string): void {
switch (field) {
case 'registryUrl':
// check if URL is valid and pack inside an array
try {
const url = new URL(value).toString();
dependency.registryUrls = [url];
} catch (err) {
logger.warn({ value }, 'Invalid regex manager registryUrl');
}
break;
default:
dependency[field] = value;
break;
}
}

for (const field of validMatchFields) {
const fieldTemplate = `${field}Template` as keyof RegexManagerTemplates;
const tmpl = config[fieldTemplate];
if (tmpl) {
try {
const compiled = template.compile(tmpl, groups, false);
updateDependency(field, compiled);
updateDependency(dependency, field, compiled);
} catch (err) {
logger.warn(
{ template: tmpl },
Expand All @@ -60,7 +64,7 @@ export function createDependency(
return null;
}
} else if (groups[field]) {
updateDependency(field, groups[field]);
updateDependency(dependency, field, groups[field]);
}
}
dependency.replaceString = replaceString;
Expand Down

0 comments on commit 33e3bf7

Please sign in to comment.