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