-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.js
51 lines (48 loc) · 1.47 KB
/
variables.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
module.exports = (inputFormat, inputVariables) => {
let depthCounter = 0;
let inVar = false;
let variables = [];
let tmp = '';
let out = inputFormat;
for (let char of inputFormat) {
if (char === '{') {
if (inVar) throw new Error();
inVar = true;
} else if (char === '}') {
if (!inVar) throw new Error();
inVar = false;
} else {
if (inVar) {
tmp += char;
}
}
if (!inVar) {
let t = tmp.split('.');
if (t.length > 1) {
variables.push({
parent: t[0],
child: t[1]
});
} else {
if (t[0] !== '') {
variables.push({
parent: t[0]
});
}
}
tmp = '';
}
}
for (let vari of variables) {
if (inputVariables[vari.parent] === undefined) continue;
if (typeof inputVariables[vari.parent] === 'object') {
for (let child in inputVariables[vari.parent]) {
if (vari.child !== child) continue;
out = out.replace(new RegExp('{' + vari.parent + '.' + vari.child + '}'), inputVariables[vari.parent][vari.child]);
}
} else {
out = out.replace(new RegExp('{' + vari.parent + '}'), inputVariables[vari.parent]);
}
}
return out;
};