Created
May 28, 2018 01:07
-
-
Save santospatrick/6fedeb1f9a711acf3ef9908414c04019 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get cultures name into a new array | |
* Immutable, pure | |
* @param {Array} cultures | |
* @returns {Array<String>} | |
*/ | |
const getNames = cultures => cultures.map(item => item.cultureName); | |
/** | |
* Get cultures quantity summed into | |
* a new Object separeted by unit | |
* @param {Array} cultures | |
* @returns {Object} | |
*/ | |
const energies = cultures => cultures | |
.reduce((acc, nextObject) => { | |
const nextValue = nextObject.energy.quantity; | |
const currentValue = acc[nextObject.energy.unit] | |
? acc[nextObject.energy.unit].quantity | |
: 0; | |
acc[nextObject.energy.unit] = { | |
quantity: currentValue + nextValue, | |
}; | |
return acc; | |
}, {}); | |
/** | |
* Get cultures quantity summed if it has | |
* some value, otherwhise it is not summed | |
* preventing NaN | |
* @param {Array} cultures | |
* @returns {Number} | |
*/ | |
const totalQuantity = cultures => cultures | |
.filter(item => !item.quantity) | |
.reduce((acc, nextObject) => acc + nextObject['quantity'], 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment