Browse Source

Modify : apps\api\src\helper\object.helper.ts

pull/3953/head
Dhaneshwari Tendle 10 months ago
parent
commit
318907779f
  1. 86
      apps/api/src/helper/object.helper.ts

86
apps/api/src/helper/object.helper.ts

@ -1,35 +1,5 @@
import { Big } from 'big.js'; import { Big } from 'big.js';
import { cloneDeep, isArray, isObject } from 'lodash'; import { isArray, isObject } from 'lodash';
export function hasNotDefinedValuesInObject(aObject: Object): boolean {
for (const key in aObject) {
if (aObject[key] === null || aObject[key] === undefined) {
return true;
} else if (isObject(aObject[key])) {
return hasNotDefinedValuesInObject(aObject[key]);
}
}
return false;
}
export function nullifyValuesInObject<T>(aObject: T, keys: string[]): T {
const object = cloneDeep(aObject);
if (object) {
keys.forEach((key) => {
object[key] = null;
});
}
return object;
}
export function nullifyValuesInObjects<T>(aObjects: T[], keys: string[]): T[] {
return aObjects.map((object) => {
return nullifyValuesInObject(object, keys);
});
}
export function redactAttributes({ export function redactAttributes({
isFirstRun = true, isFirstRun = true,
@ -44,42 +14,38 @@ export function redactAttributes({
return object; return object;
} }
// Create deep clone const redactedObject = isFirstRun ? { ...object } : object;
const redactedObject = isFirstRun
? JSON.parse(JSON.stringify(object))
: object;
for (const option of options) { for (const option of options) {
if (redactedObject.hasOwnProperty(option.attribute)) { const { attribute, valueMap } = option;
if (option.valueMap['*'] || option.valueMap['*'] === null) {
redactedObject[option.attribute] = option.valueMap['*']; // Directly check and redact attributes
} else if (option.valueMap[redactedObject[option.attribute]]) { // Directly check and redact attributes
redactedObject[option.attribute] = if (redactedObject.hasOwnProperty(attribute)) {
option.valueMap[redactedObject[option.attribute]]; const value = redactedObject[attribute];
// Apply specific value or wildcard ('*')
if (valueMap.hasOwnProperty(value)) {
redactedObject[attribute] = valueMap[value];
} else if (valueMap.hasOwnProperty('*')) {
redactedObject[attribute] = valueMap['*'];
} }
} else { } else {
// If the attribute is not present on the current object, // Iterate over nested objects or arrays
// check if it exists on any nested objects for (const key in redactedObject) {
for (const property in redactedObject) { const prop = redactedObject[key];
if (isArray(redactedObject[property])) { if (isArray(prop)) {
redactedObject[property] = redactedObject[property].map( redactedObject[key] = prop.map((item) =>
(currentObject) => { redactAttributes({
return redactAttributes({ object: item,
options, options,
isFirstRun: false, isFirstRun: false
object: currentObject })
});
}
); );
} else if ( } else if (isObject(prop) && !(prop instanceof Big)) {
isObject(redactedObject[property]) && redactedObject[key] = redactAttributes({
!(redactedObject[property] instanceof Big) object: prop,
) {
// Recursively call the function on the nested object
redactedObject[property] = redactAttributes({
options, options,
isFirstRun: false, isFirstRun: false
object: redactedObject[property]
}); });
} }
} }

Loading…
Cancel
Save