2022-01-17 00:24:24 +00:00
|
|
|
/**
|
|
|
|
* use-display-text module contains helpful utility functions to compute the display text when provided
|
|
|
|
* with the food, units, quantity, and notes.
|
|
|
|
*/
|
|
|
|
|
2022-10-22 19:51:07 +00:00
|
|
|
import { IngredientFood, IngredientUnit } from "~/lib/api/types/recipe";
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
export function getDisplayText(
|
|
|
|
notes = "",
|
|
|
|
quantity: number | null = null,
|
|
|
|
food: IngredientFood | null = null,
|
|
|
|
unit: IngredientUnit | null = null
|
|
|
|
): string {
|
|
|
|
// Fallback to note only if no food or unit is provided
|
|
|
|
if (food === null && unit === null) {
|
|
|
|
return `${quantity || ""} ${notes}`.trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise build the display text
|
|
|
|
let displayText = "";
|
|
|
|
|
|
|
|
if (quantity) {
|
|
|
|
displayText += quantity;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unit) {
|
|
|
|
displayText += ` ${unit.name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (food) {
|
|
|
|
displayText += ` ${food.name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notes) {
|
|
|
|
displayText += ` ${notes}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return displayText.trim();
|
|
|
|
}
|