(unit: WeightUnit, items: (PackItem | Item)[])
| 15 | }; |
| 16 | |
| 17 | export const getTotalWeight = (unit: WeightUnit, items: (PackItem | Item)[]): AggregateWeightProps => { |
| 18 | function reducer(acc: { include: number, exclude: number }, item: PackItem | Item) { |
| 19 | const isWorn = 'packItem' in item ? item.packItem.worn : false; |
| 20 | const quantity = 'packItem' in item ? item.packItem.quantity : 1; |
| 21 | const { weight, weight_unit, Category } = item; |
| 22 | |
| 23 | // no weight unit specified |
| 24 | if (!weight_unit) return acc; |
| 25 | |
| 26 | const conversionValue = converter(weight).from(unitDict[weight_unit]).to(unitDict[unit]); |
| 27 | const convertedWeight = parseFloat(conversionValue); |
| 28 | |
| 29 | if (Category.exclude_weight) { |
| 30 | return { ...acc, exclude: acc.exclude + convertedWeight * quantity }; |
| 31 | } |
| 32 | |
| 33 | if (isWorn) { |
| 34 | return { ...acc, include: acc.include + convertedWeight * (quantity - 1), exclude: acc.exclude + convertedWeight }; |
| 35 | } |
| 36 | |
| 37 | return { ...acc, include: acc.include + convertedWeight * quantity }; |
| 38 | } |
| 39 | |
| 40 | const { include, exclude } = items.reduce(reducer, { include: 0, exclude: 0 }); |
| 41 | const total = include + exclude; |
| 42 | |
| 43 | return { |
| 44 | included: { |
| 45 | value: include, |
| 46 | label: include.toFixed(2) |
| 47 | }, |
| 48 | excluded: { |
| 49 | value: exclude, |
| 50 | label: exclude.toFixed(2) |
| 51 | }, |
| 52 | total: { |
| 53 | value: total, |
| 54 | label: total.toFixed(2) |
| 55 | } |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | export const getItemWeight = (unit: WeightUnit, item: PackItem | Item): WeightProps => { |
| 60 | const { weight, weight_unit } = item; |
no outgoing calls
no test coverage detected