@cocreate/utils 1.8.1 → 1.10.0
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 +26 -0
- package/package.json +1 -1
- package/src/encodedecode.js +111 -0
- package/src/index.js +78 -1
- package/src/index.old.js +18 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
+
# [1.10.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.9.1...v1.10.0) (2022-08-23)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* dotNotationToObject to convert a dot notation string to a nested object structure ([b924d33](https://github.com/CoCreate-app/CoCreate-utils/commit/b924d333ccf364d6ac1f736e8cbfe2d8999316cb))
|
|
7
|
+
|
|
8
|
+
## [1.9.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.9.0...v1.9.1) (2022-07-29)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* removed console.log ([b237022](https://github.com/CoCreate-app/CoCreate-utils/commit/b2370221c33dc1972eee8204af8a310f9e44721c))
|
|
14
|
+
|
|
15
|
+
# [1.9.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.8.1...v1.9.0) (2022-07-03)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
### Bug Fixes
|
|
19
|
+
|
|
20
|
+
* add queryDocumentSelector to export default ([b067ed1](https://github.com/CoCreate-app/CoCreate-utils/commit/b067ed1354488cf52014e2d417d75c5be455fc6f))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
### Features
|
|
24
|
+
|
|
25
|
+
* queryDocumentSelector to return one element from an iframe or iframes parent ([477f179](https://github.com/CoCreate-app/CoCreate-utils/commit/477f179ef4e00aab219d12a5a9ae3a5e1cfae992))
|
|
26
|
+
|
|
1
27
|
## [1.8.1](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.8.0...v1.8.1) (2022-07-01)
|
|
2
28
|
|
|
3
29
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// function __mergeObject(target, source)
|
|
2
|
+
// {
|
|
3
|
+
// target = target || {};
|
|
4
|
+
// for (let key of Object.keys(source)) {
|
|
5
|
+
// if (source[key] instanceof Object) {
|
|
6
|
+
// Object.assign(source[key], __mergeObject(target[key], source[key]))
|
|
7
|
+
// }
|
|
8
|
+
// }
|
|
9
|
+
|
|
10
|
+
// Object.assign(target || {}, source)
|
|
11
|
+
// return target
|
|
12
|
+
// }
|
|
13
|
+
|
|
14
|
+
// function __createObject(data, path)
|
|
15
|
+
// {
|
|
16
|
+
// if (!path) return data;
|
|
17
|
+
|
|
18
|
+
// let keys = path.split('.')
|
|
19
|
+
// let newObject = data;
|
|
20
|
+
|
|
21
|
+
// for (var i = keys.length - 1; i >= 0; i--) {
|
|
22
|
+
// newObject = {[keys[i]]: newObject}
|
|
23
|
+
// }
|
|
24
|
+
// return newObject;
|
|
25
|
+
// }
|
|
26
|
+
|
|
27
|
+
// function __createArray(key, data)
|
|
28
|
+
// {
|
|
29
|
+
// try {
|
|
30
|
+
// let item = /([\w\W]+)\[(\d+)\]/gm.exec(key)
|
|
31
|
+
// if (item && item.length == 3) {
|
|
32
|
+
// let arrayKey = item[1];
|
|
33
|
+
// let index = parseInt(item[2]);
|
|
34
|
+
|
|
35
|
+
// if (!data[arrayKey] || !Array.isArray(data[arrayKey])) {
|
|
36
|
+
// data[arrayKey] = [];
|
|
37
|
+
// }
|
|
38
|
+
// data[arrayKey][index] = data[key];
|
|
39
|
+
// delete data[key];
|
|
40
|
+
// key = arrayKey;
|
|
41
|
+
// }
|
|
42
|
+
// } catch {
|
|
43
|
+
// console.log('create array error');
|
|
44
|
+
// }
|
|
45
|
+
// return key;
|
|
46
|
+
// }
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
// function isObject(item) {
|
|
51
|
+
// return (!!item) && (item.constructor === Object);
|
|
52
|
+
// }
|
|
53
|
+
// function isArray(item) {
|
|
54
|
+
// return (!!item) && (item.constructor === Array);
|
|
55
|
+
// }
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
// function decodeObject(data) {
|
|
59
|
+
// let keys = Object.keys(data)
|
|
60
|
+
// let objectData = {};
|
|
61
|
+
|
|
62
|
+
// keys.forEach((k) => {
|
|
63
|
+
// k = __createArray(k, data);
|
|
64
|
+
// if (k.split('.').length > 1) {
|
|
65
|
+
// let newData = __createObject(data[k], k);
|
|
66
|
+
// delete data[k];
|
|
67
|
+
|
|
68
|
+
// objectData = __mergeObject(objectData, newData);
|
|
69
|
+
// } else {
|
|
70
|
+
// objectData[k] = data[k];
|
|
71
|
+
// }
|
|
72
|
+
// })
|
|
73
|
+
// return objectData;
|
|
74
|
+
// }
|
|
75
|
+
|
|
76
|
+
// function encodeObject(data) {
|
|
77
|
+
// let keys = Object.keys(data);
|
|
78
|
+
// let newData = {};
|
|
79
|
+
// keys.forEach((k) => {
|
|
80
|
+
// let data_value = data[k];
|
|
81
|
+
// if (isObject(data[k])) {
|
|
82
|
+
// let new_obj = encodeObject(data[k]);
|
|
83
|
+
// let newKeys = Object.keys(new_obj);
|
|
84
|
+
// newKeys.forEach((newKey) => {
|
|
85
|
+
// let value = new_obj[newKey];
|
|
86
|
+
// if (isNaN(parseInt(newKey))) {
|
|
87
|
+
// newKey = `${k}.${newKey}`;
|
|
88
|
+
// } else {
|
|
89
|
+
// newKey = `${k}[${newKey}]`;
|
|
90
|
+
// }
|
|
91
|
+
|
|
92
|
+
// newData[newKey] = value;
|
|
93
|
+
// })
|
|
94
|
+
|
|
95
|
+
// } else if (isArray(data_value)){
|
|
96
|
+
// data_value.forEach((v, index) => {
|
|
97
|
+
// newData[`${k}[${index}]`] = v;
|
|
98
|
+
// })
|
|
99
|
+
// } else {
|
|
100
|
+
// newData[k] = data[k];
|
|
101
|
+
// }
|
|
102
|
+
// })
|
|
103
|
+
// return newData;
|
|
104
|
+
// }
|
|
105
|
+
|
|
106
|
+
module.exports = {
|
|
107
|
+
// decodeObject,
|
|
108
|
+
// encodeObject,
|
|
109
|
+
replaceArray
|
|
110
|
+
};
|
|
111
|
+
|
package/src/index.js
CHANGED
|
@@ -20,6 +20,46 @@ function clickedElement() {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
export function dotNotationToObject(data) {
|
|
24
|
+
try {
|
|
25
|
+
let obj = {};
|
|
26
|
+
|
|
27
|
+
for (const [key, value] of Object.entries(data)) {
|
|
28
|
+
let newObject = obj
|
|
29
|
+
let keys = key.split('.');
|
|
30
|
+
let length = keys.length - 1
|
|
31
|
+
for (let i = 0; i < keys.length; i++) {
|
|
32
|
+
if (/\[([0-9]*)\]/g.test(keys[i])){
|
|
33
|
+
let [k, index] = keys[i].split('[');
|
|
34
|
+
index = index.slice(0, -1)
|
|
35
|
+
console.log(index, k)
|
|
36
|
+
if (length == i){
|
|
37
|
+
newObject[k] = [];
|
|
38
|
+
newObject[k][index] = value;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
newObject[k] = [];
|
|
42
|
+
newObject[k][index] = {};
|
|
43
|
+
}
|
|
44
|
+
newObject = newObject[k][index]
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
if (length == i)
|
|
48
|
+
newObject[keys[i]] = value;
|
|
49
|
+
else
|
|
50
|
+
newObject[keys[i]] = {};
|
|
51
|
+
newObject = newObject[keys[i]]
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
console.log(obj);
|
|
56
|
+
return obj
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.log("Error converting dot notation to object", error);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
23
63
|
export function parseTextToHtml(text) {
|
|
24
64
|
let doc = new DOMParser().parseFromString(text, "text/html");
|
|
25
65
|
if (doc.head.children[0]) return doc.head.children[0];
|
|
@@ -99,7 +139,7 @@ export function domParser(str) {
|
|
|
99
139
|
try {
|
|
100
140
|
var mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
101
141
|
} catch (e){
|
|
102
|
-
console.log('find position: can not find the main tag');
|
|
142
|
+
// console.log(e, 'find position: can not find the main tag');
|
|
103
143
|
}
|
|
104
144
|
let doc;
|
|
105
145
|
switch (mainTag) {
|
|
@@ -159,6 +199,41 @@ export function queryDocumentSelectorAll(selector) {
|
|
|
159
199
|
}
|
|
160
200
|
}
|
|
161
201
|
|
|
202
|
+
export function queryDocumentSelector(selector) {
|
|
203
|
+
if (selector) {
|
|
204
|
+
let selectors = [selector];
|
|
205
|
+
if(selector.indexOf(',') !== -1){
|
|
206
|
+
selectors = selector.split(',');
|
|
207
|
+
}
|
|
208
|
+
for (let selector of selectors){
|
|
209
|
+
let el;
|
|
210
|
+
if(selector.indexOf(';') !== -1) {
|
|
211
|
+
let targetDocument;
|
|
212
|
+
let [documentSelector, targetSelector] = selector.split(';');
|
|
213
|
+
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
214
|
+
targetDocument = window.parent.document;
|
|
215
|
+
else {
|
|
216
|
+
let frame = document.querySelector(documentSelector);
|
|
217
|
+
if (frame)
|
|
218
|
+
targetDocument = frame.contentDocument;
|
|
219
|
+
}
|
|
220
|
+
if (targetDocument){
|
|
221
|
+
if (targetSelector)
|
|
222
|
+
el = targetDocument.querySelector(targetSelector);
|
|
223
|
+
else
|
|
224
|
+
if (targetDocument.clickedElement)
|
|
225
|
+
el = [targetDocument.clickedElement];
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
else
|
|
229
|
+
el = document.querySelector(selector);
|
|
230
|
+
if (el)
|
|
231
|
+
return el
|
|
232
|
+
}
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
162
237
|
// export function computeStyles(el, properties) {
|
|
163
238
|
// let computed = window.getComputedStyle(el);
|
|
164
239
|
// let result = {};
|
|
@@ -184,7 +259,9 @@ clickedElement();
|
|
|
184
259
|
|
|
185
260
|
export default {
|
|
186
261
|
parseTextToHtml,
|
|
262
|
+
dotNotationToObject,
|
|
187
263
|
cssPath,
|
|
188
264
|
domParser,
|
|
265
|
+
queryDocumentSelector,
|
|
189
266
|
queryDocumentSelectorAll
|
|
190
267
|
};
|
package/src/index.old.js
CHANGED
|
@@ -425,7 +425,24 @@ async function complexSelector(comSelector, callback) {
|
|
|
425
425
|
}
|
|
426
426
|
}
|
|
427
427
|
|
|
428
|
-
|
|
428
|
+
function decodeArray(data) {
|
|
429
|
+
let keys = Object.keys(data);
|
|
430
|
+
let objectData = {};
|
|
431
|
+
|
|
432
|
+
keys.forEach((k) => {
|
|
433
|
+
let nk = k
|
|
434
|
+
if (/\[([0-9]*)\]/g.test(k)) {
|
|
435
|
+
nk = nk.replace(/\[/g, '.');
|
|
436
|
+
if (nk.endsWith(']'))
|
|
437
|
+
nk = nk.slice(0, -1)
|
|
438
|
+
nk = nk.replace(/\]./g, '.');
|
|
439
|
+
nk = nk.replace(/\]/g, '.');
|
|
440
|
+
}
|
|
441
|
+
objectData[nk] = data[k];
|
|
442
|
+
});
|
|
443
|
+
return objectData;
|
|
444
|
+
}
|
|
445
|
+
|
|
429
446
|
|
|
430
447
|
export default {
|
|
431
448
|
getElementPath,
|