There will be a huge object mapping (transforming) with a lot of calculations and logic for some fields.
I would like to refactor this to classes or some kind of separation so it will be easily testable (jest) for each field. How would you abstract these to a few classes or which design pattern that is best suited for this?
For example:
function mapData(data) {
const orderItems = data.items.map(item => {
return {
Name: item.title,
Qty: parseInt(item.qty),
Price: parseFloat(item.price),
PriceExcVat: getItemPriceExcVat(item),
TotalIncVat: getItemTotalIncVat(item),
TotalExcVat: getItemTotalExcVat(item),
// and many more fields
}
});
return {
Id: data.data,
Total: data_grand_total,
TotalIncVat: getOrderIncVat(data),
Items: orderItems,
DeliveryAddress: {
Name: data.address.name,
Address1: data.address.line_1,
Address2: data.address.line_2,
Address3: data.address.line_3,
},
// and many more fields
}
}
function getOrderIncVat(data) { return /*do some math calculation logic here*/ }
function getItemPriceExcVat(data) { return /*do some math calculation logic here*/ }
function getItemTotalIncVat(data) { return /*do some math calculation logic here*/ }
function getItemTotalExcVat(data) { return /*do some math calculation logic here*/ }
Aucun commentaire:
Enregistrer un commentaire