| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useFraction } from "./use-fraction"; | 
					
						
							|  |  |  | import { RecipeIngredient } from "~/types/api-types/recipe"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { frac } = useFraction(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function parseIngredientText(ingredient: RecipeIngredient, disableAmount: boolean, scale: number = 1): string { | 
					
						
							|  |  |  |   if (disableAmount) { | 
					
						
							|  |  |  |     return ingredient.note; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { quantity, food, unit, note } = ingredient; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |   const validQuantity = quantity !== null && quantity !== undefined && quantity !== 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   let returnQty = ""; | 
					
						
							|  |  |  |   if (unit?.fraction) { | 
					
						
							|  |  |  |     const fraction = frac(quantity * scale, 10, true); | 
					
						
							|  |  |  |     if (fraction[0] !== undefined && fraction[0] > 0) { | 
					
						
							|  |  |  |       returnQty += fraction[0]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (fraction[1] > 0) { | 
					
						
							|  |  |  |       returnQty += ` <sup>${fraction[1]}</sup>⁄<sub>${fraction[2]}</sub>`; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |   } else if (validQuantity) { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |     returnQty = (quantity * scale).toString(); | 
					
						
							| 
									
										
										
										
											2021-12-11 19:12:08 -09:00
										 |  |  |   } else { | 
					
						
							|  |  |  |     returnQty = ""; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return `${returnQty} ${unit?.name || " "}  ${food?.name || " "} ${note}`.replace(/ {2,}/g, " "); | 
					
						
							|  |  |  | } |