@szegedsw/lib-node
Version:
A little framework published by Szeged Software Zrt. in order to enhance api endpoint security and create reuseable code. Email module, Logging system, and much more. Further improvements are expected.
36 lines • 1.25 kB
JavaScript
;
global.number = (value) => {
if (typeof value === "boolean") {
return value ? 1 : 0;
}
if (!Number.isNaN(Number(value))) {
return Number(value) === 1 ? 1 : 0;
}
// eslint-disable-next-line no-nested-ternary
return value === undefined ? -1 : value === "true" ? 1 : 0;
};
global.bool = (value) => global.number(value) === 1;
global.toArrayUpdate = (object) => {
const update = { $addToSet: {}, $pull: {} };
Object.keys(object).forEach((key) => {
Object.keys(object[key]).forEach((item) => {
const type = object[key][item] ? "$addToSet" : "$pull";
const modifier = type === "$addToSet" ? "$each" : "$in";
if (!update[type][key]) {
update[type][key] = modifier === "$each" ? { $each: [] } : { $in: [] };
}
update[type][key][modifier].push(item);
});
});
return update;
};
global.removeUndefined = (object, remove = ["_id"]) => {
const cleanObject = {};
Object.keys(object).forEach((key) => {
if (object[key] !== undefined && !remove.includes(key)) {
cleanObject[key] = object[key];
}
});
return cleanObject;
};
//# sourceMappingURL=global.ext.js.map