@modern-kit/utils
Version:
45 lines (41 loc) • 1.49 kB
JavaScript
;
var validatorHasProperty = require('../../validator/hasProperty/index.cjs');
var validatorIsArray = require('../../validator/isArray/index.cjs');
var validatorIsReference = require('../../validator/isReference/index.cjs');
require('../../validator/isPrimitive/index.cjs');
function deleteFalsyProperties(source) {
const copiedObj = {};
for (const key in source) {
if (validatorHasProperty.hasProperty(source, key)) {
const value = source[key];
if (validatorIsReference.isReference(value)) {
if (!validatorIsArray.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;
}
exports.deleteFalsyProperties = deleteFalsyProperties;
//# sourceMappingURL=index.cjs.map