Palash Gupta cf95d9c76f
fix: remove some of the frontend redundant packages (#2731)
* test: added few test cases for Tooltip

* fix: some of the stale frontend package are removed

* chore: flat package is removed

---------

Co-authored-by: Sanjib <sanjib.sah@yahoo.com>
2023-05-19 17:42:20 +05:30

35 lines
955 B
TypeScript

export const recursiveParseJSON = (obj: string): Record<string, unknown> => {
try {
const value = JSON.parse(obj);
if (typeof value === 'string') {
return recursiveParseJSON(value);
}
if (typeof value === 'object') {
Object.entries(value).forEach(([key, val]) => {
if (typeof val === 'string') {
value[key] = val.trim();
} else if (typeof val === 'object') {
value[key] = recursiveParseJSON(JSON.stringify(val));
}
});
}
return value;
} catch (e) {
return {};
}
};
type AnyObject = { [key: string]: any };
export function flattenObject(obj: AnyObject, prefix = ''): AnyObject {
return Object.keys(obj).reduce((acc: AnyObject, k: string): AnyObject => {
const pre = prefix.length ? `${prefix}.` : '';
if (typeof obj[k] === 'object' && obj[k] !== null && !Array.isArray(obj[k])) {
Object.assign(acc, flattenObject(obj[k], pre + k));
} else {
acc[pre + k] = obj[k];
}
return acc;
}, {});
}