@pipedream/freshdesk
Version:
Pipedream Freshdesk Components
55 lines (52 loc) • 1.28 kB
JavaScript
const removeNullEntries = (obj) =>
obj && Object.entries(obj).reduce((acc, [
key,
value,
]) => {
const isNumber = typeof value === "number";
const isBoolean = typeof value === "boolean";
const isNotEmpyString = typeof value === "string" && value.trim() !== "";
const isNotEmptyArray = Array.isArray(value) && value.length;
const isNotEmptyObject =
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
Object.keys(value).length !== 0;
isNotEmptyObject && (value = removeNullEntries(value));
return ((value || value === false) &&
(isNotEmpyString || isNotEmptyArray || isNotEmptyObject || isBoolean || isNumber))
? {
...acc,
[key]: value,
}
: acc;
}, {});
const parseObject = (obj) => {
if (!obj) {
return undefined;
}
if (typeof obj === "string") {
try {
return JSON.parse(obj);
} catch (e) {
return obj;
}
}
if (Array.isArray(obj)) {
return obj.map(parseObject);
}
if (typeof obj === "object") {
return Object.fromEntries(Object.entries(obj).map(([
key,
value,
]) => [
key,
parseObject(value),
]));
}
return obj;
};
export {
removeNullEntries,
parseObject,
};