@domql/utils 2.5.177 → 2.5.179

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.
@@ -36,6 +36,7 @@ __export(object_exports, {
36
36
  excludeKeysFromObject: () => excludeKeysFromObject,
37
37
  exec: () => exec,
38
38
  flattenRecursive: () => flattenRecursive,
39
+ getInObjectByPath: () => getInObjectByPath,
39
40
  hasOwnProperty: () => hasOwnProperty,
40
41
  isCyclic: () => isCyclic,
41
42
  isEmpty: () => isEmpty,
@@ -52,6 +53,7 @@ __export(object_exports, {
52
53
  overwriteShallow: () => overwriteShallow,
53
54
  removeFromObject: () => removeFromObject,
54
55
  removeNestedKeyByPath: () => removeNestedKeyByPath,
56
+ setInObjectByPath: () => setInObjectByPath,
55
57
  stringToObject: () => stringToObject
56
58
  });
57
59
  module.exports = __toCommonJS(object_exports);
@@ -231,6 +233,9 @@ const deepStringifyWithMaxDepth = (obj, stringified = {}, depth = 0, path = "")
231
233
  return stringified;
232
234
  };
233
235
  const objectToString = (obj = {}, indent = 0) => {
236
+ if (obj === null || typeof obj !== "object") {
237
+ return String(obj);
238
+ }
234
239
  if (Object.keys(obj).length === 0) {
235
240
  return "{}";
236
241
  }
@@ -611,6 +616,34 @@ const removeNestedKeyByPath = (obj, path) => {
611
616
  delete current[lastKey];
612
617
  }
613
618
  };
619
+ const setInObjectByPath = (obj, path, value) => {
620
+ if (!Array.isArray(path)) {
621
+ throw new Error("Path must be an array.");
622
+ }
623
+ let current = obj;
624
+ for (let i = 0; i < path.length - 1; i++) {
625
+ if (!current[path[i]] || typeof current[path[i]] !== "object") {
626
+ current[path[i]] = {};
627
+ }
628
+ current = current[path[i]];
629
+ }
630
+ const lastKey = path[path.length - 1];
631
+ current[lastKey] = value;
632
+ return obj;
633
+ };
634
+ const getInObjectByPath = (obj, path) => {
635
+ if (!Array.isArray(path)) {
636
+ throw new Error("Path must be an array.");
637
+ }
638
+ let current = obj;
639
+ for (let i = 0; i < path.length; i++) {
640
+ if (current === void 0 || current === null) {
641
+ return void 0;
642
+ }
643
+ current = current[path[i]];
644
+ }
645
+ return current;
646
+ };
614
647
  const detectInfiniteLoop = (arr) => {
615
648
  const maxRepeats = 10;
616
649
  let pattern = [];
@@ -50,7 +50,7 @@ function replaceLiteralsWithObjectFields(str, options = {}, forcedState) {
50
50
  if (!str.includes(options.bracketsLength === 3 ? "{{{" : "{{"))
51
51
  return str;
52
52
  const reg = brackRegex[options.bracketsLength || 2];
53
- const obj = forcedState || this.state || {};
53
+ const obj = forcedState || (this == null ? void 0 : this.state) || {};
54
54
  return str.replace(reg, (_, parentPath, variable) => {
55
55
  if (parentPath) {
56
56
  const parentLevels = parentPath.match(options.bracketsLength === 3 ? /\.\.\.\//g : /\.\.\//g).length;
@@ -202,6 +202,9 @@ const deepStringifyWithMaxDepth = (obj, stringified = {}, depth = 0, path = "")
202
202
  return stringified;
203
203
  };
204
204
  const objectToString = (obj = {}, indent = 0) => {
205
+ if (obj === null || typeof obj !== "object") {
206
+ return String(obj);
207
+ }
205
208
  if (Object.keys(obj).length === 0) {
206
209
  return "{}";
207
210
  }
@@ -582,6 +585,34 @@ const removeNestedKeyByPath = (obj, path) => {
582
585
  delete current[lastKey];
583
586
  }
584
587
  };
588
+ const setInObjectByPath = (obj, path, value) => {
589
+ if (!Array.isArray(path)) {
590
+ throw new Error("Path must be an array.");
591
+ }
592
+ let current = obj;
593
+ for (let i = 0; i < path.length - 1; i++) {
594
+ if (!current[path[i]] || typeof current[path[i]] !== "object") {
595
+ current[path[i]] = {};
596
+ }
597
+ current = current[path[i]];
598
+ }
599
+ const lastKey = path[path.length - 1];
600
+ current[lastKey] = value;
601
+ return obj;
602
+ };
603
+ const getInObjectByPath = (obj, path) => {
604
+ if (!Array.isArray(path)) {
605
+ throw new Error("Path must be an array.");
606
+ }
607
+ let current = obj;
608
+ for (let i = 0; i < path.length; i++) {
609
+ if (current === void 0 || current === null) {
610
+ return void 0;
611
+ }
612
+ current = current[path[i]];
613
+ }
614
+ return current;
615
+ };
585
616
  const detectInfiniteLoop = (arr) => {
586
617
  const maxRepeats = 10;
587
618
  let pattern = [];
@@ -648,6 +679,7 @@ export {
648
679
  excludeKeysFromObject,
649
680
  exec,
650
681
  flattenRecursive,
682
+ getInObjectByPath,
651
683
  hasOwnProperty,
652
684
  isCyclic,
653
685
  isEmpty,
@@ -664,5 +696,6 @@ export {
664
696
  overwriteShallow,
665
697
  removeFromObject,
666
698
  removeNestedKeyByPath,
699
+ setInObjectByPath,
667
700
  stringToObject
668
701
  };
@@ -18,7 +18,7 @@ function replaceLiteralsWithObjectFields(str, options = {}, forcedState) {
18
18
  if (!str.includes(options.bracketsLength === 3 ? "{{{" : "{{"))
19
19
  return str;
20
20
  const reg = brackRegex[options.bracketsLength || 2];
21
- const obj = forcedState || this.state || {};
21
+ const obj = forcedState || (this == null ? void 0 : this.state) || {};
22
22
  return str.replace(reg, (_, parentPath, variable) => {
23
23
  if (parentPath) {
24
24
  const parentLevels = parentPath.match(options.bracketsLength === 3 ? /\.\.\.\//g : /\.\.\//g).length;
package/object.js CHANGED
@@ -238,6 +238,11 @@ export const deepStringifyWithMaxDepth = (obj, stringified = {}, depth = 0, path
238
238
  }
239
239
 
240
240
  export const objectToString = (obj = {}, indent = 0) => {
241
+ // Handle empty object case
242
+ if (obj === null || typeof obj !== 'object') {
243
+ return String(obj)
244
+ }
245
+
241
246
  // Handle empty object case
242
247
  if (Object.keys(obj).length === 0) {
243
248
  return '{}'
@@ -732,6 +737,44 @@ export const removeNestedKeyByPath = (obj, path) => {
732
737
  }
733
738
  }
734
739
 
740
+ export const setInObjectByPath = (obj, path, value) => {
741
+ if (!Array.isArray(path)) {
742
+ throw new Error('Path must be an array.')
743
+ }
744
+
745
+ let current = obj
746
+
747
+ for (let i = 0; i < path.length - 1; i++) {
748
+ // If the current path segment doesn't exist or isn't an object, create it
749
+ if (!current[path[i]] || typeof current[path[i]] !== 'object') {
750
+ current[path[i]] = {}
751
+ }
752
+ current = current[path[i]]
753
+ }
754
+
755
+ const lastKey = path[path.length - 1]
756
+ current[lastKey] = value
757
+
758
+ return obj
759
+ }
760
+
761
+ export const getInObjectByPath = (obj, path) => {
762
+ if (!Array.isArray(path)) {
763
+ throw new Error('Path must be an array.')
764
+ }
765
+
766
+ let current = obj
767
+
768
+ for (let i = 0; i < path.length; i++) {
769
+ if (current === undefined || current === null) {
770
+ return undefined
771
+ }
772
+ current = current[path[i]]
773
+ }
774
+
775
+ return current
776
+ }
777
+
735
778
  export const detectInfiniteLoop = arr => {
736
779
  const maxRepeats = 10 // Maximum allowed repetitions
737
780
  let pattern = []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domql/utils",
3
- "version": "2.5.177",
3
+ "version": "2.5.179",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "module": "index.js",
@@ -25,7 +25,7 @@
25
25
  "build": "npm run build:cjs; npm run build:esm",
26
26
  "prepublish": "rimraf -I dist; npm run build; npm run copy:package:cjs"
27
27
  },
28
- "gitHead": "faa8ed6c9ba30a49c81962cd3e16c0ffac833f4e",
28
+ "gitHead": "6dd81dc7848b03bd9bf564dbc82cd95af713ae21",
29
29
  "devDependencies": {
30
30
  "@babel/core": "^7.12.0"
31
31
  }
package/string.js CHANGED
@@ -32,7 +32,7 @@ const brackRegex = {
32
32
  export function replaceLiteralsWithObjectFields (str, options = {}, forcedState) {
33
33
  if (!str.includes(options.bracketsLength === 3 ? '{{{' : '{{')) return str
34
34
  const reg = brackRegex[options.bracketsLength || 2]
35
- const obj = forcedState || this.state || {}
35
+ const obj = forcedState || this?.state || {}
36
36
  return str.replace(reg, (_, parentPath, variable) => {
37
37
  if (parentPath) {
38
38
  const parentLevels = parentPath.match(options.bracketsLength === 3 ? /\.\.\.\//g : /\.\.\//g).length