class Util {
constructor() { }
/**
* Desestruturar Objeto
*
* @param {*} object
* Objeto a ser desestruturado
*
* @param {*} arrayMap
* Mapa dos parametros a ser seguido
* Ex:
const foo = { a:1, b:2, c: { d: 'bar', e: 'abc' }, f: { g: 1, h: 2} }
const arrayMap = [
['a'],
['c', ['e']],
['f']
]
const destructuredObject = destructure(foo, arrayMap)
-> { a: 1, c: { e: 'abc' }, f: { g: 1, h: 2 }}
*/
destructure(object, arrayMap) {
if (object instanceof Array)
return object.map(item => this.destructure(item, arrayMap))
return arrayMap.reduce((init, key) => {
if (object[key[0]]) {
if (object[key[0]] instanceof Object && key[1])
init[key[0]] = this.destructure(object[key[0]], key[1])
else
init[key[0]] = object[key[0]]
}
return init
}, {})
}
}
export default new Util()