Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock Config v2 #76

Merged
merged 9 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "0.0.55",
"version": "0.0.55-beta-6",
"description": "Supporting common component library",
"main": "dist/index.js",
"scripts": {
Expand Down
39 changes: 23 additions & 16 deletions src/Common/Helper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -499,18 +499,20 @@ export const getFilteredChartVersions = (charts, selectedChartType) => {
chartRefId: item.chartRefId,
}))
}
function removeEmptyObjectKeysAndNullValues(obj) {
// It recursively removes empty object keys and array values that are null

function removeEmptyObjectKeysAndNullValues(obj, originaljsonCopy) {
// It recursively removes empty object keys and empty array keys
for (let key in obj) {
if (Array.isArray(obj[key])) {
if (obj[key].length === 0) continue
obj[key] = obj[key].filter((item) => item !== null)
// Check if the array is empty
if (obj[key].length === 0) {
delete obj[key] // Delete the key if the array is empty
if (obj[key].length === 0 && originaljsonCopy[key].length !== 0) {
delete obj[key]
}
} else if (obj[key] && typeof obj[key] === 'object') {
if (removeEmptyObjectKeysAndNullValues(obj[key])) {
if (
removeEmptyObjectKeysAndNullValues(obj[key], originaljsonCopy[key]) &&
Object.keys(originaljsonCopy[key]).length !== 0
) {
delete obj[key]
}
} else if (obj[key] === undefined) {
Expand All @@ -520,20 +522,25 @@ function removeEmptyObjectKeysAndNullValues(obj) {
return Object.keys(obj).length === 0
}

export function getUnlockedJSON(json, jsonPathArray) {
export function getUnlockedJSON(json, jsonPathArray, removeParentKeysAndEmptyArrays = false) {
if (!jsonPathArray.length) return { newDocument: json, removedPatches: [] }

const jsonCopy = JSON.parse(JSON.stringify(json))
const originaljsonCopy = JSON.parse(JSON.stringify(json))
let removedPatches = []
let patches = jsonPathArray.flatMap((jsonPath) => {
const pathsToRemove = JSONPath({ path: jsonPath, json: jsonCopy, resultType: 'all' })
return pathsToRemove.map((result) =>
Array.isArray(result.parent)
? { op: 'replace', path: result.pointer, value: null }
: { op: 'remove', path: result.pointer },
)
//reversing patches to handle correct array index deletion
pathsToRemove.reverse()
return pathsToRemove.map((result) => {
//storing removed patches to have functionality of undo
removedPatches.push({ op: 'add', path: result.pointer, value: result.value })
return { op: 'remove', path: result.pointer }
})
})
let newDocument = jsonpatch.applyPatch(jsonCopy, patches).newDocument

removeEmptyObjectKeysAndNullValues(newDocument)
return newDocument
if (removeParentKeysAndEmptyArrays) removeEmptyObjectKeysAndNullValues(newDocument, originaljsonCopy)
return { newDocument, removedPatches: removedPatches.reverse() }
}

export function getLockedJSON(json, jsonPathArray: string[]) {
Expand Down