@domql/utils 2.3.137 → 2.3.141
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/array.js +36 -0
- package/dist/cjs/array.js +33 -0
- package/dist/cjs/object.js +19 -4
- package/object.js +16 -4
- package/package.json +2 -2
package/array.js
CHANGED
|
@@ -43,3 +43,39 @@ export const mergeArray = (arr, excludeFrom = []) => {
|
|
|
43
43
|
export const mergeAndCloneIfArray = obj => {
|
|
44
44
|
return isArray(obj) ? mergeArray(obj) : deepClone(obj)
|
|
45
45
|
}
|
|
46
|
+
|
|
47
|
+
export const cutArrayBeforeValue = (arr, value) => {
|
|
48
|
+
const index = arr.indexOf(value);
|
|
49
|
+
if (index !== -1) {
|
|
50
|
+
return arr.slice(0, index);
|
|
51
|
+
}
|
|
52
|
+
return arr;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const cutArrayAfterValue = (arr, value) => {
|
|
56
|
+
const index = arr.indexOf(value);
|
|
57
|
+
if (index !== -1) {
|
|
58
|
+
return arr.slice(index + 1);
|
|
59
|
+
}
|
|
60
|
+
return arr;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const createNestedObject = (arr, lastValue) => {
|
|
64
|
+
let nestedObject = {};
|
|
65
|
+
|
|
66
|
+
if (arr.length === 0) {
|
|
67
|
+
return lastValue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
arr.reduce((obj, value, index) => {
|
|
71
|
+
if (!obj[value]) {
|
|
72
|
+
obj[value] = {};
|
|
73
|
+
}
|
|
74
|
+
if (index === arr.length - 1 && lastValue) {
|
|
75
|
+
obj[value] = lastValue;
|
|
76
|
+
}
|
|
77
|
+
return obj[value];
|
|
78
|
+
}, nestedObject);
|
|
79
|
+
|
|
80
|
+
return nestedObject;
|
|
81
|
+
}
|
package/dist/cjs/array.js
CHANGED
|
@@ -19,6 +19,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var array_exports = {};
|
|
20
20
|
__export(array_exports, {
|
|
21
21
|
arrayContainsOtherArray: () => arrayContainsOtherArray,
|
|
22
|
+
createNestedObject: () => createNestedObject,
|
|
23
|
+
cutArrayAfterValue: () => cutArrayAfterValue,
|
|
24
|
+
cutArrayBeforeValue: () => cutArrayBeforeValue,
|
|
22
25
|
joinArrays: () => joinArrays,
|
|
23
26
|
mergeAndCloneIfArray: () => mergeAndCloneIfArray,
|
|
24
27
|
mergeArray: () => mergeArray,
|
|
@@ -58,3 +61,33 @@ const mergeArray = (arr, excludeFrom = []) => {
|
|
|
58
61
|
const mergeAndCloneIfArray = (obj) => {
|
|
59
62
|
return (0, import_types.isArray)(obj) ? mergeArray(obj) : (0, import_object.deepClone)(obj);
|
|
60
63
|
};
|
|
64
|
+
const cutArrayBeforeValue = (arr, value) => {
|
|
65
|
+
const index = arr.indexOf(value);
|
|
66
|
+
if (index !== -1) {
|
|
67
|
+
return arr.slice(0, index);
|
|
68
|
+
}
|
|
69
|
+
return arr;
|
|
70
|
+
};
|
|
71
|
+
const cutArrayAfterValue = (arr, value) => {
|
|
72
|
+
const index = arr.indexOf(value);
|
|
73
|
+
if (index !== -1) {
|
|
74
|
+
return arr.slice(index + 1);
|
|
75
|
+
}
|
|
76
|
+
return arr;
|
|
77
|
+
};
|
|
78
|
+
const createNestedObject = (arr, lastValue) => {
|
|
79
|
+
let nestedObject = {};
|
|
80
|
+
if (arr.length === 0) {
|
|
81
|
+
return lastValue;
|
|
82
|
+
}
|
|
83
|
+
arr.reduce((obj, value, index) => {
|
|
84
|
+
if (!obj[value]) {
|
|
85
|
+
obj[value] = {};
|
|
86
|
+
}
|
|
87
|
+
if (index === arr.length - 1 && lastValue) {
|
|
88
|
+
obj[value] = lastValue;
|
|
89
|
+
}
|
|
90
|
+
return obj[value];
|
|
91
|
+
}, nestedObject);
|
|
92
|
+
return nestedObject;
|
|
93
|
+
};
|
package/dist/cjs/object.js
CHANGED
|
@@ -167,13 +167,28 @@ const objectToString = (obj, indent = 0) => {
|
|
|
167
167
|
const spaces = " ".repeat(indent);
|
|
168
168
|
let str = "{\n";
|
|
169
169
|
for (const [key, value] of Object.entries(obj)) {
|
|
170
|
-
const keyAllowdChars = (0, import_string.stringIncludesAny)(key, ["-", ":"]);
|
|
170
|
+
const keyAllowdChars = (0, import_string.stringIncludesAny)(key, ["-", ":", "@", ".", "!"]);
|
|
171
171
|
const stringedKey = keyAllowdChars ? `'${key}'` : key;
|
|
172
172
|
str += `${spaces} ${stringedKey}: `;
|
|
173
|
-
if (
|
|
173
|
+
if ((0, import_types.isArray)(value)) {
|
|
174
|
+
str += "[\n";
|
|
175
|
+
for (const element of value) {
|
|
176
|
+
if ((0, import_types.isObject)(element) && element !== null) {
|
|
177
|
+
str += `${spaces} ${objectToString(element, indent + 2)},
|
|
178
|
+
`;
|
|
179
|
+
} else if ((0, import_types.isString)(element)) {
|
|
180
|
+
str += `${spaces} '${element}',
|
|
181
|
+
`;
|
|
182
|
+
} else {
|
|
183
|
+
str += `${spaces} ${element},
|
|
184
|
+
`;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
str += `${spaces} ]`;
|
|
188
|
+
} else if ((0, import_types.isObject)(value)) {
|
|
174
189
|
str += objectToString(value, indent + 1);
|
|
175
|
-
} else if (
|
|
176
|
-
str += `'${value}'`;
|
|
190
|
+
} else if ((0, import_types.isString)(value)) {
|
|
191
|
+
str += (0, import_string.stringIncludesAny)(value, ["\n", "'"]) ? `\`${value}\`` : `'${value}'`;
|
|
177
192
|
} else {
|
|
178
193
|
str += value;
|
|
179
194
|
}
|
package/object.js
CHANGED
|
@@ -141,14 +141,26 @@ export const objectToString = (obj, indent = 0) => {
|
|
|
141
141
|
let str = '{\n'
|
|
142
142
|
|
|
143
143
|
for (const [key, value] of Object.entries(obj)) {
|
|
144
|
-
const keyAllowdChars = stringIncludesAny(key, ['-', ':'])
|
|
144
|
+
const keyAllowdChars = stringIncludesAny(key, ['-', ':', '@', '.', '!'])
|
|
145
145
|
const stringedKey = keyAllowdChars ? `'${key}'` : key
|
|
146
146
|
str += `${spaces} ${stringedKey}: `
|
|
147
147
|
|
|
148
|
-
if (
|
|
148
|
+
if (isArray(value)) {
|
|
149
|
+
str += '[\n'
|
|
150
|
+
for (const element of value) {
|
|
151
|
+
if (isObject(element) && element !== null) {
|
|
152
|
+
str += `${spaces} ${objectToString(element, indent + 2)},\n`
|
|
153
|
+
} else if (isString(element)) {
|
|
154
|
+
str += `${spaces} '${element}',\n`
|
|
155
|
+
} else {
|
|
156
|
+
str += `${spaces} ${element},\n`
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
str += `${spaces} ]`
|
|
160
|
+
} else if (isObject(value)) {
|
|
149
161
|
str += objectToString(value, indent + 1)
|
|
150
|
-
} else if (
|
|
151
|
-
str += `'${value}'`
|
|
162
|
+
} else if (isString(value)) {
|
|
163
|
+
str += stringIncludesAny(value, ['\n', '\'']) ? `\`${value}\`` : `'${value}'`
|
|
152
164
|
} else {
|
|
153
165
|
str += value
|
|
154
166
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domql/utils",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.141",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "index.js",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@domql/key": "latest",
|
|
24
24
|
"@domql/tags": "latest"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "ba9451d23df608f67ee1fd24de442cd558387202",
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@babel/core": "^7.12.0"
|
|
29
29
|
}
|