refactor: simplify parse ingredients code
This commit is contained in:
@@ -1,35 +1,94 @@
|
||||
import { parseIngredient as _parseIngredient } from "https://esm.sh/parse-ingredient@1.0.1";
|
||||
import {
|
||||
parseIngredient,
|
||||
unitsOfMeasure as _unitsOfMeasure,
|
||||
} from "https://esm.sh/parse-ingredient@1.2.1";
|
||||
import { Ingredient, IngredientGroup } from "@lib/recipeSchema.ts";
|
||||
import { removeMarkdownFormatting } from "@lib/string.ts";
|
||||
|
||||
export function parseIngredient(text: string) {
|
||||
const ing = _parseIngredient(text, {
|
||||
additionalUOMs: {
|
||||
tableSpoon: {
|
||||
short: "EL",
|
||||
plural: "Table Spoons",
|
||||
alternates: ["el", "EL", "Tbsp", "tbsp"],
|
||||
},
|
||||
teaSpoon: {
|
||||
short: "TL",
|
||||
plural: "Tea Spoon",
|
||||
alternates: ["tl", "TL", "Tsp", "tsp", "teaspoon"],
|
||||
},
|
||||
litre: {
|
||||
short: "L",
|
||||
plural: "liters",
|
||||
alternates: ["L", "l"],
|
||||
},
|
||||
paket: {
|
||||
short: "Paket",
|
||||
plural: "Pakets",
|
||||
alternates: ["Paket", "paket"],
|
||||
},
|
||||
},
|
||||
const customUnits = {
|
||||
tableSpoon: {
|
||||
short: "EL",
|
||||
plural: "Table Spoons",
|
||||
alternates: ["el", "EL", "Tbsp", "tbsp"],
|
||||
},
|
||||
dose: {
|
||||
short: "Dose",
|
||||
plural: "Dosen",
|
||||
alternates: ["Dose", "dose", "Dose(n)"],
|
||||
},
|
||||
pound: {
|
||||
short: "lb",
|
||||
plural: "pounds",
|
||||
alternates: ["lb", "lbs", "pound", "pounds"],
|
||||
},
|
||||
teaSpoon: {
|
||||
short: "TL",
|
||||
plural: "Tea Spoon",
|
||||
alternates: ["tl", "TL", "Tsp", "tsp", "teaspoon"],
|
||||
},
|
||||
litre: {
|
||||
short: "L",
|
||||
plural: "liters",
|
||||
alternates: ["L", "l"],
|
||||
},
|
||||
paket: {
|
||||
short: "Paket",
|
||||
plural: "Pakets",
|
||||
alternates: ["Paket", "paket"],
|
||||
},
|
||||
};
|
||||
|
||||
export const unitsOfMeasure = {
|
||||
..._unitsOfMeasure,
|
||||
...customUnits,
|
||||
} as const;
|
||||
|
||||
export function parseIngredients(
|
||||
text: string,
|
||||
): (Ingredient | IngredientGroup)[] {
|
||||
const cleanText = removeMarkdownFormatting(text);
|
||||
|
||||
const ingredients = parseIngredient(cleanText, {
|
||||
normalizeUOM: true,
|
||||
additionalUOMs: customUnits,
|
||||
});
|
||||
|
||||
return {
|
||||
name: ing[0].description,
|
||||
unit: ing[0].unitOfMeasure || "",
|
||||
quantity: ing[0].quantity?.toString() || "",
|
||||
note: "",
|
||||
};
|
||||
const results: (Ingredient | IngredientGroup)[] = [];
|
||||
let currentGroup: IngredientGroup | undefined;
|
||||
|
||||
for (const ing of ingredients) {
|
||||
if (ing.isGroupHeader) {
|
||||
if (currentGroup) {
|
||||
results.push(currentGroup);
|
||||
}
|
||||
currentGroup = {
|
||||
name: ing.description.replace(/:$/, ""),
|
||||
items: [],
|
||||
};
|
||||
} else {
|
||||
const ingredient = {
|
||||
name: ing.description.replace(/^\s?-/, "").trim(),
|
||||
unit: ing.unitOfMeasure || "",
|
||||
quantity: ing.quantity?.toString() || ing.quantity2?.toString() || "",
|
||||
note: "",
|
||||
};
|
||||
|
||||
const unit = ingredient.unit.toLowerCase() as keyof typeof unitsOfMeasure;
|
||||
if (unit in unitsOfMeasure && unit !== "cup") {
|
||||
ingredient.unit = unitsOfMeasure[unit].short;
|
||||
}
|
||||
|
||||
if (!currentGroup) {
|
||||
results.push(ingredient);
|
||||
} else {
|
||||
currentGroup.items.push(ingredient);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (currentGroup) {
|
||||
results.push(currentGroup);
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user