@devtron-labs/devtron-fe-common-lib 0.0.59 → 0.0.60

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.
@@ -55,7 +55,10 @@ export declare const customStyles: {
55
55
  dropdownIndicator: (base: any, state: any) => any;
56
56
  };
57
57
  export declare const getFilteredChartVersions: (charts: any, selectedChartType: any) => any;
58
- export declare function getUnlockedJSON(json: any, jsonPathArray: any): any;
58
+ export declare function getUnlockedJSON(json: any, jsonPathArray: any, removeParentKeysAndEmptyArrays?: boolean): {
59
+ newDocument: any;
60
+ removedPatches: any[];
61
+ };
59
62
  export declare function getLockedJSON(json: any, jsonPathArray: string[]): any;
60
63
  /**
61
64
  * Returns a debounced variant of the function
package/dist/index.js CHANGED
@@ -11804,42 +11804,59 @@ var getFilteredChartVersions = function (charts, selectedChartType) {
11804
11804
  chartRefId: item.chartRefId,
11805
11805
  }); });
11806
11806
  };
11807
- function removeEmptyObjectKeysAndNullValues(obj) {
11808
- // It recursively removes empty object keys and array values that are null
11809
- for (var key in obj) {
11807
+ function removeEmptyObjectKeysAndNullValues(obj, originaljsonCopy) {
11808
+ var _loop_1 = function (key) {
11810
11809
  if (Array.isArray(obj[key])) {
11811
- if (obj[key].length === 0)
11812
- continue;
11813
- obj[key] = obj[key].filter(function (item) { return item !== null; });
11814
11810
  // Check if the array is empty
11815
- if (obj[key].length === 0) {
11816
- delete obj[key]; // Delete the key if the array is empty
11811
+ if (obj[key].length !== 0) {
11812
+ obj[key].forEach(function (element, index) {
11813
+ if (element === null || (typeof element === 'object' && Object.keys(element).length === 0))
11814
+ obj[key].splice(index, 1);
11815
+ else
11816
+ removeEmptyObjectKeysAndNullValues(element, originaljsonCopy[key][index]);
11817
+ });
11818
+ }
11819
+ if (obj[key].length === 0 && originaljsonCopy[key].length !== 0) {
11820
+ delete obj[key];
11817
11821
  }
11818
11822
  }
11819
11823
  else if (obj[key] && typeof obj[key] === 'object') {
11820
- if (removeEmptyObjectKeysAndNullValues(obj[key])) {
11824
+ if (removeEmptyObjectKeysAndNullValues(obj[key], originaljsonCopy[key]) &&
11825
+ Object.keys(originaljsonCopy[key]).length !== 0) {
11821
11826
  delete obj[key];
11822
11827
  }
11823
11828
  }
11824
11829
  else if (obj[key] === undefined) {
11825
11830
  delete obj[key];
11826
11831
  }
11832
+ };
11833
+ // It recursively removes empty object keys and empty array keys
11834
+ for (var key in obj) {
11835
+ _loop_1(key);
11827
11836
  }
11828
11837
  return Object.keys(obj).length === 0;
11829
11838
  }
11830
- function getUnlockedJSON(json, jsonPathArray) {
11839
+ function getUnlockedJSON(json, jsonPathArray, removeParentKeysAndEmptyArrays) {
11840
+ if (removeParentKeysAndEmptyArrays === void 0) { removeParentKeysAndEmptyArrays = false; }
11841
+ if (!jsonPathArray.length)
11842
+ return { newDocument: json, removedPatches: [] };
11831
11843
  var jsonCopy = JSON.parse(JSON.stringify(json));
11844
+ var originaljsonCopy = JSON.parse(JSON.stringify(json));
11845
+ var removedPatches = [];
11832
11846
  var patches = jsonPathArray.flatMap(function (jsonPath) {
11833
11847
  var pathsToRemove = JSONPath({ path: jsonPath, json: jsonCopy, resultType: 'all' });
11848
+ //reversing patches to handle correct array index deletion
11849
+ pathsToRemove.reverse();
11834
11850
  return pathsToRemove.map(function (result) {
11835
- return Array.isArray(result.parent)
11836
- ? { op: 'replace', path: result.pointer, value: null }
11837
- : { op: 'remove', path: result.pointer };
11851
+ //storing removed patches to have functionality of undo
11852
+ removedPatches.push({ op: 'add', path: result.pointer, value: result.value });
11853
+ return { op: 'remove', path: result.pointer };
11838
11854
  });
11839
11855
  });
11840
11856
  var newDocument = applyPatch(jsonCopy, patches).newDocument;
11841
- removeEmptyObjectKeysAndNullValues(newDocument);
11842
- return newDocument;
11857
+ if (removeParentKeysAndEmptyArrays)
11858
+ removeEmptyObjectKeysAndNullValues(newDocument, originaljsonCopy);
11859
+ return { newDocument: newDocument, removedPatches: removedPatches.reverse() };
11843
11860
  }
11844
11861
  function getLockedJSON(json, jsonPathArray) {
11845
11862
  var jsonCopy = JSON.parse(JSON.stringify(json));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devtron-labs/devtron-fe-common-lib",
3
- "version": "0.0.59",
3
+ "version": "0.0.60",
4
4
  "description": "Supporting common component library",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -499,18 +499,27 @@ export const getFilteredChartVersions = (charts, selectedChartType) => {
499
499
  chartRefId: item.chartRefId,
500
500
  }))
501
501
  }
502
- function removeEmptyObjectKeysAndNullValues(obj) {
503
- // It recursively removes empty object keys and array values that are null
502
+
503
+ function removeEmptyObjectKeysAndNullValues(obj, originaljsonCopy) {
504
+ // It recursively removes empty object keys and empty array keys
504
505
  for (let key in obj) {
505
506
  if (Array.isArray(obj[key])) {
506
- if (obj[key].length === 0) continue
507
- obj[key] = obj[key].filter((item) => item !== null)
508
507
  // Check if the array is empty
509
- if (obj[key].length === 0) {
510
- delete obj[key] // Delete the key if the array is empty
508
+ if (obj[key].length !== 0) {
509
+ obj[key].forEach((element, index) => {
510
+ if (element === null || (typeof element === 'object' && Object.keys(element).length === 0))
511
+ obj[key].splice(index, 1)
512
+ else removeEmptyObjectKeysAndNullValues(element, originaljsonCopy[key][index])
513
+ })
514
+ }
515
+ if (obj[key].length === 0 && originaljsonCopy[key].length !== 0) {
516
+ delete obj[key]
511
517
  }
512
518
  } else if (obj[key] && typeof obj[key] === 'object') {
513
- if (removeEmptyObjectKeysAndNullValues(obj[key])) {
519
+ if (
520
+ removeEmptyObjectKeysAndNullValues(obj[key], originaljsonCopy[key]) &&
521
+ Object.keys(originaljsonCopy[key]).length !== 0
522
+ ) {
514
523
  delete obj[key]
515
524
  }
516
525
  } else if (obj[key] === undefined) {
@@ -520,20 +529,25 @@ function removeEmptyObjectKeysAndNullValues(obj) {
520
529
  return Object.keys(obj).length === 0
521
530
  }
522
531
 
523
- export function getUnlockedJSON(json, jsonPathArray) {
532
+ export function getUnlockedJSON(json, jsonPathArray, removeParentKeysAndEmptyArrays = false) {
533
+ if (!jsonPathArray.length) return { newDocument: json, removedPatches: [] }
534
+
524
535
  const jsonCopy = JSON.parse(JSON.stringify(json))
536
+ const originaljsonCopy = JSON.parse(JSON.stringify(json))
537
+ let removedPatches = []
525
538
  let patches = jsonPathArray.flatMap((jsonPath) => {
526
539
  const pathsToRemove = JSONPath({ path: jsonPath, json: jsonCopy, resultType: 'all' })
527
- return pathsToRemove.map((result) =>
528
- Array.isArray(result.parent)
529
- ? { op: 'replace', path: result.pointer, value: null }
530
- : { op: 'remove', path: result.pointer },
531
- )
540
+ //reversing patches to handle correct array index deletion
541
+ pathsToRemove.reverse()
542
+ return pathsToRemove.map((result) => {
543
+ //storing removed patches to have functionality of undo
544
+ removedPatches.push({ op: 'add', path: result.pointer, value: result.value })
545
+ return { op: 'remove', path: result.pointer }
546
+ })
532
547
  })
533
548
  let newDocument = jsonpatch.applyPatch(jsonCopy, patches).newDocument
534
-
535
- removeEmptyObjectKeysAndNullValues(newDocument)
536
- return newDocument
549
+ if (removeParentKeysAndEmptyArrays) removeEmptyObjectKeysAndNullValues(newDocument, originaljsonCopy)
550
+ return { newDocument, removedPatches: removedPatches.reverse() }
537
551
  }
538
552
 
539
553
  export function getLockedJSON(json, jsonPathArray: string[]) {