@domql/utils 2.3.69 → 2.3.70
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/dist/cjs/object.js +108 -76
- package/object.js +46 -14
- package/package.json +2 -2
package/dist/cjs/object.js
CHANGED
|
@@ -39,59 +39,61 @@ __export(object_exports, {
|
|
|
39
39
|
overwriteObj: () => overwriteObj
|
|
40
40
|
});
|
|
41
41
|
module.exports = __toCommonJS(object_exports);
|
|
42
|
+
var import_globals = require("@domql/globals");
|
|
42
43
|
var import_types = require("./types.js");
|
|
43
44
|
const exec = (param, element, state) => {
|
|
44
45
|
if ((0, import_types.isFunction)(param))
|
|
45
46
|
return param(element, state || element.state);
|
|
46
47
|
return param;
|
|
47
48
|
};
|
|
48
|
-
const map = (
|
|
49
|
+
const map = (obj, extention, element) => {
|
|
49
50
|
for (const e in extention) {
|
|
50
|
-
|
|
51
|
+
obj[e] = exec(extention[e], element);
|
|
51
52
|
}
|
|
52
53
|
};
|
|
53
|
-
const merge = (element,
|
|
54
|
-
for (const e in
|
|
54
|
+
const merge = (element, obj) => {
|
|
55
|
+
for (const e in obj) {
|
|
55
56
|
const elementProp = element[e];
|
|
56
|
-
const
|
|
57
|
+
const objProp = obj[e];
|
|
57
58
|
if (elementProp === void 0) {
|
|
58
|
-
element[e] =
|
|
59
|
+
element[e] = objProp;
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
62
|
return element;
|
|
62
63
|
};
|
|
63
64
|
const deepMerge = (element, extend) => {
|
|
64
65
|
for (const e in extend) {
|
|
65
|
-
const elementProp = element[e];
|
|
66
66
|
const extendProp = extend[e];
|
|
67
67
|
if (e === "parent" || e === "props")
|
|
68
68
|
continue;
|
|
69
|
-
if (
|
|
69
|
+
if (element[e] === void 0) {
|
|
70
|
+
element[e] = extendProp;
|
|
71
|
+
} else if ((0, import_types.isObjectLike)(element[e]) && (0, import_types.isObjectLike)(extendProp)) {
|
|
72
|
+
deepMerge(element[e], extendProp);
|
|
73
|
+
} else {
|
|
70
74
|
element[e] = extendProp;
|
|
71
|
-
} else if ((0, import_types.isObjectLike)(elementProp) && (0, import_types.isObject)(extendProp)) {
|
|
72
|
-
deepMerge(elementProp, extendProp);
|
|
73
75
|
}
|
|
74
76
|
}
|
|
75
77
|
return element;
|
|
76
78
|
};
|
|
77
|
-
const clone = (
|
|
79
|
+
const clone = (obj) => {
|
|
78
80
|
const o = {};
|
|
79
|
-
for (const
|
|
80
|
-
if (
|
|
81
|
+
for (const prop in obj) {
|
|
82
|
+
if (prop === "node")
|
|
81
83
|
continue;
|
|
82
|
-
o[
|
|
84
|
+
o[prop] = obj[prop];
|
|
83
85
|
}
|
|
84
86
|
return o;
|
|
85
87
|
};
|
|
86
|
-
const deepCloneExclude = (
|
|
87
|
-
if ((0, import_types.isArray)(
|
|
88
|
-
return
|
|
88
|
+
const deepCloneExclude = (obj, exclude = []) => {
|
|
89
|
+
if ((0, import_types.isArray)(obj)) {
|
|
90
|
+
return obj.map((x) => deepCloneExclude(x, exclude));
|
|
89
91
|
}
|
|
90
92
|
const o = {};
|
|
91
|
-
for (const k in
|
|
93
|
+
for (const k in obj) {
|
|
92
94
|
if (exclude.indexOf(k) > -1)
|
|
93
95
|
continue;
|
|
94
|
-
let v =
|
|
96
|
+
let v = obj[k];
|
|
95
97
|
if (k === "extend" && (0, import_types.isArray)(v)) {
|
|
96
98
|
v = mergeArrayExclude(v, exclude);
|
|
97
99
|
}
|
|
@@ -107,59 +109,89 @@ const deepCloneExclude = (obj2, exclude = []) => {
|
|
|
107
109
|
const mergeArrayExclude = (arr, excl = []) => {
|
|
108
110
|
return arr.reduce((acc, curr) => deepMerge(acc, deepCloneExclude(curr, excl)), {});
|
|
109
111
|
};
|
|
110
|
-
const deepClone = (
|
|
111
|
-
if ((0, import_types.isArray)(
|
|
112
|
-
return
|
|
112
|
+
const deepClone = (obj) => {
|
|
113
|
+
if ((0, import_types.isArray)(obj)) {
|
|
114
|
+
return obj.map(deepClone);
|
|
113
115
|
}
|
|
114
116
|
const o = {};
|
|
115
|
-
for (const
|
|
116
|
-
let
|
|
117
|
-
if (
|
|
118
|
-
|
|
117
|
+
for (const prop in obj) {
|
|
118
|
+
let objProp = obj[prop];
|
|
119
|
+
if (prop === "extend" && (0, import_types.isArray)(objProp)) {
|
|
120
|
+
objProp = mergeArray(objProp);
|
|
119
121
|
}
|
|
120
|
-
if ((0, import_types.isArray)(
|
|
121
|
-
o[
|
|
122
|
-
} else if ((0, import_types.isObject)(
|
|
123
|
-
o[
|
|
122
|
+
if ((0, import_types.isArray)(objProp)) {
|
|
123
|
+
o[prop] = objProp.map((v) => (0, import_types.isObject)(v) ? deepClone(v) : v);
|
|
124
|
+
} else if ((0, import_types.isObject)(objProp)) {
|
|
125
|
+
o[prop] = deepClone(objProp);
|
|
124
126
|
} else
|
|
125
|
-
o[
|
|
127
|
+
o[prop] = objProp;
|
|
126
128
|
}
|
|
127
129
|
return o;
|
|
128
130
|
};
|
|
129
|
-
const deepStringify = (
|
|
130
|
-
for (const
|
|
131
|
-
const
|
|
132
|
-
if ((0, import_types.isFunction)(
|
|
133
|
-
|
|
134
|
-
} else if ((0, import_types.isObject)(
|
|
135
|
-
|
|
136
|
-
deepStringify(
|
|
137
|
-
} else if ((0, import_types.isArray)(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
131
|
+
const deepStringify = (obj, stringified = {}) => {
|
|
132
|
+
for (const prop in obj) {
|
|
133
|
+
const objProp = obj[prop];
|
|
134
|
+
if ((0, import_types.isFunction)(objProp)) {
|
|
135
|
+
stringified[prop] = objProp.toString();
|
|
136
|
+
} else if ((0, import_types.isObject)(objProp)) {
|
|
137
|
+
stringified[prop] = {};
|
|
138
|
+
deepStringify(objProp, stringified[prop]);
|
|
139
|
+
} else if ((0, import_types.isArray)(objProp)) {
|
|
140
|
+
stringified[prop] = [];
|
|
141
|
+
objProp.forEach((v, i) => {
|
|
142
|
+
if ((0, import_types.isObject)(v)) {
|
|
143
|
+
stringified[prop][i] = {};
|
|
144
|
+
deepStringify(v, stringified[prop][i]);
|
|
145
|
+
} else if ((0, import_types.isFunction)(v)) {
|
|
146
|
+
stringified[prop][i] = v.toString();
|
|
147
|
+
} else {
|
|
148
|
+
stringified[prop][i] = v;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
stringified[prop] = objProp;
|
|
153
|
+
}
|
|
142
154
|
}
|
|
143
|
-
|
|
144
|
-
return stringified2;
|
|
155
|
+
return stringified;
|
|
145
156
|
};
|
|
146
157
|
const deepDestringify = (obj, stringified = {}) => {
|
|
147
158
|
for (const prop in obj) {
|
|
148
159
|
const objProp = obj[prop];
|
|
149
160
|
if ((0, import_types.isString)(objProp)) {
|
|
150
|
-
if (objProp.includes("=>") || objProp.includes("function") || objProp
|
|
161
|
+
if (objProp.includes("=>") || objProp.includes("function") || objProp.startsWith("(")) {
|
|
151
162
|
try {
|
|
152
|
-
const evalProp = eval(objProp);
|
|
163
|
+
const evalProp = import_globals.window.eval(`(${objProp})`);
|
|
153
164
|
stringified[prop] = evalProp;
|
|
154
165
|
} catch (e) {
|
|
155
166
|
if (e)
|
|
156
167
|
stringified[prop] = objProp;
|
|
157
168
|
}
|
|
158
169
|
}
|
|
159
|
-
} else
|
|
170
|
+
} else {
|
|
160
171
|
stringified[prop] = objProp;
|
|
161
|
-
|
|
162
|
-
|
|
172
|
+
}
|
|
173
|
+
if ((0, import_types.isArray)(objProp)) {
|
|
174
|
+
stringified[prop] = [];
|
|
175
|
+
objProp.forEach((arrProp) => {
|
|
176
|
+
if ((0, import_types.isString)(arrProp)) {
|
|
177
|
+
if (arrProp.includes("=>") || arrProp.includes("function") || arrProp.startsWith("(")) {
|
|
178
|
+
try {
|
|
179
|
+
const evalProp = import_globals.window.eval(arrProp);
|
|
180
|
+
stringified[prop].push(evalProp);
|
|
181
|
+
} catch (e) {
|
|
182
|
+
if (e)
|
|
183
|
+
stringified[prop].push(arrProp);
|
|
184
|
+
}
|
|
185
|
+
} else {
|
|
186
|
+
stringified[prop].push(arrProp);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
stringified[prop].push(deepDestringify(arrProp));
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
} else if ((0, import_types.isObject)(objProp)) {
|
|
193
|
+
stringified[prop] = deepDestringify(objProp, stringified[prop]);
|
|
194
|
+
}
|
|
163
195
|
}
|
|
164
196
|
return stringified;
|
|
165
197
|
};
|
|
@@ -178,44 +210,44 @@ const overwrite = (element, params, options) => {
|
|
|
178
210
|
}
|
|
179
211
|
return changes;
|
|
180
212
|
};
|
|
181
|
-
const diff = (
|
|
213
|
+
const diff = (obj, original, cache) => {
|
|
182
214
|
const changes = cache || {};
|
|
183
|
-
for (const e in
|
|
215
|
+
for (const e in obj) {
|
|
184
216
|
if (e === "ref")
|
|
185
217
|
continue;
|
|
186
218
|
const originalProp = original[e];
|
|
187
|
-
const
|
|
188
|
-
if ((0, import_types.isObjectLike)(originalProp) && (0, import_types.isObjectLike)(
|
|
219
|
+
const objProp = obj[e];
|
|
220
|
+
if ((0, import_types.isObjectLike)(originalProp) && (0, import_types.isObjectLike)(objProp)) {
|
|
189
221
|
changes[e] = {};
|
|
190
|
-
diff(originalProp,
|
|
191
|
-
} else if (
|
|
192
|
-
changes[e] =
|
|
222
|
+
diff(originalProp, objProp, changes[e]);
|
|
223
|
+
} else if (objProp !== void 0) {
|
|
224
|
+
changes[e] = objProp;
|
|
193
225
|
}
|
|
194
226
|
}
|
|
195
227
|
return changes;
|
|
196
228
|
};
|
|
197
|
-
const overwriteObj = (params,
|
|
229
|
+
const overwriteObj = (params, obj) => {
|
|
198
230
|
const changes = {};
|
|
199
231
|
for (const e in params) {
|
|
200
|
-
const
|
|
232
|
+
const objProp = obj[e];
|
|
201
233
|
const paramsProp = params[e];
|
|
202
234
|
if (paramsProp) {
|
|
203
|
-
|
|
235
|
+
obj[e] = changes[e] = objProp;
|
|
204
236
|
}
|
|
205
237
|
}
|
|
206
238
|
return changes;
|
|
207
239
|
};
|
|
208
|
-
const overwriteDeep = (params,
|
|
240
|
+
const overwriteDeep = (params, obj) => {
|
|
209
241
|
for (const e in params) {
|
|
210
|
-
const
|
|
242
|
+
const objProp = obj[e];
|
|
211
243
|
const paramsProp = params[e];
|
|
212
|
-
if ((0, import_types.isObjectLike)(
|
|
213
|
-
overwriteDeep(
|
|
244
|
+
if ((0, import_types.isObjectLike)(objProp) && (0, import_types.isObjectLike)(paramsProp)) {
|
|
245
|
+
overwriteDeep(paramsProp, objProp);
|
|
214
246
|
} else if (paramsProp !== void 0) {
|
|
215
|
-
|
|
247
|
+
obj[e] = paramsProp;
|
|
216
248
|
}
|
|
217
249
|
}
|
|
218
|
-
return
|
|
250
|
+
return obj;
|
|
219
251
|
};
|
|
220
252
|
const mergeIfExisted = (a, b) => {
|
|
221
253
|
if ((0, import_types.isObjectLike)(a) && (0, import_types.isObjectLike)(b))
|
|
@@ -225,16 +257,16 @@ const mergeIfExisted = (a, b) => {
|
|
|
225
257
|
const mergeArray = (arr) => {
|
|
226
258
|
return arr.reduce((a, c) => deepMerge(a, deepClone(c)), {});
|
|
227
259
|
};
|
|
228
|
-
const mergeAndCloneIfArray = (
|
|
229
|
-
return (0, import_types.isArray)(
|
|
260
|
+
const mergeAndCloneIfArray = (obj) => {
|
|
261
|
+
return (0, import_types.isArray)(obj) ? mergeArray(obj) : deepClone(obj);
|
|
230
262
|
};
|
|
231
|
-
const flattenRecursive = (param,
|
|
263
|
+
const flattenRecursive = (param, prop, stack = []) => {
|
|
232
264
|
const objectized = mergeAndCloneIfArray(param);
|
|
233
265
|
stack.push(objectized);
|
|
234
|
-
const extendOfExtend = objectized[
|
|
266
|
+
const extendOfExtend = objectized[prop];
|
|
235
267
|
if (extendOfExtend)
|
|
236
|
-
flattenRecursive(extendOfExtend,
|
|
237
|
-
delete objectized[
|
|
268
|
+
flattenRecursive(extendOfExtend, prop, stack);
|
|
269
|
+
delete objectized[prop];
|
|
238
270
|
return stack;
|
|
239
271
|
};
|
|
240
272
|
const isEqualDeep = (param, element) => {
|
|
@@ -242,9 +274,9 @@ const isEqualDeep = (param, element) => {
|
|
|
242
274
|
return true;
|
|
243
275
|
if (!param || !element)
|
|
244
276
|
return false;
|
|
245
|
-
for (const
|
|
246
|
-
const paramProp = param[
|
|
247
|
-
const elementProp = element[
|
|
277
|
+
for (const prop in param) {
|
|
278
|
+
const paramProp = param[prop];
|
|
279
|
+
const elementProp = element[prop];
|
|
248
280
|
if ((0, import_types.isObjectLike)(paramProp)) {
|
|
249
281
|
const isEqual = isEqualDeep(paramProp, elementProp);
|
|
250
282
|
if (!isEqual)
|
package/object.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
import { window } from '@domql/globals'
|
|
3
4
|
import { isFunction, isObjectLike, isObject, isArray, isString } from './types.js'
|
|
4
5
|
|
|
5
6
|
export const exec = (param, element, state) => {
|
|
@@ -26,14 +27,14 @@ export const merge = (element, obj) => {
|
|
|
26
27
|
|
|
27
28
|
export const deepMerge = (element, extend) => {
|
|
28
29
|
for (const e in extend) {
|
|
29
|
-
const elementProp = element[e]
|
|
30
30
|
const extendProp = extend[e]
|
|
31
|
-
// const cachedProps = cache.props
|
|
32
31
|
if (e === 'parent' || e === 'props') continue
|
|
33
|
-
if (
|
|
32
|
+
if (element[e] === undefined) {
|
|
33
|
+
element[e] = extendProp
|
|
34
|
+
} else if (isObjectLike(element[e]) && isObjectLike(extendProp)) {
|
|
35
|
+
deepMerge(element[e], extendProp)
|
|
36
|
+
} else {
|
|
34
37
|
element[e] = extendProp
|
|
35
|
-
} else if (isObjectLike(elementProp) && isObject(extendProp)) {
|
|
36
|
-
deepMerge(elementProp, extendProp)
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
return element
|
|
@@ -111,13 +112,23 @@ export const deepStringify = (obj, stringified = {}) => {
|
|
|
111
112
|
stringified[prop] = objProp.toString()
|
|
112
113
|
} else if (isObject(objProp)) {
|
|
113
114
|
stringified[prop] = {}
|
|
114
|
-
deepStringify(objProp
|
|
115
|
+
deepStringify(objProp, stringified[prop])
|
|
115
116
|
} else if (isArray(objProp)) {
|
|
116
117
|
stringified[prop] = []
|
|
117
|
-
objProp.
|
|
118
|
-
|
|
118
|
+
objProp.forEach((v, i) => {
|
|
119
|
+
if (isObject(v)) {
|
|
120
|
+
stringified[prop][i] = {}
|
|
121
|
+
deepStringify(v, stringified[prop][i])
|
|
122
|
+
} else if (isFunction(v)) {
|
|
123
|
+
stringified[prop][i] = v.toString()
|
|
124
|
+
} else {
|
|
125
|
+
stringified[prop][i] = v
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
} else {
|
|
129
|
+
stringified[prop] = objProp
|
|
130
|
+
}
|
|
119
131
|
}
|
|
120
|
-
console.log(obj, stringified)
|
|
121
132
|
return stringified
|
|
122
133
|
}
|
|
123
134
|
|
|
@@ -128,14 +139,35 @@ export const deepDestringify = (obj, stringified = {}) => {
|
|
|
128
139
|
for (const prop in obj) {
|
|
129
140
|
const objProp = obj[prop]
|
|
130
141
|
if (isString(objProp)) {
|
|
131
|
-
if (objProp.includes('=>') || objProp.includes('function') || objProp
|
|
142
|
+
if (objProp.includes('=>') || objProp.includes('function') || objProp.startsWith('(')) {
|
|
132
143
|
try {
|
|
133
|
-
const evalProp = eval(objProp) //
|
|
144
|
+
const evalProp = window.eval(`(${objProp})`) // use parentheses to convert string to function expression
|
|
134
145
|
stringified[prop] = evalProp
|
|
135
146
|
} catch (e) { if (e) stringified[prop] = objProp }
|
|
136
147
|
}
|
|
137
|
-
} else
|
|
138
|
-
|
|
148
|
+
} else {
|
|
149
|
+
stringified[prop] = objProp
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (isArray(objProp)) {
|
|
153
|
+
stringified[prop] = []
|
|
154
|
+
objProp.forEach((arrProp) => {
|
|
155
|
+
if (isString(arrProp)) {
|
|
156
|
+
if (arrProp.includes('=>') || arrProp.includes('function') || arrProp.startsWith('(')) {
|
|
157
|
+
try {
|
|
158
|
+
const evalProp = window.eval(arrProp) // eslint-disable-line
|
|
159
|
+
stringified[prop].push(evalProp)
|
|
160
|
+
} catch (e) { if (e) stringified[prop].push(arrProp) }
|
|
161
|
+
} else {
|
|
162
|
+
stringified[prop].push(arrProp)
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
stringified[prop].push(deepDestringify(arrProp))
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
} else if (isObject(objProp)) {
|
|
169
|
+
stringified[prop] = deepDestringify(objProp, stringified[prop]) // recursively call deepDestringify for nested objects
|
|
170
|
+
}
|
|
139
171
|
}
|
|
140
172
|
return stringified
|
|
141
173
|
}
|
|
@@ -204,7 +236,7 @@ export const overwriteDeep = (params, obj) => {
|
|
|
204
236
|
const objProp = obj[e]
|
|
205
237
|
const paramsProp = params[e]
|
|
206
238
|
if (isObjectLike(objProp) && isObjectLike(paramsProp)) {
|
|
207
|
-
overwriteDeep(
|
|
239
|
+
overwriteDeep(paramsProp, objProp)
|
|
208
240
|
} else if (paramsProp !== undefined) {
|
|
209
241
|
obj[e] = paramsProp
|
|
210
242
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/utils",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.70",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "index.js",
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"@domql/globals": "latest",
|
|
23
23
|
"@domql/tags": "latest"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "c2fa4c40d134093b837eafc1c4374561032281bf"
|
|
26
26
|
}
|