@icodebible/utils
Version:
Collection of essential utilities that help manipulation and transformation of various js object.
69 lines (68 loc) • 2.09 kB
JavaScript
/**
*
*/
const _ = require('lodash');
const { isArray, isObject } = require('../../helpers');
/**
*
*/
exports.ObjectPayloadUpdater = (payload, updates) => {
/**
* Declaration of new sanitized Object
*/
let mPayload = {};
/**
*
*/
if (payload) {
/**
*
*/
_.forEach(_.keys(payload), key => {
/**
* Check if the Object passed is Array or not
*/
if (payload[key] && isArray(payload[key])) {
/**
* Return updated Array with new updates
*/
const arr = payload[key];
const updatesArr = _.has(updates, key) ? updates[key] : [];
mPayload = {
...mPayload,
[]: _.uniqBy(_.unionBy(arr, updatesArr, 'uid'), 'uid'),
};
/**
* Check if the Object passed is Object or not
*/
} else if (payload[key] && isObject(payload[key])) {
/**
* Return updated Object with new updates
*/
const obj = payload[key];
const updatesObj = _.has(updates, key) ? updates[key] : null;
mPayload = {
...mPayload,
[]: _.merge(obj, updatesObj),
};
} else {
/**
* Return updated Global Object with new updates
*/
mPayload = {
...mPayload,
[]: _.has(updates, key) ? updates[key] : payload[key],
};
}
});
/**
* Return updated Object with all new updates to all levels
*/
return mPayload;
} else {
/**
* Return message no payload passed to be sanitized
*/
return 'No payload supplied';
}
};