json-variables 11.0.2 → 11.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -6
- package/LICENSE +1 -1
- package/dist/json-variables.esm.js +440 -371
- package/dist/json-variables.umd.js +26 -26
- package/package.json +46 -50
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @name json-variables
|
|
3
3
|
* @fileoverview Resolves custom-marked, cross-referenced paths in parsed JSON
|
|
4
|
-
* @version 11.0.
|
|
4
|
+
* @version 11.0.6
|
|
5
5
|
* @author Roy Revelt, Codsen Ltd
|
|
6
6
|
* @license MIT
|
|
7
7
|
* {@link https://codsen.com/os/json-variables/}
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
10
|
import { traverse } from 'ast-monkey-traverse';
|
|
11
|
-
import
|
|
11
|
+
import { isMatch } from 'matcher';
|
|
12
12
|
import objectPath from 'object-path';
|
|
13
13
|
import { arrayiffy } from 'arrayiffy-if-string';
|
|
14
14
|
import { strFindHeadsTails } from 'string-find-heads-tails';
|
|
@@ -18,425 +18,494 @@ import { rApply } from 'ranges-apply';
|
|
|
18
18
|
import { remDup } from 'string-remove-duplicate-heads-tails';
|
|
19
19
|
import { matchRightIncl, matchLeftIncl } from 'string-match-left-right';
|
|
20
20
|
|
|
21
|
-
var version$1 = "11.0.
|
|
21
|
+
var version$1 = "11.0.6";
|
|
22
22
|
|
|
23
23
|
const version = version$1;
|
|
24
24
|
const has = Object.prototype.hasOwnProperty;
|
|
25
25
|
const defaults = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
26
|
+
heads: "%%_",
|
|
27
|
+
tails: "_%%",
|
|
28
|
+
headsNoWrap: "%%-",
|
|
29
|
+
tailsNoWrap: "-%%",
|
|
30
|
+
lookForDataContainers: true,
|
|
31
|
+
dataContainerIdentifierTails: "_data",
|
|
32
|
+
wrapHeadsWith: "",
|
|
33
|
+
wrapTailsWith: "",
|
|
34
|
+
dontWrapVars: [],
|
|
35
|
+
preventDoubleWrapping: true,
|
|
36
|
+
wrapGlobalFlipSwitch: true,
|
|
37
|
+
noSingleMarkers: false,
|
|
38
|
+
resolveToBoolIfAnyValuesContainBool: true,
|
|
39
|
+
resolveToFalseIfAnyValuesContainBool: true,
|
|
40
|
+
throwWhenNonStringInsertedInString: false,
|
|
41
|
+
allowUnresolved: false,
|
|
42
42
|
};
|
|
43
43
|
function isStr(something) {
|
|
44
|
-
|
|
44
|
+
return typeof something === "string";
|
|
45
45
|
}
|
|
46
46
|
function isNum(something) {
|
|
47
|
-
|
|
47
|
+
return typeof something === "number";
|
|
48
48
|
}
|
|
49
49
|
function isBool(something) {
|
|
50
|
-
|
|
50
|
+
return typeof something === "boolean";
|
|
51
51
|
}
|
|
52
52
|
function isNull(something) {
|
|
53
|
-
|
|
53
|
+
return something === null;
|
|
54
54
|
}
|
|
55
55
|
function isObj(something) {
|
|
56
|
-
|
|
56
|
+
return (something && typeof something === "object" && !Array.isArray(something));
|
|
57
57
|
}
|
|
58
58
|
function existy(x) {
|
|
59
|
-
|
|
59
|
+
return x != null;
|
|
60
60
|
}
|
|
61
61
|
function trimIfString(something) {
|
|
62
|
-
|
|
62
|
+
return isStr(something) ? something.trim() : something;
|
|
63
63
|
}
|
|
64
64
|
function getTopmostKey(str) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
if (typeof str === "string" && str.length > 0 && str.indexOf(".") !== -1) {
|
|
66
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
67
|
+
if (str[i] === ".") {
|
|
68
|
+
return str.slice(0, i);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return str;
|
|
73
73
|
}
|
|
74
74
|
function withoutTopmostKey(str) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
75
|
+
if (typeof str === "string" && str.length > 0 && str.indexOf(".") !== -1) {
|
|
76
|
+
for (let i = 0, len = str.length; i < len; i++) {
|
|
77
|
+
if (str[i] === ".") {
|
|
78
|
+
return str.slice(i + 1);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return str;
|
|
83
83
|
}
|
|
84
84
|
function goLevelUp(str) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
85
|
+
if (typeof str === "string" && str.length > 0 && str.indexOf(".") !== -1) {
|
|
86
|
+
for (let i = str.length; i--;) {
|
|
87
|
+
if (str[i] === ".") {
|
|
88
|
+
return str.slice(0, i);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return str;
|
|
93
93
|
}
|
|
94
94
|
function getLastKey(str) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
if (typeof str === "string" && str.length > 0 && str.indexOf(".") !== -1) {
|
|
96
|
+
for (let i = str.length; i--;) {
|
|
97
|
+
if (str[i] === ".") {
|
|
98
|
+
return str.slice(i + 1);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return str;
|
|
103
103
|
}
|
|
104
104
|
function containsHeadsOrTails(str, opts) {
|
|
105
|
-
|
|
105
|
+
if (typeof str !== "string" || !str.trim()) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
if (str.includes(opts.heads) ||
|
|
109
|
+
str.includes(opts.tails) ||
|
|
110
|
+
(isStr(opts.headsNoWrap) &&
|
|
111
|
+
opts.headsNoWrap.length > 0 &&
|
|
112
|
+
str.includes(opts.headsNoWrap)) ||
|
|
113
|
+
(isStr(opts.tailsNoWrap) &&
|
|
114
|
+
opts.tailsNoWrap.length > 0 &&
|
|
115
|
+
str.includes(opts.tailsNoWrap))) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
106
118
|
return false;
|
|
107
|
-
}
|
|
108
|
-
if (str.includes(opts.heads) || str.includes(opts.tails) || isStr(opts.headsNoWrap) && opts.headsNoWrap.length > 0 && str.includes(opts.headsNoWrap) || isStr(opts.tailsNoWrap) && opts.tailsNoWrap.length > 0 && str.includes(opts.tailsNoWrap)) {
|
|
109
|
-
return true;
|
|
110
|
-
}
|
|
111
|
-
return false;
|
|
112
119
|
}
|
|
113
120
|
function removeWrappingHeadsAndTails(str, heads, tails) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
121
|
+
let tempFrom;
|
|
122
|
+
let tempTo;
|
|
123
|
+
if (typeof str === "string" &&
|
|
124
|
+
str.length > 0 &&
|
|
125
|
+
matchRightIncl(str, 0, heads, {
|
|
126
|
+
trimBeforeMatching: true,
|
|
127
|
+
cb: (_c, _t, index) => {
|
|
128
|
+
tempFrom = index;
|
|
129
|
+
return true;
|
|
130
|
+
},
|
|
131
|
+
}) &&
|
|
132
|
+
matchLeftIncl(str, str.length - 1, tails, {
|
|
133
|
+
trimBeforeMatching: true,
|
|
134
|
+
cb: (_c, _t, index) => {
|
|
135
|
+
tempTo = index + 1;
|
|
136
|
+
return true;
|
|
137
|
+
},
|
|
138
|
+
})) {
|
|
139
|
+
return str.slice(tempFrom, tempTo);
|
|
140
|
+
}
|
|
141
|
+
return str;
|
|
132
142
|
}
|
|
133
143
|
function wrap(placementValue, opts, dontWrapTheseVars = false, breadCrumbPath, newPath, oldVarName) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
if (isStr(placementValue) && !dontWrapTheseVars && opts.wrapGlobalFlipSwitch && !opts.dontWrapVars.some(val => matcher.isMatch(oldVarName, val)) && (!opts.preventDoubleWrapping || opts.preventDoubleWrapping && isStr(placementValue) && !placementValue.includes(opts.wrapHeadsWith) && !placementValue.includes(opts.wrapTailsWith))) {
|
|
141
|
-
return opts.wrapHeadsWith + placementValue + opts.wrapTailsWith;
|
|
142
|
-
}
|
|
143
|
-
if (dontWrapTheseVars) {
|
|
144
|
-
if (!isStr(placementValue)) {
|
|
145
|
-
return placementValue;
|
|
146
|
-
}
|
|
147
|
-
const tempValue = remDup(placementValue, {
|
|
148
|
-
heads: opts.wrapHeadsWith,
|
|
149
|
-
tails: opts.wrapTailsWith
|
|
150
|
-
});
|
|
151
|
-
if (!isStr(tempValue)) {
|
|
152
|
-
return tempValue;
|
|
144
|
+
if (!opts.wrapHeadsWith) {
|
|
145
|
+
opts.wrapHeadsWith = "";
|
|
146
|
+
}
|
|
147
|
+
if (!opts.wrapTailsWith) {
|
|
148
|
+
opts.wrapTailsWith = "";
|
|
153
149
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
150
|
+
if (isStr(placementValue) &&
|
|
151
|
+
!dontWrapTheseVars &&
|
|
152
|
+
opts.wrapGlobalFlipSwitch &&
|
|
153
|
+
!opts.dontWrapVars.some((val) => isMatch(oldVarName, val)) &&
|
|
154
|
+
(!opts.preventDoubleWrapping ||
|
|
155
|
+
(opts.preventDoubleWrapping &&
|
|
156
|
+
isStr(placementValue) &&
|
|
157
|
+
!placementValue.includes(opts.wrapHeadsWith) &&
|
|
158
|
+
!placementValue.includes(opts.wrapTailsWith)))) {
|
|
159
|
+
return opts.wrapHeadsWith + placementValue + opts.wrapTailsWith;
|
|
160
|
+
}
|
|
161
|
+
if (dontWrapTheseVars) {
|
|
162
|
+
if (!isStr(placementValue)) {
|
|
163
|
+
return placementValue;
|
|
164
|
+
}
|
|
165
|
+
const tempValue = remDup(placementValue, {
|
|
166
|
+
heads: opts.wrapHeadsWith,
|
|
167
|
+
tails: opts.wrapTailsWith,
|
|
168
|
+
});
|
|
169
|
+
if (!isStr(tempValue)) {
|
|
170
|
+
return tempValue;
|
|
171
|
+
}
|
|
172
|
+
return removeWrappingHeadsAndTails(tempValue, opts.wrapHeadsWith, opts.wrapTailsWith);
|
|
173
|
+
}
|
|
174
|
+
return placementValue;
|
|
157
175
|
}
|
|
158
176
|
function findValues(input, varName, path, opts) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (getLastKey(currentPath) === varName) {
|
|
173
|
-
throw new Error(`json-variables/findValues(): [THROW_ID_20] While trying to resolve: "${varName}" at path "${path}", we encountered a closed loop. The parent key "${getLastKey(currentPath)}" is called the same as the variable "${varName}" we're looking for.`);
|
|
174
|
-
}
|
|
175
|
-
if (opts.lookForDataContainers && typeof opts.dataContainerIdentifierTails === "string" && opts.dataContainerIdentifierTails.length > 0 && !currentPath.endsWith(opts.dataContainerIdentifierTails)) {
|
|
176
|
-
const gotPath = objectPath.get(input, currentPath + opts.dataContainerIdentifierTails);
|
|
177
|
-
if (isObj(gotPath) && objectPath.get(gotPath, varName)) {
|
|
178
|
-
resolveValue = objectPath.get(gotPath, varName);
|
|
179
|
-
handBrakeOff = false;
|
|
177
|
+
let resolveValue;
|
|
178
|
+
if (path.indexOf(".") !== -1) {
|
|
179
|
+
let currentPath = path;
|
|
180
|
+
let handBrakeOff = true;
|
|
181
|
+
if (opts.lookForDataContainers &&
|
|
182
|
+
typeof opts.dataContainerIdentifierTails === "string" &&
|
|
183
|
+
opts.dataContainerIdentifierTails.length > 0 &&
|
|
184
|
+
!currentPath.endsWith(opts.dataContainerIdentifierTails)) {
|
|
185
|
+
const gotPath = objectPath.get(input, currentPath + opts.dataContainerIdentifierTails);
|
|
186
|
+
if (isObj(gotPath) && objectPath.get(gotPath, varName)) {
|
|
187
|
+
resolveValue = objectPath.get(gotPath, varName);
|
|
188
|
+
handBrakeOff = false;
|
|
189
|
+
}
|
|
180
190
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
191
|
+
while (handBrakeOff && currentPath.indexOf(".") !== -1) {
|
|
192
|
+
currentPath = goLevelUp(currentPath);
|
|
193
|
+
if (getLastKey(currentPath) === varName) {
|
|
194
|
+
throw new Error(`json-variables/findValues(): [THROW_ID_20] While trying to resolve: "${varName}" at path "${path}", we encountered a closed loop. The parent key "${getLastKey(currentPath)}" is called the same as the variable "${varName}" we're looking for.`);
|
|
195
|
+
}
|
|
196
|
+
if (opts.lookForDataContainers &&
|
|
197
|
+
typeof opts.dataContainerIdentifierTails === "string" &&
|
|
198
|
+
opts.dataContainerIdentifierTails.length > 0 &&
|
|
199
|
+
!currentPath.endsWith(opts.dataContainerIdentifierTails)) {
|
|
200
|
+
const gotPath = objectPath.get(input, currentPath + opts.dataContainerIdentifierTails);
|
|
201
|
+
if (isObj(gotPath) && objectPath.get(gotPath, varName)) {
|
|
202
|
+
resolveValue = objectPath.get(gotPath, varName);
|
|
203
|
+
handBrakeOff = false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (resolveValue === undefined) {
|
|
207
|
+
const gotPath = objectPath.get(input, currentPath);
|
|
208
|
+
if (isObj(gotPath) && objectPath.get(gotPath, varName)) {
|
|
209
|
+
resolveValue = objectPath.get(gotPath, varName);
|
|
210
|
+
handBrakeOff = false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (resolveValue === undefined) {
|
|
216
|
+
const gotPath = objectPath.get(input, varName);
|
|
217
|
+
if (gotPath !== undefined) {
|
|
218
|
+
resolveValue = gotPath;
|
|
187
219
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
}
|
|
220
|
+
}
|
|
221
|
+
if (resolveValue === undefined) {
|
|
222
|
+
if (varName.indexOf(".") === -1) {
|
|
223
|
+
const gotPathArr = getByKey(input, varName);
|
|
224
|
+
if (gotPathArr.length > 0) {
|
|
225
|
+
for (let y = 0, len2 = gotPathArr.length; y < len2; y++) {
|
|
226
|
+
if (isStr(gotPathArr[y].val) ||
|
|
227
|
+
isBool(gotPathArr[y].val) ||
|
|
228
|
+
isNull(gotPathArr[y].val)) {
|
|
229
|
+
resolveValue = gotPathArr[y].val;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
else if (isNum(gotPathArr[y].val)) {
|
|
233
|
+
resolveValue = String(gotPathArr[y].val);
|
|
234
|
+
break;
|
|
235
|
+
}
|
|
236
|
+
else if (Array.isArray(gotPathArr[y].val)) {
|
|
237
|
+
resolveValue = gotPathArr[y].val.join("");
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
throw new Error(`json-variables/findValues(): [THROW_ID_21] While trying to resolve: "${varName}" at path "${path}", we actually found the key named ${varName}, but it was not equal to a string but to:\n${JSON.stringify(gotPathArr[y], null, 4)}\nWe can't resolve a string with that! It should be a string.`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
214
245
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
246
|
+
else {
|
|
247
|
+
const gotPath = getByKey(input, getTopmostKey(varName));
|
|
248
|
+
if (gotPath.length > 0) {
|
|
249
|
+
for (let y = 0, len2 = gotPath.length; y < len2; y++) {
|
|
250
|
+
const temp = objectPath.get(gotPath[y].val, withoutTopmostKey(varName));
|
|
251
|
+
if (temp && isStr(temp)) {
|
|
252
|
+
resolveValue = temp;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
224
256
|
}
|
|
225
|
-
}
|
|
226
257
|
}
|
|
227
|
-
|
|
228
|
-
return resolveValue;
|
|
258
|
+
return resolveValue;
|
|
229
259
|
}
|
|
230
260
|
function resolveString(input, string, path, opts, incomingBreadCrumbPath = []) {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const secretResolvedVarsStash = {};
|
|
241
|
-
const breadCrumbPath = Array.from(incomingBreadCrumbPath);
|
|
242
|
-
breadCrumbPath.push(path);
|
|
243
|
-
const finalRangesArr = new Ranges();
|
|
244
|
-
function processHeadsAndTails(arr, dontWrapTheseVars, wholeValueIsVariable) {
|
|
245
|
-
for (let i = 0, len = arr.length; i < len; i++) {
|
|
246
|
-
const obj = arr[i];
|
|
247
|
-
const varName = string.slice(obj.headsEndAt, obj.tailsStartAt);
|
|
248
|
-
if (varName.length === 0) {
|
|
249
|
-
finalRangesArr.push(obj.headsStartAt,
|
|
250
|
-
obj.tailsEndAt
|
|
251
|
-
);
|
|
252
|
-
} else if (has.call(secretResolvedVarsStash, varName) && isStr(secretResolvedVarsStash[varName])) {
|
|
253
|
-
finalRangesArr.push(obj.headsStartAt,
|
|
254
|
-
obj.tailsEndAt,
|
|
255
|
-
secretResolvedVarsStash[varName]
|
|
256
|
-
);
|
|
257
|
-
} else {
|
|
258
|
-
let resolvedValue = findValues(input,
|
|
259
|
-
varName.trim(),
|
|
260
|
-
path,
|
|
261
|
-
opts
|
|
262
|
-
);
|
|
263
|
-
if (resolvedValue === undefined) {
|
|
264
|
-
if (opts.allowUnresolved === true) {
|
|
265
|
-
resolvedValue = "";
|
|
266
|
-
} else if (typeof opts.allowUnresolved === "string") {
|
|
267
|
-
resolvedValue = opts.allowUnresolved;
|
|
268
|
-
} else {
|
|
269
|
-
throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_18] We couldn't find the value to resolve the variable ${string.slice(obj.headsEndAt, obj.tailsStartAt)}. We're at path: "${path}".`);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
if (!wholeValueIsVariable && opts.throwWhenNonStringInsertedInString && !isStr(resolvedValue)) {
|
|
273
|
-
throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_23] While resolving the variable ${string.slice(obj.headsEndAt, obj.tailsStartAt)} at path ${path}, it resolved into a non-string value, ${JSON.stringify(resolvedValue, null, 4)}. This is happening because options setting "throwWhenNonStringInsertedInString" is active (set to "true").`);
|
|
261
|
+
if (incomingBreadCrumbPath.includes(path)) {
|
|
262
|
+
let extra = "";
|
|
263
|
+
if (incomingBreadCrumbPath.length > 1) {
|
|
264
|
+
const separator = " →\n";
|
|
265
|
+
extra = incomingBreadCrumbPath.reduce((accum, curr, idx) => accum +
|
|
266
|
+
(idx === 0 ? "" : separator) +
|
|
267
|
+
(curr === path ? "💥 " : " ") +
|
|
268
|
+
curr, " Here's the path we travelled up until we hit the recursion:\n\n");
|
|
269
|
+
extra += `${separator}💥 ${path}`;
|
|
274
270
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
271
|
+
throw new Error(`json-variables/resolveString(): [THROW_ID_19] While trying to resolve: "${string}" at path "${path}", we encountered a closed loop, the key is referencing itself."${extra}`);
|
|
272
|
+
}
|
|
273
|
+
const secretResolvedVarsStash = {};
|
|
274
|
+
const breadCrumbPath = Array.from(incomingBreadCrumbPath);
|
|
275
|
+
breadCrumbPath.push(path);
|
|
276
|
+
const finalRangesArr = new Ranges();
|
|
277
|
+
function processHeadsAndTails(arr, dontWrapTheseVars, wholeValueIsVariable) {
|
|
278
|
+
for (let i = 0, len = arr.length; i < len; i++) {
|
|
279
|
+
const obj = arr[i];
|
|
280
|
+
const varName = string.slice(obj.headsEndAt, obj.tailsStartAt);
|
|
281
|
+
if (varName.length === 0) {
|
|
282
|
+
finalRangesArr.push(obj.headsStartAt,
|
|
283
|
+
obj.tailsEndAt
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
else if (has.call(secretResolvedVarsStash, varName) &&
|
|
287
|
+
isStr(secretResolvedVarsStash[varName])) {
|
|
288
|
+
finalRangesArr.push(obj.headsStartAt,
|
|
289
|
+
obj.tailsEndAt,
|
|
290
|
+
secretResolvedVarsStash[varName]
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
let resolvedValue = findValues(input,
|
|
295
|
+
varName.trim(),
|
|
296
|
+
path,
|
|
297
|
+
opts
|
|
298
|
+
);
|
|
299
|
+
if (resolvedValue === undefined) {
|
|
300
|
+
if (opts.allowUnresolved === true) {
|
|
301
|
+
resolvedValue = "";
|
|
302
|
+
}
|
|
303
|
+
else if (typeof opts.allowUnresolved === "string") {
|
|
304
|
+
resolvedValue = opts.allowUnresolved;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_18] We couldn't find the value to resolve the variable ${string.slice(obj.headsEndAt, obj.tailsStartAt)}. We're at path: "${path}".`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (!wholeValueIsVariable &&
|
|
311
|
+
opts.throwWhenNonStringInsertedInString &&
|
|
312
|
+
!isStr(resolvedValue)) {
|
|
313
|
+
throw new Error(`json-variables/processHeadsAndTails(): [THROW_ID_23] While resolving the variable ${string.slice(obj.headsEndAt, obj.tailsStartAt)} at path ${path}, it resolved into a non-string value, ${JSON.stringify(resolvedValue, null, 4)}. This is happening because options setting "throwWhenNonStringInsertedInString" is active (set to "true").`);
|
|
314
|
+
}
|
|
315
|
+
if (isBool(resolvedValue)) {
|
|
316
|
+
if (opts.resolveToBoolIfAnyValuesContainBool) {
|
|
317
|
+
finalRangesArr.wipe();
|
|
318
|
+
if (!opts.resolveToFalseIfAnyValuesContainBool) {
|
|
319
|
+
return resolvedValue;
|
|
320
|
+
}
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
resolvedValue = "";
|
|
324
|
+
}
|
|
325
|
+
else if (isNull(resolvedValue) && wholeValueIsVariable) {
|
|
326
|
+
finalRangesArr.wipe();
|
|
327
|
+
return resolvedValue;
|
|
328
|
+
}
|
|
329
|
+
else if (Array.isArray(resolvedValue)) {
|
|
330
|
+
resolvedValue = String(resolvedValue.join(""));
|
|
331
|
+
}
|
|
332
|
+
else if (isNull(resolvedValue)) {
|
|
333
|
+
resolvedValue = "";
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
resolvedValue = String(resolvedValue);
|
|
337
|
+
}
|
|
338
|
+
const newPath = path.includes(".")
|
|
339
|
+
? `${goLevelUp(path)}.${varName}`
|
|
340
|
+
: varName;
|
|
341
|
+
if (containsHeadsOrTails(resolvedValue, opts)) {
|
|
342
|
+
const replacementVal = wrap(resolveString(
|
|
343
|
+
input, resolvedValue, newPath, opts, breadCrumbPath), opts, dontWrapTheseVars, breadCrumbPath, newPath, varName.trim());
|
|
344
|
+
if (isStr(replacementVal)) {
|
|
345
|
+
finalRangesArr.push(obj.headsStartAt,
|
|
346
|
+
obj.tailsEndAt,
|
|
347
|
+
replacementVal);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
secretResolvedVarsStash[varName] = resolvedValue;
|
|
352
|
+
const replacementVal = wrap(resolvedValue, opts, dontWrapTheseVars, breadCrumbPath, newPath, varName.trim());
|
|
353
|
+
if (isStr(replacementVal)) {
|
|
354
|
+
finalRangesArr.push(obj.headsStartAt,
|
|
355
|
+
obj.tailsEndAt,
|
|
356
|
+
replacementVal);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
280
359
|
}
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
resolvedValue = "";
|
|
284
|
-
} else if (isNull(resolvedValue) && wholeValueIsVariable) {
|
|
285
|
-
finalRangesArr.wipe();
|
|
286
|
-
return resolvedValue;
|
|
287
|
-
} else if (Array.isArray(resolvedValue)) {
|
|
288
|
-
resolvedValue = String(resolvedValue.join(""));
|
|
289
|
-
} else if (isNull(resolvedValue)) {
|
|
290
|
-
resolvedValue = "";
|
|
291
|
-
} else {
|
|
292
|
-
resolvedValue = String(resolvedValue);
|
|
293
|
-
}
|
|
294
|
-
const newPath = path.includes(".") ? `${goLevelUp(path)}.${varName}` : varName;
|
|
295
|
-
if (containsHeadsOrTails(resolvedValue, opts)) {
|
|
296
|
-
const replacementVal = wrap(resolveString(
|
|
297
|
-
input, resolvedValue, newPath, opts, breadCrumbPath), opts, dontWrapTheseVars, breadCrumbPath, newPath, varName.trim());
|
|
298
|
-
if (isStr(replacementVal)) {
|
|
299
|
-
finalRangesArr.push(obj.headsStartAt,
|
|
300
|
-
obj.tailsEndAt,
|
|
301
|
-
replacementVal);
|
|
302
|
-
}
|
|
303
|
-
} else {
|
|
304
|
-
secretResolvedVarsStash[varName] = resolvedValue;
|
|
305
|
-
const replacementVal = wrap(resolvedValue, opts, dontWrapTheseVars, breadCrumbPath, newPath, varName.trim());
|
|
306
|
-
if (isStr(replacementVal)) {
|
|
307
|
-
finalRangesArr.push(obj.headsStartAt,
|
|
308
|
-
obj.tailsEndAt,
|
|
309
|
-
replacementVal);
|
|
310
|
-
}
|
|
311
360
|
}
|
|
312
|
-
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
361
|
+
return undefined;
|
|
362
|
+
}
|
|
363
|
+
let foundHeadsAndTails;
|
|
364
|
+
try {
|
|
365
|
+
foundHeadsAndTails = strFindHeadsTails(string, opts.heads, opts.tails, {
|
|
366
|
+
source: "",
|
|
367
|
+
throwWhenSomethingWrongIsDetected: false,
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
catch (error) {
|
|
371
|
+
throw new Error(`json-variables/resolveString(): [THROW_ID_17] While trying to resolve string: "${string}" at path ${path}, something wrong with heads and tails was detected! Here's the internal error message:\n${error}`);
|
|
372
|
+
}
|
|
373
|
+
let wholeValueIsVariable = false;
|
|
374
|
+
if (foundHeadsAndTails.length === 1 &&
|
|
375
|
+
rApply(string, [
|
|
376
|
+
[foundHeadsAndTails[0].headsStartAt, foundHeadsAndTails[0].tailsEndAt],
|
|
377
|
+
]).trim() === "") {
|
|
378
|
+
wholeValueIsVariable = true;
|
|
379
|
+
}
|
|
380
|
+
const temp1 = processHeadsAndTails(foundHeadsAndTails, false, wholeValueIsVariable);
|
|
381
|
+
if (isBool(temp1)) {
|
|
382
|
+
return temp1;
|
|
383
|
+
}
|
|
384
|
+
if (isNull(temp1)) {
|
|
385
|
+
return temp1;
|
|
386
|
+
}
|
|
387
|
+
try {
|
|
388
|
+
foundHeadsAndTails = strFindHeadsTails(string, opts.headsNoWrap, opts.tailsNoWrap, {
|
|
389
|
+
source: "",
|
|
390
|
+
throwWhenSomethingWrongIsDetected: false,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
throw new Error(`json-variables/resolveString(): [THROW_ID_22] While trying to resolve string: "${string}" at path ${path}, something wrong with no-wrap heads and no-wrap tails was detected! Here's the internal error message:\n${error}`);
|
|
395
|
+
}
|
|
396
|
+
if (foundHeadsAndTails.length === 1 &&
|
|
397
|
+
rApply(string, [
|
|
398
|
+
[foundHeadsAndTails[0].headsStartAt, foundHeadsAndTails[0].tailsEndAt],
|
|
399
|
+
]).trim() === "") {
|
|
400
|
+
wholeValueIsVariable = true;
|
|
401
|
+
}
|
|
402
|
+
const temp2 = processHeadsAndTails(foundHeadsAndTails, true, wholeValueIsVariable);
|
|
403
|
+
if (isBool(temp2)) {
|
|
404
|
+
return temp2;
|
|
405
|
+
}
|
|
406
|
+
if (isNull(temp2)) {
|
|
407
|
+
return temp2;
|
|
408
|
+
}
|
|
409
|
+
if (finalRangesArr && finalRangesArr.current()) {
|
|
410
|
+
return rApply(string, finalRangesArr.current());
|
|
411
|
+
}
|
|
412
|
+
return string;
|
|
358
413
|
}
|
|
359
414
|
function jVar(input, originalOpts) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
opts.dontWrapVars
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
413
|
-
if (opts.headsNoWrap === opts.tailsNoWrap) {
|
|
414
|
-
throw new Error("json-variables/jVar(): [THROW_ID_14] Alas! opts.headsNoWrap and opts.tailsNoWrap can't be equal!");
|
|
415
|
-
}
|
|
416
|
-
let current;
|
|
417
|
-
return traverse(input, (key, val, innerObj) => {
|
|
418
|
-
if (existy(val) && containsHeadsOrTails(key, opts)) {
|
|
419
|
-
throw new Error(`json-variables/jVar(): [THROW_ID_15] Alas! Object keys can't contain variables!\nPlease check the following key: ${key}`);
|
|
420
|
-
}
|
|
421
|
-
if (val !== undefined) {
|
|
422
|
-
current = val;
|
|
423
|
-
} else {
|
|
424
|
-
current = key;
|
|
425
|
-
}
|
|
426
|
-
if (current === "") {
|
|
427
|
-
return current;
|
|
428
|
-
}
|
|
429
|
-
if (opts.heads.length !== 0 && trimIfString(current) === trimIfString(opts.heads) || opts.tails.length !== 0 && trimIfString(current) === trimIfString(opts.tails) || opts.headsNoWrap.length !== 0 && trimIfString(current) === trimIfString(opts.headsNoWrap) || opts.tailsNoWrap.length !== 0 && trimIfString(current) === trimIfString(opts.tailsNoWrap)) {
|
|
430
|
-
if (!opts.noSingleMarkers) {
|
|
431
|
-
return current;
|
|
432
|
-
}
|
|
433
|
-
throw new Error(`json-variables/jVar(): [THROW_ID_16] Alas! While processing the input, we stumbled upon ${trimIfString(current)} which is equal to ${trimIfString(current) === trimIfString(opts.heads) ? "heads" : ""}${trimIfString(current) === trimIfString(opts.tails) ? "tails" : ""}${isStr(opts.headsNoWrap) && trimIfString(current) === trimIfString(opts.headsNoWrap) ? "headsNoWrap" : ""}${isStr(opts.tailsNoWrap) && trimIfString(current) === trimIfString(opts.tailsNoWrap) ? "tailsNoWrap" : ""}. If you wouldn't have set opts.noSingleMarkers to "true" this error would not happen and computer would have left the current element (${trimIfString(current)}) alone`);
|
|
415
|
+
if (!arguments.length) {
|
|
416
|
+
throw new Error("json-variables/jVar(): [THROW_ID_01] Alas! Inputs are missing!");
|
|
417
|
+
}
|
|
418
|
+
if (!isObj(input)) {
|
|
419
|
+
throw new TypeError(`json-variables/jVar(): [THROW_ID_02] Alas! The input must be a plain object! Currently it's: ${Array.isArray(input) ? "array" : typeof input}`);
|
|
420
|
+
}
|
|
421
|
+
if (originalOpts && !isObj(originalOpts)) {
|
|
422
|
+
throw new TypeError(`json-variables/jVar(): [THROW_ID_03] Alas! An Optional Options Object must be a plain object! Currently it's: ${Array.isArray(originalOpts) ? "array" : typeof originalOpts}`);
|
|
423
|
+
}
|
|
424
|
+
const opts = { ...defaults, ...originalOpts };
|
|
425
|
+
if (!opts.dontWrapVars) {
|
|
426
|
+
opts.dontWrapVars = [];
|
|
427
|
+
}
|
|
428
|
+
else if (!Array.isArray(opts.dontWrapVars)) {
|
|
429
|
+
opts.dontWrapVars = arrayiffy(opts.dontWrapVars);
|
|
430
|
+
}
|
|
431
|
+
let culpritVal;
|
|
432
|
+
let culpritIndex;
|
|
433
|
+
if (opts.dontWrapVars.length > 0 &&
|
|
434
|
+
!opts.dontWrapVars.every((el, idx) => {
|
|
435
|
+
if (!isStr(el)) {
|
|
436
|
+
culpritVal = el;
|
|
437
|
+
culpritIndex = idx;
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
return true;
|
|
441
|
+
})) {
|
|
442
|
+
throw new Error(`json-variables/jVar(): [THROW_ID_05] Alas! All variable names set in opts.dontWrapVars should be of a string type. Computer detected a value "${culpritVal}" at index ${culpritIndex}, which is not string but ${Array.isArray(culpritVal) ? "array" : typeof culpritVal}!`);
|
|
443
|
+
}
|
|
444
|
+
if (opts.heads === "") {
|
|
445
|
+
throw new Error("json-variables/jVar(): [THROW_ID_06] Alas! opts.heads are empty!");
|
|
446
|
+
}
|
|
447
|
+
if (opts.tails === "") {
|
|
448
|
+
throw new Error("json-variables/jVar(): [THROW_ID_07] Alas! opts.tails are empty!");
|
|
449
|
+
}
|
|
450
|
+
if (opts.lookForDataContainers && opts.dataContainerIdentifierTails === "") {
|
|
451
|
+
throw new Error("json-variables/jVar(): [THROW_ID_08] Alas! opts.dataContainerIdentifierTails is empty!");
|
|
452
|
+
}
|
|
453
|
+
if (opts.heads === opts.tails) {
|
|
454
|
+
throw new Error("json-variables/jVar(): [THROW_ID_09] Alas! opts.heads and opts.tails can't be equal!");
|
|
455
|
+
}
|
|
456
|
+
if (opts.heads === opts.headsNoWrap) {
|
|
457
|
+
throw new Error("json-variables/jVar(): [THROW_ID_10] Alas! opts.heads and opts.headsNoWrap can't be equal!");
|
|
458
|
+
}
|
|
459
|
+
if (opts.tails === opts.tailsNoWrap) {
|
|
460
|
+
throw new Error("json-variables/jVar(): [THROW_ID_11] Alas! opts.tails and opts.tailsNoWrap can't be equal!");
|
|
461
|
+
}
|
|
462
|
+
if (opts.headsNoWrap === "") {
|
|
463
|
+
throw new Error("json-variables/jVar(): [THROW_ID_12] Alas! opts.headsNoWrap is an empty string!");
|
|
464
|
+
}
|
|
465
|
+
if (opts.tailsNoWrap === "") {
|
|
466
|
+
throw new Error("json-variables/jVar(): [THROW_ID_13] Alas! opts.tailsNoWrap is an empty string!");
|
|
434
467
|
}
|
|
435
|
-
if (
|
|
436
|
-
|
|
468
|
+
if (opts.headsNoWrap === opts.tailsNoWrap) {
|
|
469
|
+
throw new Error("json-variables/jVar(): [THROW_ID_14] Alas! opts.headsNoWrap and opts.tailsNoWrap can't be equal!");
|
|
437
470
|
}
|
|
438
|
-
|
|
439
|
-
|
|
471
|
+
let current;
|
|
472
|
+
return traverse(input, (key, val, innerObj) => {
|
|
473
|
+
if (existy(val) && containsHeadsOrTails(key, opts)) {
|
|
474
|
+
throw new Error(`json-variables/jVar(): [THROW_ID_15] Alas! Object keys can't contain variables!\nPlease check the following key: ${key}`);
|
|
475
|
+
}
|
|
476
|
+
if (val !== undefined) {
|
|
477
|
+
current = val;
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
current = key;
|
|
481
|
+
}
|
|
482
|
+
if (current === "") {
|
|
483
|
+
return current;
|
|
484
|
+
}
|
|
485
|
+
if ((opts.heads.length !== 0 &&
|
|
486
|
+
trimIfString(current) === trimIfString(opts.heads)) ||
|
|
487
|
+
(opts.tails.length !== 0 &&
|
|
488
|
+
trimIfString(current) === trimIfString(opts.tails)) ||
|
|
489
|
+
(opts.headsNoWrap.length !== 0 &&
|
|
490
|
+
trimIfString(current) === trimIfString(opts.headsNoWrap)) ||
|
|
491
|
+
(opts.tailsNoWrap.length !== 0 &&
|
|
492
|
+
trimIfString(current) === trimIfString(opts.tailsNoWrap))) {
|
|
493
|
+
if (!opts.noSingleMarkers) {
|
|
494
|
+
return current;
|
|
495
|
+
}
|
|
496
|
+
throw new Error(`json-variables/jVar(): [THROW_ID_16] Alas! While processing the input, we stumbled upon ${trimIfString(current)} which is equal to ${trimIfString(current) === trimIfString(opts.heads) ? "heads" : ""}${trimIfString(current) === trimIfString(opts.tails) ? "tails" : ""}${isStr(opts.headsNoWrap) &&
|
|
497
|
+
trimIfString(current) === trimIfString(opts.headsNoWrap)
|
|
498
|
+
? "headsNoWrap"
|
|
499
|
+
: ""}${isStr(opts.tailsNoWrap) &&
|
|
500
|
+
trimIfString(current) === trimIfString(opts.tailsNoWrap)
|
|
501
|
+
? "tailsNoWrap"
|
|
502
|
+
: ""}. If you wouldn't have set opts.noSingleMarkers to "true" this error would not happen and computer would have left the current element (${trimIfString(current)}) alone`);
|
|
503
|
+
}
|
|
504
|
+
if (isStr(current) && containsHeadsOrTails(current, opts)) {
|
|
505
|
+
return resolveString(input, current, innerObj.path, opts);
|
|
506
|
+
}
|
|
507
|
+
return current;
|
|
508
|
+
});
|
|
440
509
|
}
|
|
441
510
|
|
|
442
511
|
export { defaults, jVar, version };
|