2024-05-19 03:43:59 +00:00
|
|
|
export const equivalentUnits = {
|
2025-10-18 02:08:22 +00:00
|
|
|
kg: {
|
|
|
|
|
kgs: 1,
|
|
|
|
|
kilograms: 1,
|
|
|
|
|
kilogram: 1,
|
|
|
|
|
|
|
|
|
|
g: 1000,
|
|
|
|
|
gram: 1000,
|
|
|
|
|
grams: 1000,
|
|
|
|
|
|
|
|
|
|
lb: 2.20462,
|
|
|
|
|
lbs: 2.20462,
|
|
|
|
|
pound: 2.20462,
|
|
|
|
|
pounds: 2.20462,
|
|
|
|
|
},
|
|
|
|
|
litres: {
|
|
|
|
|
l: 1,
|
|
|
|
|
liter: 1,
|
|
|
|
|
litre: 1,
|
|
|
|
|
|
|
|
|
|
ml: 1000,
|
|
|
|
|
milliliters: 1000,
|
|
|
|
|
milliliter: 1000,
|
|
|
|
|
|
|
|
|
|
'fl oz': 33.814,
|
|
|
|
|
'fluid ounce': 33.814,
|
|
|
|
|
'fluid ounces': 33.814,
|
|
|
|
|
|
|
|
|
|
cup: 4.22675,
|
|
|
|
|
cups: 4.22675,
|
|
|
|
|
|
|
|
|
|
tbsp: 67.628,
|
|
|
|
|
tablespoon: 67.628,
|
|
|
|
|
tablespoons: 67.628,
|
|
|
|
|
|
|
|
|
|
tsp: 202.884,
|
|
|
|
|
teaspoon: 202.884,
|
|
|
|
|
teaspoons: 202.884,
|
|
|
|
|
|
|
|
|
|
pt: 2.11338,
|
|
|
|
|
pint: 2.11338,
|
|
|
|
|
pints: 2.11338,
|
|
|
|
|
|
|
|
|
|
qt: 1.05669,
|
|
|
|
|
quart: 1.05669,
|
|
|
|
|
quarts: 1.05669,
|
|
|
|
|
|
|
|
|
|
gal: 0.264172,
|
|
|
|
|
gallon: 0.264172,
|
|
|
|
|
gallons: 0.264172,
|
|
|
|
|
|
|
|
|
|
oz: 35.1951,
|
|
|
|
|
ounce: 35.1951,
|
|
|
|
|
},
|
|
|
|
|
items: {
|
|
|
|
|
item: 1,
|
|
|
|
|
items: 1,
|
|
|
|
|
pcs: 1,
|
|
|
|
|
piece: 1,
|
|
|
|
|
pieces: 1,
|
|
|
|
|
florets: 8, // Broccoli
|
|
|
|
|
head: 1, // Broccoli
|
|
|
|
|
heads: 1, // Broccoli
|
|
|
|
|
slice: 10, // Bread
|
|
|
|
|
slices: 10, // Bread
|
|
|
|
|
loaf: 1, // Bread
|
|
|
|
|
loaves: 1, // Bread
|
|
|
|
|
cloves: 8, // Garlic
|
|
|
|
|
bulb: 1, // Garlic
|
|
|
|
|
bulbs: 1, // Garlic
|
|
|
|
|
stalk: 1, // Celery
|
|
|
|
|
stalks: 1, // Celery
|
|
|
|
|
bunch: 1, // Cilantro
|
|
|
|
|
bunches: 1, // Cilantro
|
|
|
|
|
sprig: 1, // Cilantro
|
|
|
|
|
sprigs: 1, // Cilantro
|
|
|
|
|
cans: 1, // Canned goods
|
|
|
|
|
can: 1, // Canned goods
|
|
|
|
|
pack: 1, // Packaged goods
|
|
|
|
|
packs: 1, // Packaged goods
|
|
|
|
|
package: 1, // Packaged goods
|
|
|
|
|
packages: 1, // Packaged goods
|
|
|
|
|
container: 1, // Packaged goods
|
|
|
|
|
containers: 1, // Packaged goods
|
|
|
|
|
},
|
2024-05-19 03:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getBaseUnit(unit) {
|
2025-10-18 02:08:22 +00:00
|
|
|
for (const unitType in equivalentUnits) {
|
|
|
|
|
if (unit in equivalentUnits[unitType]) {
|
|
|
|
|
return unitType
|
2024-05-19 03:43:59 +00:00
|
|
|
}
|
2025-10-18 02:08:22 +00:00
|
|
|
}
|
2024-05-19 03:43:59 +00:00
|
|
|
|
2025-10-18 02:08:22 +00:00
|
|
|
return null
|
2024-05-19 03:43:59 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-13 08:19:36 +00:00
|
|
|
export function getConversionFactor(unit) {
|
2025-10-18 02:08:22 +00:00
|
|
|
if (unit in equivalentUnits) {
|
|
|
|
|
return { unit, factor: 1 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const unitLower = unit.toLowerCase()
|
|
|
|
|
if (unitLower in equivalentUnits) {
|
|
|
|
|
return { unit: unitLower, factor: 1 }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseUnit = getBaseUnit(unit)
|
|
|
|
|
if (baseUnit) {
|
|
|
|
|
return {
|
|
|
|
|
unit: baseUnit,
|
|
|
|
|
factor: equivalentUnits[baseUnit][unit],
|
2024-10-13 08:19:36 +00:00
|
|
|
}
|
2025-10-18 02:08:22 +00:00
|
|
|
}
|
2024-05-19 03:43:59 +00:00
|
|
|
|
2025-10-18 02:08:22 +00:00
|
|
|
const baseUnitLower = getBaseUnit(unitLower)
|
|
|
|
|
if (baseUnitLower) {
|
|
|
|
|
return {
|
|
|
|
|
unit: baseUnitLower,
|
|
|
|
|
factor: equivalentUnits[baseUnitLower][unitLower],
|
2024-10-13 08:19:36 +00:00
|
|
|
}
|
2025-10-18 02:08:22 +00:00
|
|
|
}
|
2024-05-19 10:21:49 +00:00
|
|
|
|
2025-10-18 02:08:22 +00:00
|
|
|
return null
|
2024-10-13 08:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function calculateTotals(quantityList) {
|
2025-10-18 02:08:22 +00:00
|
|
|
const totals = {}
|
|
|
|
|
for (const quantity of quantityList) {
|
|
|
|
|
const baseUnit = getConversionFactor(quantity.unit) ?? { unit: quantity.unit, factor: 1 }
|
|
|
|
|
const factor = baseUnit.factor
|
|
|
|
|
const unit = baseUnit.unit
|
|
|
|
|
|
|
|
|
|
if (!totals[unit]) {
|
|
|
|
|
totals[unit] = 0
|
2024-10-13 08:19:36 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-18 02:08:22 +00:00
|
|
|
totals[unit] += quantity.quantity / factor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Object.keys(totals).map((unit) => ({ unit, quantity: totals[unit] }))
|
|
|
|
|
}
|