{
profile: {
soma: 20,
menor: 1
}
}After getProperties
{
'profile.soma': 20,
'profile.menor': 1
}| const isObject = (value) => value && typeof value === 'object'; | |
| const isFilled = (value) => typeof value === 'string' && value.trim(); | |
| const reduceObject = (object, fn) => { | |
| const entries = Object.entries(object); | |
| const reduction = entries.reduce( | |
| (object, entry) => Object.assign({}, object,fn(...entry)), | |
| {} | |
| ); | |
| return reduction; | |
| }; | |
| const getProperties = (value, parent) => { | |
| const isValid = isObject(value) || isFilled(parent); | |
| if (!isValid) { | |
| throw new Error('Can\'t concat non-object values without parent.'); | |
| return; | |
| } | |
| if (!isFilled(parent)) { | |
| return reduceObject(value, (property, value) => getProperties(value, property)); | |
| } | |
| if (!isObject(value)) { | |
| return { | |
| [parent]: value | |
| }; | |
| } | |
| return reduceObject(value, (property, value) => getProperties(value, `${parent}.${property}`)); | |
| }; | |
| export default getProperties; |