forked from taskcluster/json-parameterization
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
122 lines (110 loc) · 4 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// The MIT License (MIT)
//
// Copyright (c) 2014 Jonas Finnemann Jensen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
var _ = require("lodash");
var debug = require("debug")("json-parameterization");
/** Parameterize input with params */
var parameterize = function (input, params) {
// Evaluate an expression
var evalExpr = function (expr, fallback) {
// Find parts of the expression
var parts = expr.split(/\|/);
var value = parts.shift();
// Check if first value is on a "string" format
var match = /\s*"([^"]*)"\s*|\s*'([^']*)'\s*/.exec(value);
if (match) {
value = match[1] || match[2] || "";
} else {
// Otherwise, trim and look up in parameters
var result = params[value.trim()];
if (result instanceof Function) {
// if it's a function then we call it, without parameters
value = result();
} else if (result !== undefined) {
// if it's defined we return the value substituted in
value = _.cloneDeep(result);
} else {
// If we didn't get a value, function or string
debug("Couldn't find parameter: '%s' used in '%s'", value, expr);
return fallback;
}
}
// While there are parts of the expression remaining
while (parts.length > 0) {
// Find next part and trim it
var part = parts.shift().trim();
// Look up in params
var result = params[part];
if (result instanceof Function) {
// Modify value if we got a function
value = result(value);
} else {
// If we didn't get a function, then this is done
debug("Couldn't find modify: '%s' used in '%s'", part, expr);
return fallback;
}
}
// Return value
return value;
};
// Parameterize a string
var parameterizeString = function (str) {
// Handle special case where the entire string is an expression
if (/^{{[^}]+}}$/.test(str)) {
return evalExpr(str.substr(2, str.length - 4), str);
}
// Otherwise treat {{...}} as string interpolations
return str.replace(/{{([^}]*)}}/g, function (originalText, expression) {
return evalExpr(expression, originalText);
});
};
// Substitute objects that needs this
var substitute = function (obj) {
// Parameterized strings
if (typeof obj == "string") {
return parameterizeString(obj);
}
// Parameterize keys of objects
if (typeof obj == "object") {
if (obj === null) {
return obj;
}
if (obj instanceof Array) {
return undefined;
}
var clone = {};
for (var k in obj) {
var key = parameterizeString(k);
if (typeof key !== "string") {
key = k;
}
clone[key] = _.cloneDeepWith(obj[k], substitute);
}
return clone;
}
// Clone all other values as lodash normally would
return undefined;
};
// Create clone while substituting objects that need it
return _.cloneDeepWith(input, substitute);
};
// Export parameterize
module.exports = parameterize;