-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1868 from IntersectMBO/fix/1601-junk-data-in-prot…
…ocol-parameter-change-governance-actions
- Loading branch information
Showing
13 changed files
with
445 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Filters out null parameters from the original object and returns a new object. | ||
* | ||
* @param originalObject - The original object to filter. | ||
* @param filterOutKeys - An optional array of keys to exclude from the filtering process. | ||
* @returns The filtered object without null parameters. | ||
*/ | ||
export const filterOutNullParams = ( | ||
originalObject?: Record<string, unknown> | undefined | null, | ||
filterOutKeys?: string[], | ||
) => { | ||
if (!originalObject) { | ||
return null; | ||
} | ||
|
||
const finalObject = Object.entries(originalObject).reduce( | ||
(acc: Record<string, unknown>, [key, value]) => { | ||
if ( | ||
value !== null && | ||
value !== undefined && | ||
!filterOutKeys?.includes(key) | ||
) { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, | ||
{}, | ||
); | ||
|
||
return finalObject; | ||
}; |
34 changes: 34 additions & 0 deletions
34
govtool/frontend/src/utils/filterUpdatableProtocolParams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Filters the updatable protocol parameters based on the original object, | ||
* reference object, and optional filter-out keys. | ||
* | ||
* @param originalObject - The original object containing the protocol parameters. | ||
* @param referenceObject - The reference object containing the updated protocol parameters. | ||
* @param filterOutKeys - An optional array of keys to be excluded from the final object. | ||
* @returns The filtered object containing the updatable protocol parameters or null. | ||
*/ | ||
export const filterUpdatableProtocolParams = ( | ||
originalObject?: Record<string, unknown> | null, | ||
referenceObject?: Record<string, unknown> | null, | ||
filterOutKeys?: string[], | ||
): Record<string, unknown> | null => { | ||
if (!originalObject || !referenceObject) { | ||
return null; | ||
} | ||
|
||
const finalObject = Object.entries(referenceObject).reduce< | ||
Record<string, unknown> | ||
>((acc, [key, referenceValue]) => { | ||
const isValid = | ||
!filterOutKeys?.includes(key) && | ||
originalObject.hasOwnProperty(key) && | ||
referenceValue !== undefined && | ||
referenceValue !== null; | ||
|
||
if (isValid) acc[key] = originalObject[key]; | ||
|
||
return acc; | ||
}, {}); | ||
|
||
return finalObject; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
govtool/frontend/src/utils/tests/filterOutNullParams.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { filterOutNullParams } from "../filterOutNullParams"; | ||
|
||
describe("filterOutNullParams", () => { | ||
it("should filter out null parameters", () => { | ||
const originalObject = { | ||
key1: "value1", | ||
key2: null, | ||
key3: "value3", | ||
key4: null, | ||
}; | ||
|
||
const expectedObject = { | ||
key1: "value1", | ||
key3: "value3", | ||
}; | ||
|
||
const result = filterOutNullParams(originalObject); | ||
|
||
expect(result).toEqual(expectedObject); | ||
}); | ||
|
||
it("should exclude keys from filtering process", () => { | ||
const originalObject = { | ||
key1: "value1", | ||
key2: null, | ||
key3: "value3", | ||
key4: null, | ||
}; | ||
|
||
const filterOutKeys = ["key2", "key4"]; | ||
|
||
const expectedObject = { | ||
key1: "value1", | ||
key3: "value3", | ||
}; | ||
|
||
const result = filterOutNullParams(originalObject, filterOutKeys); | ||
|
||
expect(result).toEqual(expectedObject); | ||
}); | ||
|
||
it("should return null if originalObject is null", () => { | ||
const originalObject = null; | ||
|
||
const result = filterOutNullParams(originalObject); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
it("should return null if originalObject is undefined", () => { | ||
const originalObject = undefined; | ||
|
||
const result = filterOutNullParams(originalObject); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
|
||
it("shouldn't filter out 0", () => { | ||
const originalObject = { | ||
key1: "value1", | ||
key2: 0, | ||
key3: "value3", | ||
key4: undefined, | ||
}; | ||
|
||
const expectedObject = { | ||
key1: "value1", | ||
key2: 0, | ||
key3: "value3", | ||
}; | ||
|
||
const result = filterOutNullParams(originalObject); | ||
|
||
expect(result).toEqual(expectedObject); | ||
}); | ||
}); |
101 changes: 101 additions & 0 deletions
101
govtool/frontend/src/utils/tests/filterUpdatableProtocolParams.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { filterUpdatableProtocolParams } from "../filterUpdatableProtocolParams"; | ||
|
||
describe("filterUpdatableProtocolParams", () => { | ||
it("should return only existing in reference object properties", () => { | ||
const originalParams = { | ||
id: 1, | ||
name: "John Doe", | ||
age: 30, | ||
email: "[email protected]", | ||
nonce: "abc123", | ||
}; | ||
|
||
const updateParams = { | ||
name: "Jane Doe", | ||
age: null, | ||
address: undefined, | ||
}; | ||
|
||
const expectedOutput = { | ||
name: "John Doe", | ||
}; | ||
|
||
const result = filterUpdatableProtocolParams(originalParams, updateParams); | ||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
it("should return only existing in reference object properties and filter out keys", () => { | ||
const originalParams = { | ||
id: 1, | ||
name: "John Doe", | ||
age: 30, | ||
email: "", | ||
nonce: "abc123", | ||
}; | ||
|
||
const updateParams = { | ||
id: 234, | ||
name: "Jane Doe", | ||
age: null, | ||
address: undefined, | ||
}; | ||
|
||
const expectedOutput = { | ||
name: "John Doe", | ||
}; | ||
|
||
const result = filterUpdatableProtocolParams(originalParams, updateParams, [ | ||
"id", | ||
]); | ||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
it("should not filter out 0 value from the reference object", () => { | ||
const originalParams = { | ||
id: 1, | ||
name: "John Doe", | ||
age: 30, | ||
email: "", | ||
nonce: "abc123", | ||
}; | ||
|
||
const updateParams = { | ||
id: 234, | ||
name: "Jane Doe", | ||
age: 0, | ||
address: undefined, | ||
}; | ||
|
||
const expectedOutput = { | ||
name: "John Doe", | ||
age: 30, | ||
}; | ||
|
||
const result = filterUpdatableProtocolParams(originalParams, updateParams, [ | ||
"id", | ||
]); | ||
expect(result).toEqual(expectedOutput); | ||
}); | ||
|
||
it("should return null if original object is not provided", () => { | ||
const updateParams = { | ||
name: "Jane Doe", | ||
age: null, | ||
address: undefined, | ||
}; | ||
|
||
const result = filterUpdatableProtocolParams(null, updateParams); | ||
expect(result).toBeNull(); | ||
}); | ||
|
||
it("should return null if update object is not provided", () => { | ||
const originalParams = { | ||
id: 1, | ||
name: "John Doe", | ||
age: 30, | ||
}; | ||
|
||
const result = filterUpdatableProtocolParams(originalParams, null); | ||
expect(result).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,5 +58,6 @@ | |
"ts-node": "^10.9.1", | ||
"tsconfig-paths": "^4.2.0", | ||
"typescript": "^5.1.3" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} |
Oops, something went wrong.