Refactor config utils to use functions instead of consts

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-02-27 17:45:44 +01:00
parent b128efebff
commit 5987e2ed7e
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -4,22 +4,17 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
export const toArrayConfig: (
configValue: string,
separator?: string,
) => string[] = (configValue: string, separator = ',') => {
export function toArrayConfig(configValue: string, separator = ','): string[] {
if (!configValue) {
return [];
}
if (!configValue.includes(separator)) {
return [configValue.trim()];
}
return configValue.split(separator).map((arrayItem) => arrayItem.trim());
};
}
export const buildErrorMessage = (errorMessages: string[]): string => {
export function buildErrorMessage(errorMessages: string[]): string {
let totalErrorMessage = 'There were some errors with your configuration:';
for (const message of errorMessages) {
totalErrorMessage += '\n - ';
@ -28,14 +23,14 @@ export const buildErrorMessage = (errorMessages: string[]): string => {
totalErrorMessage +=
'\nFor further information, have a look at our configuration docs at https://docs.hedgedoc.org/configuration';
return totalErrorMessage;
};
}
export const replaceAuthErrorsWithEnvironmentVariables = (
export function replaceAuthErrorsWithEnvironmentVariables(
message: string,
name: string,
replacement: string,
arrayOfNames: string[],
): string => {
): string {
// this builds a regex like /"gitlab\[(\d+)]\./ to extract the position in the arrayOfNames
const regex = new RegExp('"' + name + '\\[(\\d+)]\\.', 'g');
message = message.replace(
@ -91,4 +86,4 @@ export const replaceAuthErrorsWithEnvironmentVariables = (
message = message.replace('.rolesClaim', '_ROLES_CLAIM');
message = message.replace('.accessRole', '_ACCESS_ROLE');
return message;
};
}