-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
58 lines (49 loc) · 1.47 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
var _ = require('lodash');
var regForStaging = /^\$\w*?_/;
function isStagingVariable(key, stage) {
if (_.isNumber(key)){
return false;
}
return key.indexOf('$' + stage + '_') === 0;
}
function processSection(variables, stage) {
var result = {};
if(_.isArray(variables)) {
return variables;
}
_.forEach(variables, function (val, key) {
if (isStagingVariable(key, stage)) {
var newKey = key.substring(stage.length + 2);
var innerSection = typeof val === 'object' ? processSection(val, stage): val;
result[newKey] = innerSection;
} else if (regForStaging.exec(key) === null && result[key] === undefined) {
var innerSection = typeof val === 'object' ? processSection(val, stage): val;
result[key] = innerSection;
} else if(regForStaging.exec(key) === null && _.isArray(val) && result[key] === undefined) {
result[key] = val;
}
});
return result;
}
function bindReferences(orgJson, variables) {
var result = {};
_.forEach(variables, function (val, key) {
if (_.isPlainObject(val)) {
var innerSection = bindReferences(orgJson, val);
result[key] = innerSection;
} else if (regForStaging.exec(key) === null && result[key] === undefined) {
if (typeof val === 'string'){
result[key] = _.template(val)(orgJson);
} else {
result[key] = val;
}
}
});
return result;
}
function process(variables, stage){
var json = processSection(variables, stage);
var result = bindReferences(json, json);
return result;
}
module.exports = process;