metamorphosi
Version:
Transform jsons using templates
45 lines • 1.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compact = void 0;
function isObject(val) {
return Object === val.constructor;
}
function compact($, keysThatMustExist = [], dropValues = [undefined], keep = false) {
if (Array.isArray($)) {
const enhancedDropValues = [...dropValues, '$keep'];
let keepEmpty = false;
$ = $.filter(o => {
if (o === '$keep')
keepEmpty = true;
return enhancedDropValues.indexOf(o) === -1;
});
if ($.length === 0 && keepEmpty)
return $;
if ($.length > 0)
return $;
}
else if ($ && isObject($)) {
if ((keysThatMustExist === null || keysThatMustExist === void 0 ? void 0 : keysThatMustExist.length) > 0) {
for (let key of keysThatMustExist) {
if (dropValues.indexOf($[key]) !== -1)
return;
}
}
const newObj = {};
for (key in $) {
if (dropValues.indexOf($[key]) === -1)
newObj[key] = $[key];
}
if (keep)
return newObj;
for (var key in newObj) {
if (dropValues.indexOf($[key]) === -1)
return newObj;
}
}
else if (dropValues.indexOf($) === -1) {
return $;
}
}
exports.compact = compact;
//# sourceMappingURL=compact.js.map