@domql/utils 2.3.126 → 2.3.138

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/index.js CHANGED
@@ -22,3 +22,4 @@ __reExport(utils_exports, require("./function.js"), module.exports);
22
22
  __reExport(utils_exports, require("./array.js"), module.exports);
23
23
  __reExport(utils_exports, require("./node.js"), module.exports);
24
24
  __reExport(utils_exports, require("./log.js"), module.exports);
25
+ __reExport(utils_exports, require("./string.js"), module.exports);
@@ -35,15 +35,18 @@ __export(object_exports, {
35
35
  merge: () => merge,
36
36
  mergeArrayExclude: () => mergeArrayExclude,
37
37
  mergeIfExisted: () => mergeIfExisted,
38
+ objectToString: () => objectToString,
38
39
  overwrite: () => overwrite,
39
40
  overwriteDeep: () => overwriteDeep,
40
41
  overwriteShallow: () => overwriteShallow,
41
- removeFromObject: () => removeFromObject
42
+ removeFromObject: () => removeFromObject,
43
+ stringToObject: () => stringToObject
42
44
  });
43
45
  module.exports = __toCommonJS(object_exports);
44
46
  var import_globals = require("@domql/globals");
45
47
  var import_types = require("./types.js");
46
48
  var import_array = require("./array.js");
49
+ var import_string = require("./string.js");
47
50
  const exec = (param, element, state, context) => {
48
51
  if ((0, import_types.isFunction)(param)) {
49
52
  return param(
@@ -160,6 +163,40 @@ const deepStringify = (obj, stringified = {}) => {
160
163
  }
161
164
  return stringified;
162
165
  };
166
+ const objectToString = (obj, indent = 0) => {
167
+ const spaces = " ".repeat(indent);
168
+ let str = "{\n";
169
+ for (const [key, value] of Object.entries(obj)) {
170
+ const keyAllowdChars = (0, import_string.stringIncludesAny)(key, ["-", ":", "@", ".", "!"]);
171
+ const stringedKey = keyAllowdChars ? `'${key}'` : key;
172
+ str += `${spaces} ${stringedKey}: `;
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)) {
189
+ str += objectToString(value, indent + 1);
190
+ } else if ((0, import_types.isString)(value)) {
191
+ str += (0, import_string.stringIncludesAny)(value, ["\n", "'"]) ? `\`${value}\`` : `'${value}'`;
192
+ } else {
193
+ str += value;
194
+ }
195
+ str += ",\n";
196
+ }
197
+ str += `${spaces}}`;
198
+ return str;
199
+ };
163
200
  const detachFunctionsFromObject = (obj, detached = {}) => {
164
201
  for (const prop in obj) {
165
202
  const objProp = obj[prop];
@@ -230,6 +267,16 @@ const deepDestringify = (obj, stringified = {}) => {
230
267
  }
231
268
  return stringified;
232
269
  };
270
+ const stringToObject = (str) => {
271
+ let obj;
272
+ try {
273
+ obj = import_globals.window.eval("(" + str + ")");
274
+ } catch (e) {
275
+ console.warn(e);
276
+ }
277
+ if (obj)
278
+ return obj;
279
+ };
233
280
  const diffObjects = (original, objToDiff, cache) => {
234
281
  for (const e in objToDiff) {
235
282
  if (e === "ref")
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var string_exports = {};
20
+ __export(string_exports, {
21
+ stringIncludesAny: () => stringIncludesAny
22
+ });
23
+ module.exports = __toCommonJS(string_exports);
24
+ const stringIncludesAny = (str, characters) => {
25
+ for (const char of characters) {
26
+ if (str.includes(char)) {
27
+ return true;
28
+ }
29
+ }
30
+ return false;
31
+ };
package/index.js CHANGED
@@ -7,3 +7,4 @@ export * from './function.js'
7
7
  export * from './array.js'
8
8
  export * from './node.js'
9
9
  export * from './log.js'
10
+ export * from './string.js'
package/object.js CHANGED
@@ -3,6 +3,7 @@
3
3
  import { window } from '@domql/globals'
4
4
  import { isFunction, isObjectLike, isObject, isArray, isString, is } from './types.js'
5
5
  import { mergeAndCloneIfArray, mergeArray } from './array.js'
6
+ import { stringIncludesAny } from './string.js'
6
7
 
7
8
  export const exec = (param, element, state, context) => {
8
9
  if (isFunction(param)) {
@@ -135,6 +136,42 @@ export const deepStringify = (obj, stringified = {}) => {
135
136
  return stringified
136
137
  }
137
138
 
139
+ export const objectToString = (obj, indent = 0) => {
140
+ const spaces = ' '.repeat(indent)
141
+ let str = '{\n'
142
+
143
+ for (const [key, value] of Object.entries(obj)) {
144
+ const keyAllowdChars = stringIncludesAny(key, ['-', ':', '@', '.', '!'])
145
+ const stringedKey = keyAllowdChars ? `'${key}'` : key
146
+ str += `${spaces} ${stringedKey}: `
147
+
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)) {
161
+ str += objectToString(value, indent + 1)
162
+ } else if (isString(value)) {
163
+ str += stringIncludesAny(value, ['\n', '\'']) ? `\`${value}\`` : `'${value}'`
164
+ } else {
165
+ str += value
166
+ }
167
+
168
+ str += ',\n'
169
+ }
170
+
171
+ str += `${spaces}}`
172
+ return str
173
+ }
174
+
138
175
  /**
139
176
  * Stringify object
140
177
  */
@@ -205,6 +242,15 @@ export const deepDestringify = (obj, stringified = {}) => {
205
242
  return stringified
206
243
  }
207
244
 
245
+ export const stringToObject = (str) => {
246
+ let obj
247
+ try {
248
+ obj = window.eval('(' + str + ')') // eslint-disable-line
249
+ } catch (e) { console.warn(e) }
250
+
251
+ if (obj) return obj
252
+ }
253
+
208
254
  export const diffObjects = (original, objToDiff, cache) => {
209
255
  for (const e in objToDiff) {
210
256
  if (e === 'ref') continue
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.3.126",
3
+ "version": "2.3.138",
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": "6329de71d2e831d0efd9d59e765567aa8c563481",
26
+ "gitHead": "066e799de6a79c164756c90b4d68e16b226523df",
27
27
  "devDependencies": {
28
28
  "@babel/core": "^7.12.0"
29
29
  }
package/string.js ADDED
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+
3
+ export const stringIncludesAny = (str, characters) => {
4
+ for (const char of characters) {
5
+ if (str.includes(char)) {
6
+ return true;
7
+ }
8
+ }
9
+ return false;
10
+ }