-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (54 loc) · 2.37 KB
/
index.js
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
var propertyPrefix = "${";
var propertyPostfix = "}";
var propertyDelimeter = ".";
var allSettings;
function isDict(v) {
return (typeof v==='object' && v!==null && !(v instanceof Array) && !(v instanceof Date));
}
function isString(s) {
return (typeof s === 'string' && s !== null);
}
var getValueForSettingsKey = function (keyName, settings) {
var namespaceLookup = keyName.split(propertyDelimeter);
var value = settings;
for (var i in namespaceLookup) {
value = value[namespaceLookup[i]];
if (value === undefined) {
//console.log("could not resolve property: " + keyName);
return null;
}
}
return value;
}
var resolvePlaceholdersRecursive = function(json) {
if (isDict(json)) {
for (var key in json) {
var subJson = json[key];
var resolvedDict = resolvePlaceholdersRecursive(subJson);
json[key] = resolvedDict;
}
}
else if (isString(json) && json.includes(propertyPrefix) && json.includes(propertyPostfix)) {
var prefixIndex = json.lastIndexOf(propertyPrefix);
var postfixIndex = json.lastIndexOf(propertyPostfix);
var placeholderKeyToResolve = json.substring(prefixIndex + propertyPrefix.length, postfixIndex);
var resolvedPlaceholderKeyValue = getValueForSettingsKey(placeholderKeyToResolve, allSettings);
resolvedPlaceholderKeyValue = resolvedPlaceholderKeyValue === null ? "???" : resolvedPlaceholderKeyValue;
var jsonAfterResolution = json.replace(propertyPrefix + placeholderKeyToResolve + propertyPostfix, resolvedPlaceholderKeyValue);
//console.log("Found Key to resolve: " + placeholderKeyToResolve + " in " + json + " resolved to " +
// resolvedPlaceholderKeyValue);
// Call it again in case there are more placeholders to resolve in the same string.
var resolved = resolvePlaceholdersRecursive(jsonAfterResolution);
json = resolved;
}
return json;
}
var resolvePlaceholders = function(propertiesJson, placeholderPrefix, placeholderSuffix, placeholderDelimeter) {
allSettings = propertiesJson;
propertyPrefix = placeholderPrefix;
propertyPostfix = placeholderSuffix;
propertyDelimeter = placeholderDelimeter;
return resolvePlaceholdersRecursive(propertiesJson);
}
exports.resolve = resolvePlaceholders;
exports.resolveValue = getValueForSettingsKey;