0
0
Fork 0
mirror of https://github.com/renovatebot/renovate.git synced 2025-01-27 13:20:13 +00:00
renovatebot_renovate/lib/workers/global/config/parse/coersions.ts
renovate[bot] eb8c08079e
chore(deps): update typescript-eslint monorepo to v8 (major) (#30750)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
2024-08-14 10:33:02 +00:00

43 lines
965 B
TypeScript

import is from '@sindresorhus/is';
import JSON5 from 'json5';
export const coersions: Record<string, (arg: string) => unknown> = {
boolean: (val: string): boolean => {
if (val === 'true' || val === '') {
return true;
}
if (val === 'false') {
return false;
}
throw new Error(
"Invalid boolean value: expected 'true' or 'false', but got '" +
val +
"'",
);
},
array: (val: string): string[] => {
if (val === '') {
return [];
}
try {
return JSON5.parse(val);
} catch {
return val
.split(',')
.map((el) => el.trim())
.filter(is.nonEmptyString);
}
},
object: (val: string): any => {
if (val === '') {
return {};
}
try {
return JSON5.parse(val);
} catch {
throw new Error("Invalid JSON value: '" + val + "'");
}
},
string: (val: string): string => val.replace(/\\n/g, '\n'),
integer: parseInt,
};