-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcommon.ts
71 lines (60 loc) · 2.27 KB
/
common.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { JsonObject, isObject } from '@app-config/utils';
import { logger } from '@app-config/logging';
export type KeyFormatter = (key: string, separator: string) => string;
/** Strategy used in 'app-config vars' for variable names */
export function camelToScreamingCase(key: string, separator: string = '_'): string {
return key
.replace(/([^A-Z]+)([A-Z][a-z])/g, `$1${separator}$2`)
.replace(/([^0-9]+)([0-9]+)/g, `$1${separator}$2`)
.replace(/-/g, separator)
.toUpperCase();
}
/** Strategy used in 'app-config vars' to extract variable names from hierachy */
export function flattenObjectTree(
obj: JsonObject,
prefix: string = '',
separator: string = '_',
formatter: KeyFormatter = camelToScreamingCase,
): { [key: string]: string } {
return Object.entries(obj).reduce((merged, [key, value]) => {
const flatKey = `${prefix}${prefix ? separator : ''}${formatter(key, separator)}`;
let flattenedObject;
if (isObject(value)) {
flattenedObject = flattenObjectTree(value, flatKey, separator, formatter);
} else if (Array.isArray(value)) {
const flattenedArray = value.reduce<JsonObject>((acc, val, ind) => {
return Object.assign(acc, { [ind]: val });
}, {});
flattenedObject = flattenObjectTree(flattenedArray, flatKey, separator, formatter);
} else {
flattenedObject = {
[flatKey]: value,
};
}
return Object.assign(merged, flattenedObject);
}, {});
}
/** Strategy for renaming keys, used for 'app-config vars' */
export function renameInFlattenedTree(
flattened: { [key: string]: string },
renames: string[] = [],
keepOriginalKeys = false,
): typeof flattened {
for (const rename of renames) {
const matched = /^(.*)=(.*)$/.exec(rename);
if (matched) {
const [, renameFrom, renameTo] = matched;
if (flattened[renameFrom]) {
flattened[renameTo] = flattened[renameFrom]; // eslint-disable-line no-param-reassign
if (!keepOriginalKeys) {
delete flattened[renameFrom]; // eslint-disable-line no-param-reassign
}
} else {
logger.warn(`A rename was used ('${rename}'), but no value was found.`);
}
} else {
logger.warn(`A rename was used ('${rename}'), but not in correct format.`);
}
}
return flattened;
}