@domql/utils 2.5.13 → 2.5.16

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.
@@ -19,11 +19,29 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var function_exports = {};
20
20
  __export(function_exports, {
21
21
  debounce: () => debounce,
22
+ debounceOnContext: () => debounceOnContext,
22
23
  isStringFunction: () => isStringFunction,
23
24
  memoize: () => memoize
24
25
  });
25
26
  module.exports = __toCommonJS(function_exports);
26
- const debounce = (element, func, timeout = 300) => {
27
+ function debounce(func, wait, immediate) {
28
+ let timeout;
29
+ return function() {
30
+ const context = this;
31
+ const args = arguments;
32
+ const later = function() {
33
+ timeout = null;
34
+ if (!immediate)
35
+ func.apply(context, args);
36
+ };
37
+ const callNow = immediate && !timeout;
38
+ clearTimeout(timeout);
39
+ timeout = setTimeout(later, wait);
40
+ if (callNow)
41
+ func.apply(context, args);
42
+ };
43
+ }
44
+ const debounceOnContext = (element, func, timeout = 300) => {
27
45
  let timer;
28
46
  return (...args) => {
29
47
  clearTimeout(timer);
@@ -21,6 +21,7 @@ __export(object_exports, {
21
21
  clone: () => clone,
22
22
  deepClone: () => deepClone,
23
23
  deepCloneExclude: () => deepCloneExclude,
24
+ deepContains: () => deepContains,
24
25
  deepDestringify: () => deepDestringify,
25
26
  deepMerge: () => deepMerge,
26
27
  deepStringify: () => deepStringify,
@@ -173,8 +174,8 @@ const objectToString = (obj, indent = 0) => {
173
174
  const spaces = " ".repeat(indent);
174
175
  let str = "{\n";
175
176
  for (const [key, value] of Object.entries(obj)) {
176
- const keyAllowdChars = (0, import_string.stringIncludesAny)(key, ["-", ":", "@", ".", "!"]);
177
- const stringedKey = keyAllowdChars ? `'${key}'` : key;
177
+ const keyNotAllowdChars = (0, import_string.stringIncludesAny)(key, ["-", ":", "@", ".", "/", "!"]);
178
+ const stringedKey = keyNotAllowdChars ? `'${key}'` : key;
178
179
  str += `${spaces} ${stringedKey}: `;
179
180
  if ((0, import_types.isArray)(value)) {
180
181
  str += "[\n";
@@ -229,7 +230,7 @@ const detachFunctionsFromObject = (obj, detached = {}) => {
229
230
  }
230
231
  return detached;
231
232
  };
232
- const deepDestringify = (obj, stringified = {}) => {
233
+ const deepDestringify = (obj, destringified = {}) => {
233
234
  for (const prop in obj) {
234
235
  const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, prop);
235
236
  if (!hasOwnProperty)
@@ -239,52 +240,50 @@ const deepDestringify = (obj, stringified = {}) => {
239
240
  if (objProp.includes("=>") || objProp.includes("function") || objProp.startsWith("(")) {
240
241
  try {
241
242
  const evalProp = import_globals.window.eval(`(${objProp})`);
242
- stringified[prop] = evalProp;
243
+ destringified[prop] = evalProp;
243
244
  } catch (e) {
244
245
  if (e)
245
- stringified[prop] = objProp;
246
+ destringified[prop] = objProp;
246
247
  }
247
248
  } else {
248
- stringified[prop] = objProp;
249
+ destringified[prop] = objProp;
249
250
  }
250
251
  } else if ((0, import_types.isArray)(objProp)) {
251
- stringified[prop] = [];
252
+ destringified[prop] = [];
252
253
  objProp.forEach((arrProp) => {
253
254
  if ((0, import_types.isString)(arrProp)) {
254
255
  if (arrProp.includes("=>") || arrProp.includes("function") || arrProp.startsWith("(")) {
255
256
  try {
256
257
  const evalProp = import_globals.window.eval(`(${arrProp})`);
257
- stringified[prop].push(evalProp);
258
+ destringified[prop].push(evalProp);
258
259
  } catch (e) {
259
260
  if (e)
260
- stringified[prop].push(arrProp);
261
+ destringified[prop].push(arrProp);
261
262
  }
262
263
  } else {
263
- stringified[prop].push(arrProp);
264
+ destringified[prop].push(arrProp);
264
265
  }
265
266
  } else if ((0, import_types.isObject)(arrProp)) {
266
- stringified[prop].push(deepDestringify(arrProp));
267
+ destringified[prop].push(deepDestringify(arrProp));
267
268
  } else {
268
- stringified[prop].push(arrProp);
269
+ destringified[prop].push(arrProp);
269
270
  }
270
271
  });
271
272
  } else if ((0, import_types.isObject)(objProp)) {
272
- stringified[prop] = deepDestringify(objProp, stringified[prop]);
273
+ destringified[prop] = deepDestringify(objProp, destringified[prop]);
273
274
  } else {
274
- stringified[prop] = objProp;
275
+ destringified[prop] = objProp;
275
276
  }
276
277
  }
277
- return stringified;
278
+ return destringified;
278
279
  };
279
- const stringToObject = (str) => {
280
- let obj;
280
+ const stringToObject = (str, verbose) => {
281
281
  try {
282
- obj = import_globals.window.eval("(" + str + ")");
282
+ return import_globals.window.eval("(" + str + ")");
283
283
  } catch (e) {
284
- console.warn(e);
284
+ if (verbose)
285
+ console.warn(e);
285
286
  }
286
- if (obj)
287
- return obj;
288
287
  };
289
288
  const diffObjects = (original, objToDiff, cache) => {
290
289
  for (const e in objToDiff) {
@@ -406,6 +405,33 @@ const isEqualDeep = (param, element, visited = /* @__PURE__ */ new Set()) => {
406
405
  }
407
406
  return true;
408
407
  };
408
+ const deepContains = (obj1, obj2) => {
409
+ if (typeof obj1 !== typeof obj2) {
410
+ return false;
411
+ }
412
+ if ((0, import_types.isObjectLike)(obj1)) {
413
+ if (Array.isArray(obj1) && Array.isArray(obj2)) {
414
+ if (obj1.length !== obj2.length) {
415
+ return false;
416
+ }
417
+ for (let i = 0; i < obj1.length; i++) {
418
+ if (!deepContains(obj1[i], obj2[i])) {
419
+ return false;
420
+ }
421
+ }
422
+ } else if ((0, import_types.isObjectLike)(obj1) && obj2 !== null) {
423
+ for (const key in obj1) {
424
+ const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj2, key);
425
+ if (!hasOwnProperty || !deepContains(obj1[key], obj2[key])) {
426
+ return false;
427
+ }
428
+ }
429
+ }
430
+ } else {
431
+ return obj2 === obj1;
432
+ }
433
+ return true;
434
+ };
409
435
  const removeFromObject = (obj, props) => {
410
436
  if (props === void 0 || props === null)
411
437
  return obj;
package/function.js CHANGED
@@ -1,6 +1,48 @@
1
1
  'use strict'
2
2
 
3
- export const debounce = (element, func, timeout = 300) => {
3
+ /**
4
+ * Debounces a function, ensuring that it is only executed after a specified timeout
5
+ * period has elapsed since the last invocation.
6
+ *
7
+ * @param {function} func - The function to be debounced.
8
+ * @param {number} [timeout=300] - The time (in milliseconds) to wait after the last call to
9
+ * `debounce` before executing the `func`.
10
+ * @returns {function} - A debounced version of the input function `func`.
11
+ * @example
12
+ * // Usage example:
13
+ * const debouncedFunction = debounce(this, myFunction, 500);
14
+ * window.addEventListener('resize', debouncedFunction);
15
+ */
16
+ export function debounce (func, wait, immediate) {
17
+ let timeout
18
+ return function () {
19
+ const context = this; const args = arguments
20
+ const later = function () {
21
+ timeout = null
22
+ if (!immediate) func.apply(context, args)
23
+ }
24
+ const callNow = immediate && !timeout
25
+ clearTimeout(timeout)
26
+ timeout = setTimeout(later, wait)
27
+ if (callNow) func.apply(context, args)
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Debounces a function, ensuring that it is only executed after a specified timeout
33
+ * period has elapsed since the last invocation.
34
+ *
35
+ * @param {Object} element - The context (this) to which the debounced function will be applied.
36
+ * @param {function} func - The function to be debounced.
37
+ * @param {number} [timeout=300] - The time (in milliseconds) to wait after the last call to
38
+ * `debounce` before executing the `func`.
39
+ * @returns {function} - A debounced version of the input function `func`.
40
+ * @example
41
+ * // Usage example:
42
+ * const debouncedFunction = debounce(this, myFunction, 500);
43
+ * window.addEventListener('resize', debouncedFunction);
44
+ */
45
+ export const debounceOnContext = (element, func, timeout = 300) => {
4
46
  let timer
5
47
  return (...args) => {
6
48
  clearTimeout(timer)
package/object.js CHANGED
@@ -146,8 +146,8 @@ export const objectToString = (obj, indent = 0) => {
146
146
  let str = '{\n'
147
147
 
148
148
  for (const [key, value] of Object.entries(obj)) {
149
- const keyAllowdChars = stringIncludesAny(key, ['-', ':', '@', '.', '!'])
150
- const stringedKey = keyAllowdChars ? `'${key}'` : key
149
+ const keyNotAllowdChars = stringIncludesAny(key, ['-', ':', '@', '.', '/', '!'])
150
+ const stringedKey = keyNotAllowdChars ? `'${key}'` : key
151
151
  str += `${spaces} ${stringedKey}: `
152
152
 
153
153
  if (isArray(value)) {
@@ -208,7 +208,7 @@ export const detachFunctionsFromObject = (obj, detached = {}) => {
208
208
  /**
209
209
  * Detringify object
210
210
  */
211
- export const deepDestringify = (obj, stringified = {}) => {
211
+ export const deepDestringify = (obj, destringified = {}) => {
212
212
  for (const prop in obj) {
213
213
  const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, prop)
214
214
  if (!hasOwnProperty) continue
@@ -217,45 +217,42 @@ export const deepDestringify = (obj, stringified = {}) => {
217
217
  if (objProp.includes('=>') || objProp.includes('function') || objProp.startsWith('(')) {
218
218
  try {
219
219
  const evalProp = window.eval(`(${objProp})`) // use parentheses to convert string to function expression
220
- stringified[prop] = evalProp
221
- } catch (e) { if (e) stringified[prop] = objProp }
220
+ destringified[prop] = evalProp
221
+ } catch (e) { if (e) destringified[prop] = objProp }
222
222
  } else {
223
- stringified[prop] = objProp
223
+ destringified[prop] = objProp
224
224
  }
225
225
  } else if (isArray(objProp)) {
226
- stringified[prop] = []
226
+ destringified[prop] = []
227
227
  objProp.forEach((arrProp) => {
228
228
  if (isString(arrProp)) {
229
229
  if (arrProp.includes('=>') || arrProp.includes('function') || arrProp.startsWith('(')) {
230
230
  try {
231
231
  const evalProp = window.eval(`(${arrProp})`) // use parentheses to convert string to function expression
232
- stringified[prop].push(evalProp)
233
- } catch (e) { if (e) stringified[prop].push(arrProp) }
232
+ destringified[prop].push(evalProp)
233
+ } catch (e) { if (e) destringified[prop].push(arrProp) }
234
234
  } else {
235
- stringified[prop].push(arrProp)
235
+ destringified[prop].push(arrProp)
236
236
  }
237
237
  } else if (isObject(arrProp)) {
238
- stringified[prop].push(deepDestringify(arrProp))
238
+ destringified[prop].push(deepDestringify(arrProp))
239
239
  } else {
240
- stringified[prop].push(arrProp)
240
+ destringified[prop].push(arrProp)
241
241
  }
242
242
  })
243
243
  } else if (isObject(objProp)) {
244
- stringified[prop] = deepDestringify(objProp, stringified[prop]) // recursively call deepDestringify for nested objects
244
+ destringified[prop] = deepDestringify(objProp, destringified[prop]) // recursively call deepDestringify for nested objects
245
245
  } else {
246
- stringified[prop] = objProp
246
+ destringified[prop] = objProp
247
247
  }
248
248
  }
249
- return stringified
249
+ return destringified
250
250
  }
251
251
 
252
- export const stringToObject = (str) => {
253
- let obj
252
+ export const stringToObject = (str, verbose) => {
254
253
  try {
255
- obj = window.eval('(' + str + ')') // eslint-disable-line
256
- } catch (e) { console.warn(e) }
257
-
258
- if (obj) return obj
254
+ return window.eval('(' + str + ')') // eslint-disable-line
255
+ } catch (e) { if (verbose) console.warn(e) }
259
256
  }
260
257
 
261
258
  export const diffObjects = (original, objToDiff, cache) => {
@@ -449,6 +446,36 @@ export const isEqualDeep = (param, element, visited = new Set()) => {
449
446
  return true
450
447
  }
451
448
 
449
+ export const deepContains = (obj1, obj2) => {
450
+ if (typeof obj1 !== typeof obj2) {
451
+ return false
452
+ }
453
+
454
+ if (isObjectLike(obj1)) {
455
+ if (Array.isArray(obj1) && Array.isArray(obj2)) {
456
+ if (obj1.length !== obj2.length) {
457
+ return false
458
+ }
459
+ for (let i = 0; i < obj1.length; i++) {
460
+ if (!deepContains(obj1[i], obj2[i])) {
461
+ return false
462
+ }
463
+ }
464
+ } else if (isObjectLike(obj1) && obj2 !== null) {
465
+ for (const key in obj1) {
466
+ const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj2, key)
467
+ if (!hasOwnProperty || !deepContains(obj1[key], obj2[key])) {
468
+ return false
469
+ }
470
+ }
471
+ }
472
+ } else {
473
+ return obj2 === obj1
474
+ }
475
+
476
+ return true
477
+ }
478
+
452
479
  export const removeFromObject = (obj, props) => {
453
480
  if (props === undefined || props === null) return obj
454
481
  if (is(props)('string', 'number')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.5.13",
3
+ "version": "2.5.16",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "index.js",
@@ -23,7 +23,7 @@
23
23
  "build": "yarn build:cjs",
24
24
  "prepublish": "rimraf -I dist && yarn build && yarn copy:package:cjs"
25
25
  },
26
- "gitHead": "3bbff7f4e2ced6f104572489a2779a5cf0dbb450",
26
+ "gitHead": "2987c1965a4cab0258feb592d273d519260f5461",
27
27
  "devDependencies": {
28
28
  "@babel/core": "^7.12.0"
29
29
  }