UNPKG

@modern-kit/utils

Version:
43 lines (40 loc) 1.37 kB
import { hasProperty } from '../../validator/hasProperty/index.mjs'; import { isArray } from '../../validator/isArray/index.mjs'; import { isReference } from '../../validator/isReference/index.mjs'; import '../../validator/isPrimitive/index.mjs'; function deleteFalsyProperties(source) { const copiedObj = {}; for (const key in source) { if (hasProperty(source, key)) { const value = source[key]; if (isReference(value)) { if (!isArray(value)) { const newObj = deleteFalsyProperties(value); const isNonEmptyObj = !!Object.keys(newObj).length; if (isNonEmptyObj) { copiedObj[key] = newObj; } continue; } const newArray = value.reduce((acc, cur) => { if (typeof cur !== "object") { acc.push(cur); return acc; } const property = deleteFalsyProperties(cur); const isNonEmptyObj = !!Object.keys(property).length; if (isNonEmptyObj) acc.push(property); return acc; }, []); if (newArray.length) { copiedObj[key] = newArray; } } else if (value || typeof value === "number" || typeof value === "boolean") { copiedObj[key] = value; } } } return copiedObj; } export { deleteFalsyProperties }; //# sourceMappingURL=index.mjs.map