UNPKG

@icodebible/utils

Version:

Collection of essential utilities that help manipulation and transformation of various js object.

116 lines (114 loc) 2.95 kB
/** * */ const _ = require('lodash'); const { isArray, isObject } = require('../helpers'); /** * Exported function that resolve all Object properties based on configuration passed */ exports.ObjectPropsResolver = (payload, config) => { /** * Declaration of new sanitized Object */ let mPayload = {}; /** * */ if (payload) { /** * */ _.forEach(_.keys(payload), key => { /** * Check if the Object passed is Array or not */ if (isArray(payload[key])) { /** * Return new Array with Object renamed keys based on configurations passed */ mPayload = { ...mPayload, [key]: getArrayOfRenamedObjectProp(payload[key], config), }; /** * Check if the Object passed is Object or not */ } else if (isObject(payload[key])) { /** * Return new Object with renamed keys based on configurations passed */ mPayload = { ...mPayload, [key]: ObjectPropsModifier(payload[key], config), }; } else { /** * Return renamed Global Object based on configuration passed */ mPayload = config && _.has(config, 'lookup') && _.includes(config.lookup, key) ? { ...mPayload, [config.entities[key]]: payload[key], } : { ...mPayload, [key]: payload[key] }; } }); /** * Returning Object after being modified based on configurations */ return mPayload; } else { /** * Return message no payload passed to be sanitized */ return 'No payload supplied'; } }; /** * * @param {*} arr * @param {*} config */ getArrayOfRenamedObjectProp = (arr, config) => { /** * */ if (isArray(arr)) { /** * Return Array of object already renamed */ return _.map(arr, obj => { /** * Return renamed Object properties */ return ObjectPropsModifier(obj, config); }); } }; /** * * @param {*} obj * @param {*} config */ ObjectPropsModifier = (obj, config) => { /** * */ return obj && config ? _.mapKeys(obj, (value, key) => { return config && _.has(config, 'lookup') && _.includes(config.lookup, key) ? [config.entities[key]] : [key]; }) : {}; };