@domql/utils 2.5.117 → 2.5.123

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 CHANGED
@@ -67,26 +67,6 @@ export const cutArrayAfterValue = (arr, value) => {
67
67
  return arr
68
68
  }
69
69
 
70
- export const createNestedObject = (arr, lastValue) => {
71
- const nestedObject = {}
72
-
73
- if (arr.length === 0) {
74
- return lastValue
75
- }
76
-
77
- arr.reduce((obj, value, index) => {
78
- if (!obj[value]) {
79
- obj[value] = {}
80
- }
81
- if (index === arr.length - 1 && lastValue) {
82
- obj[value] = lastValue
83
- }
84
- return obj[value]
85
- }, nestedObject)
86
-
87
- return nestedObject
88
- }
89
-
90
70
  export const removeValueFromArray = (arr, value) => {
91
71
  const index = arr.indexOf(value)
92
72
  if (index > -1) {
package/dist/cjs/array.js CHANGED
@@ -21,7 +21,6 @@ __export(array_exports, {
21
21
  addItemAfterEveryElement: () => addItemAfterEveryElement,
22
22
  arrayContainsOtherArray: () => arrayContainsOtherArray,
23
23
  arraysEqual: () => arraysEqual,
24
- createNestedObject: () => createNestedObject,
25
24
  cutArrayAfterValue: () => cutArrayAfterValue,
26
25
  cutArrayBeforeValue: () => cutArrayBeforeValue,
27
26
  getFrequencyInArray: () => getFrequencyInArray,
@@ -88,22 +87,6 @@ const cutArrayAfterValue = (arr, value) => {
88
87
  }
89
88
  return arr;
90
89
  };
91
- const createNestedObject = (arr, lastValue) => {
92
- const nestedObject = {};
93
- if (arr.length === 0) {
94
- return lastValue;
95
- }
96
- arr.reduce((obj, value, index) => {
97
- if (!obj[value]) {
98
- obj[value] = {};
99
- }
100
- if (index === arr.length - 1 && lastValue) {
101
- obj[value] = lastValue;
102
- }
103
- return obj[value];
104
- }, nestedObject);
105
- return nestedObject;
106
- };
107
90
  const removeValueFromArray = (arr, value) => {
108
91
  const index = arr.indexOf(value);
109
92
  if (index > -1) {
@@ -18,6 +18,7 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var function_exports = {};
20
20
  __export(function_exports, {
21
+ cloneFunction: () => cloneFunction,
21
22
  debounce: () => debounce,
22
23
  debounceOnContext: () => debounceOnContext,
23
24
  isStringFunction: () => isStringFunction,
@@ -67,3 +68,14 @@ const isStringFunction = (inputString) => {
67
68
  const functionRegex = /^((function\s*\([^)]*\)\s*\{[^}]*\})|(\([^)]*\)\s*=>))/;
68
69
  return functionRegex.test(inputString);
69
70
  };
71
+ function cloneFunction(fn, win = window) {
72
+ const temp = function() {
73
+ return fn.apply(win, arguments);
74
+ };
75
+ for (const key in fn) {
76
+ if (Object.hasOwnProperty.call(fn, key)) {
77
+ temp[key] = fn[key];
78
+ }
79
+ }
80
+ return temp;
81
+ }
@@ -20,6 +20,7 @@ var object_exports = {};
20
20
  __export(object_exports, {
21
21
  checkIfKeyIsComponent: () => checkIfKeyIsComponent,
22
22
  clone: () => clone,
23
+ createNestedObject: () => createNestedObject,
23
24
  createObjectWithoutPrototype: () => createObjectWithoutPrototype,
24
25
  deepClone: () => deepClone,
25
26
  deepCloneExclude: () => deepCloneExclude,
@@ -51,6 +52,7 @@ __export(object_exports, {
51
52
  overwriteDeep: () => overwriteDeep,
52
53
  overwriteShallow: () => overwriteShallow,
53
54
  removeFromObject: () => removeFromObject,
55
+ removeNestedKeyByPath: () => removeNestedKeyByPath,
54
56
  stringToObject: () => stringToObject
55
57
  });
56
58
  module.exports = __toCommonJS(object_exports);
@@ -59,6 +61,7 @@ var import_types = require("./types.js");
59
61
  var import_array = require("./array.js");
60
62
  var import_string = require("./string.js");
61
63
  var import_node = require("./node.js");
64
+ var import_function = require("./function.js");
62
65
  const exec = (param, element, state, context) => {
63
66
  if ((0, import_types.isFunction)(param)) {
64
67
  return param(
@@ -170,6 +173,8 @@ const deepCloneWithExtend = (obj, excludeFrom = ["node"], options = {}) => {
170
173
  continue;
171
174
  if ((0, import_types.isObjectLike)(objProp)) {
172
175
  o[prop] = deepCloneWithExtend(objProp, excludeFrom, options);
176
+ } else if ((0, import_types.isFunction)(objProp) && options.window) {
177
+ o[prop] = (0, import_function.cloneFunction)(objProp, options.window);
173
178
  } else
174
179
  o[prop] = objProp;
175
180
  }
@@ -413,16 +418,23 @@ const overwriteShallow = (obj, params, excludeFrom = []) => {
413
418
  }
414
419
  return obj;
415
420
  };
416
- const overwriteDeep = (obj, params, excludeFrom = []) => {
421
+ const overwriteDeep = (obj, params, excludeFrom = [], visited = /* @__PURE__ */ new WeakMap()) => {
422
+ if (!(0, import_types.isObjectLike)(obj) || !(0, import_types.isObjectLike)(params) || (0, import_node.isDOMNode)(obj) || (0, import_node.isDOMNode)(params)) {
423
+ return params;
424
+ }
425
+ if (visited.has(obj)) {
426
+ return visited.get(obj);
427
+ }
428
+ visited.set(obj, obj);
417
429
  for (const e in params) {
418
- if (e === "__ref")
419
- continue;
420
- if (excludeFrom.includes(e) || e.startsWith("__"))
430
+ if (e === "__ref" || excludeFrom.includes(e) || e.startsWith("__"))
421
431
  continue;
422
432
  const objProp = obj[e];
423
433
  const paramsProp = params[e];
424
- if ((0, import_types.isObjectLike)(objProp) && (0, import_types.isObjectLike)(paramsProp)) {
425
- overwriteDeep(objProp, paramsProp);
434
+ if ((0, import_node.isDOMNode)(paramsProp)) {
435
+ obj[e] = paramsProp;
436
+ } else if ((0, import_types.isObjectLike)(objProp) && (0, import_types.isObjectLike)(paramsProp)) {
437
+ obj[e] = overwriteDeep(objProp, paramsProp, excludeFrom, visited);
426
438
  } else if (paramsProp !== void 0) {
427
439
  obj[e] = paramsProp;
428
440
  }
@@ -578,3 +590,35 @@ const getExtendsInElement = (obj) => {
578
590
  traverse(obj);
579
591
  return result;
580
592
  };
593
+ const createNestedObject = (arr, lastValue) => {
594
+ const nestedObject = {};
595
+ if (arr.length === 0) {
596
+ return lastValue;
597
+ }
598
+ arr.reduce((obj, value, index) => {
599
+ if (!obj[value]) {
600
+ obj[value] = {};
601
+ }
602
+ if (index === arr.length - 1 && lastValue) {
603
+ obj[value] = lastValue;
604
+ }
605
+ return obj[value];
606
+ }, nestedObject);
607
+ return nestedObject;
608
+ };
609
+ const removeNestedKeyByPath = (obj, path) => {
610
+ if (!Array.isArray(path)) {
611
+ throw new Error("Path must be an array.");
612
+ }
613
+ let current = obj;
614
+ for (let i = 0; i < path.length - 1; i++) {
615
+ if (current[path[i]] === void 0) {
616
+ return;
617
+ }
618
+ current = current[path[i]];
619
+ }
620
+ const lastKey = path[path.length - 1];
621
+ if (current && Object.hasOwnProperty.call(current, lastKey)) {
622
+ delete current[lastKey];
623
+ }
624
+ };
package/function.js CHANGED
@@ -71,3 +71,17 @@ export const isStringFunction = inputString => {
71
71
  // Use the regex to test if the inputString matches the function pattern
72
72
  return functionRegex.test(inputString)
73
73
  }
74
+
75
+ export function cloneFunction (fn, win = window) {
76
+ const temp = function () {
77
+ return fn.apply(win, arguments)
78
+ }
79
+
80
+ // Copy properties from original function
81
+ for (const key in fn) {
82
+ if (Object.hasOwnProperty.call(fn, key)) {
83
+ temp[key] = fn[key]
84
+ }
85
+ }
86
+ return temp
87
+ }
package/object.js CHANGED
@@ -5,6 +5,7 @@ import { isFunction, isObjectLike, isObject, isArray, isString, is, isUndefined,
5
5
  import { mergeAndCloneIfArray, mergeArray } from './array.js'
6
6
  import { stringIncludesAny } from './string.js'
7
7
  import { isDOMNode } from './node.js'
8
+ import { cloneFunction } from './function.js'
8
9
 
9
10
  export const exec = (param, element, state, context) => {
10
11
  if (isFunction(param)) {
@@ -176,6 +177,8 @@ export const deepCloneWithExtend = (obj, excludeFrom = ['node'], options = {}) =
176
177
  // queueMicrotask(() => {
177
178
  o[prop] = deepCloneWithExtend(objProp, excludeFrom, options)
178
179
  // })
180
+ } else if (isFunction(objProp) && options.window) {
181
+ o[prop] = cloneFunction(objProp, options.window)
179
182
  } else o[prop] = objProp
180
183
  }
181
184
  return o
@@ -460,18 +463,32 @@ export const overwriteShallow = (obj, params, excludeFrom = []) => {
460
463
  /**
461
464
  * Overwrites DEEPLY object properties with another
462
465
  */
463
- export const overwriteDeep = (obj, params, excludeFrom = []) => {
466
+ export const overwriteDeep = (obj, params, excludeFrom = [], visited = new WeakMap()) => {
467
+ if (!isObjectLike(obj) || !isObjectLike(params) || isDOMNode(obj) || isDOMNode(params)) {
468
+ return params
469
+ }
470
+
471
+ if (visited.has(obj)) {
472
+ return visited.get(obj)
473
+ }
474
+
475
+ visited.set(obj, obj)
476
+
464
477
  for (const e in params) {
465
- if (e === '__ref') continue
466
- if (excludeFrom.includes(e) || e.startsWith('__')) continue
478
+ if (e === '__ref' || excludeFrom.includes(e) || e.startsWith('__')) continue
479
+
467
480
  const objProp = obj[e]
468
481
  const paramsProp = params[e]
469
- if (isObjectLike(objProp) && isObjectLike(paramsProp)) {
470
- overwriteDeep(objProp, paramsProp)
482
+
483
+ if (isDOMNode(paramsProp)) {
484
+ obj[e] = paramsProp
485
+ } else if (isObjectLike(objProp) && isObjectLike(paramsProp)) {
486
+ obj[e] = overwriteDeep(objProp, paramsProp, excludeFrom, visited)
471
487
  } else if (paramsProp !== undefined) {
472
488
  obj[e] = paramsProp
473
489
  }
474
490
  }
491
+
475
492
  return obj
476
493
  }
477
494
 
@@ -700,3 +717,43 @@ export const getExtendsInElement = (obj) => {
700
717
  traverse(obj)
701
718
  return result
702
719
  }
720
+
721
+ export const createNestedObject = (arr, lastValue) => {
722
+ const nestedObject = {}
723
+
724
+ if (arr.length === 0) {
725
+ return lastValue
726
+ }
727
+
728
+ arr.reduce((obj, value, index) => {
729
+ if (!obj[value]) {
730
+ obj[value] = {}
731
+ }
732
+ if (index === arr.length - 1 && lastValue) {
733
+ obj[value] = lastValue
734
+ }
735
+ return obj[value]
736
+ }, nestedObject)
737
+
738
+ return nestedObject
739
+ }
740
+
741
+ export const removeNestedKeyByPath = (obj, path) => {
742
+ if (!Array.isArray(path)) {
743
+ throw new Error('Path must be an array.')
744
+ }
745
+
746
+ let current = obj
747
+
748
+ for (let i = 0; i < path.length - 1; i++) {
749
+ if (current[path[i]] === undefined) {
750
+ return // Path does not exist, so nothing to remove.
751
+ }
752
+ current = current[path[i]]
753
+ }
754
+
755
+ const lastKey = path[path.length - 1]
756
+ if (current && Object.hasOwnProperty.call(current, lastKey)) {
757
+ delete current[lastKey]
758
+ }
759
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.5.117",
3
+ "version": "2.5.123",
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": "56be344ba28e3cb51c146c1b74fe3c0e21341879",
26
+ "gitHead": "6dd041deb89bc2ebfa1f5ee9360d563df60eca13",
27
27
  "devDependencies": {
28
28
  "@babel/core": "^7.12.0"
29
29
  }