MCPcopy Create free account
hub / github.com/Packstack-Tech/packstack / getTotalWeight

Function getTotalWeight

frontend/src/lib/utils/weight.ts:17–57  ·  view source on GitHub ↗
(unit: WeightUnit, items: (PackItem | Item)[])

Source from the content-addressed store, hash-verified

15};
16
17export 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
59export const getItemWeight = (unit: WeightUnit, item: PackItem | Item): WeightProps => {
60 const { weight, weight_unit } = item;

Callers 1

getWeightByCategoryFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected