Skip to content

Instantly share code, notes, and snippets.

@santospatrick
Created May 28, 2018 01:07
Show Gist options
  • Save santospatrick/6fedeb1f9a711acf3ef9908414c04019 to your computer and use it in GitHub Desktop.
Save santospatrick/6fedeb1f9a711acf3ef9908414c04019 to your computer and use it in GitHub Desktop.
/**
* 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