I wrote this function below which transforms the passed array of products by product type and currency type
function getProductsByCurrency(products, type, exchangeRate = 1) {
var productsRetrieved = products.map(item => ({id: item.id,
name: item.name,
price: (item.price * exchangeRate).toFixed(2),
type: type}));
return productsRetrieved;
}
Is it possible to break down the function to be more specific? or design it in a better way? For example by naming it getProductsByCurrency it doesn't look right because if I am using it with default rate, i can pass books array to retrieve products with 'books' type independent of exchange rate. Perhaps there is a way to use partial functions(FP)?
Edit: Adding more context to what I am trying to achieve.
Lets say I have three categories of products(phones, cosmetics, books) coming from three resources. I need to create three consolidated arrays of all the products by different currencies(productsinUSD, productsinAUD, productsinPounds)
Also using below function to consolidate the arrays
function concatProducts(arr) {
return [].concat.apply([], arr);
}
So I am calling getProductsByCurrency three times to transform them by product type and currency(exchange rate) and passing those values as an array to concat them to achieve productsinUSD. And repeat to get productsinAUD, productsinPounds.
Also type is string value(ex: 'mobiles')