UNPKG

parse-ingredient

Version:

Recipe ingredient parser with support for mixed numbers and vulgar fractions

2 lines 5.83 kB
"use strict";var y=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var T=(a,e)=>{for(var n in e)y(a,n,{get:e[n],enumerable:!0})},C=(a,e,n,m)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of S(e))!P.call(a,l)&&l!==n&&y(a,l,{get:()=>e[l],enumerable:!(m=A(e,l))||m.enumerable});return a};var F=a=>C(y({},"__esModule",{value:!0}),a);var J={};T(J,{defaultOptions:()=>h,firstWordRegEx:()=>k,fors:()=>v,forsRegEx:()=>M,fromRegEx:()=>w,froms:()=>j,ofRegEx:()=>R,ofs:()=>W,parseIngredient:()=>B,rangeSeparatorRegEx:()=>q,rangeSeparatorWords:()=>L,trailingQuantityRegEx:()=>x,unitsOfMeasure:()=>N});module.exports=F(J);var u=require("numeric-quantity");var D=require("numeric-quantity"),h={additionalUOMs:{},allowLeadingOf:!1,normalizeUOM:!1,ignoreUOMs:[]},v=["For"],M=new RegExp(`^(?:${v.join("|")})\\s`,"i"),L=["or","to"],Q=`(-|\u2013|\u2014|(?:${L.join("|")})\\s)`,q=new RegExp(`^${Q}`,"i"),k=/^(fl(?:uid)?(?:\s+|-)(?:oz|ounces?)|\w+[-.]?)(.+)?/,z=D.numericRegex.source.replace(/^\^/,"").replace(/\$$/,""),x=new RegExp(`(,|:|-|\u2013|\u2014|x|\u2A2F)?\\s*((${z})\\s*(${Q}))?\\s*(${z})\\s*(fl(?:uid)?(?:\\s+|-)(?:oz|ounces?)|\\w+)?$`,"i"),W=["of"],R=new RegExp(`^(?:${W.join("|")})\\s+`,"i"),j=["from","of"],w=new RegExp(`\\s+(?:${j.join("|")})$`,"i"),N={bag:{short:"bag",plural:"bags",alternates:[]},box:{short:"box",plural:"boxes",alternates:[]},bunch:{short:"bunch",plural:"bunches",alternates:[]},can:{short:"can",plural:"cans",alternates:[]},carton:{short:"carton",plural:"cartons",alternates:[]},centimeter:{short:"cm",plural:"centimeters",alternates:["cm."]},clove:{short:"clove",plural:"cloves",alternates:[]},container:{short:"container",plural:"containers",alternates:[]},cup:{short:"c",plural:"cups",alternates:["c.","C"]},dash:{short:"dash",plural:"dashes",alternates:[]},drop:{short:"drop",plural:"drops",alternates:[]},ear:{short:"ear",plural:"ears",alternates:[]},"fluid ounce":{short:"fl oz",plural:"fluid ounces",alternates:["fluidounce","floz","fl-oz","fluid-ounce","fluid-ounces","fluidounces","fl ounce","fl ounces","fl-ounce","fl-ounces","fluid oz","fluid-oz"]},foot:{short:"ft",plural:"feet",alternates:["ft."]},gallon:{short:"gal",plural:"gallons",alternates:["gal."]},gram:{short:"g",plural:"grams",alternates:["g."]},head:{short:"head",plural:"heads",alternates:[]},inch:{short:"in",plural:"inches",alternates:["in."]},kilogram:{short:"kg",plural:"kilograms",alternates:["kg."]},large:{short:"lg",plural:"large",alternates:["lg","lg."]},liter:{short:"l",plural:"liters",alternates:["l."]},medium:{short:"md",plural:"medium",alternates:["med","med.","md."]},meter:{short:"m",plural:"meters",alternates:["m."]},milligram:{short:"mg",plural:"milligrams",alternates:["mg."]},milliliter:{short:"ml",plural:"milliliters",alternates:["mL","ml.","mL."]},millimeter:{short:"mm",plural:"millimeters",alternates:["mm."]},ounce:{short:"oz",plural:"ounces",alternates:["oz."]},pack:{short:"pack",plural:"packs",alternates:[]},package:{short:"pkg",plural:"packages",alternates:["pkg.","pkgs","pkgs."]},piece:{short:"piece",plural:"pieces",alternates:["pc","pc.","pcs","pcs."]},pinch:{short:"pinch",plural:"pinches",alternates:[]},pint:{short:"pt",plural:"pints",alternates:["pt."]},pound:{short:"lb",plural:"pounds",alternates:["lb.","lbs","lbs."]},quart:{short:"qt",plural:"quarts",alternates:["qt.","qts","qts."]},small:{short:"sm",plural:"small",alternates:["sm."]},sprig:{short:"sprig",plural:"sprigs",alternates:[]},stick:{short:"stick",plural:"sticks",alternates:[]},tablespoon:{short:"tbsp",plural:"tablespoons",alternates:["tbsp.","T","Tbsp.","Tbsp"]},teaspoon:{short:"tsp",plural:"teaspoons",alternates:["tsp.","t"]},yard:{short:"yd",plural:"yards",alternates:["yd.","yds."]}};var G=/\r?\n/,H=([a,e])=>({id:a,...e}),B=(a,e=h)=>{let n={...h,...e},m={...N,...n.additionalUOMs},l=Object.entries(m).map(H),I=l.length;return a.split(G).map(p=>p.trim()).filter(Boolean).map(p=>{let s={quantity:null,quantity2:null,unitOfMeasureID:null,unitOfMeasure:null,description:"",isGroupHeader:!1};if(isNaN((0,u.numericQuantity)(p[0]))){let t=x.exec(p);if(t&&n.ignoreUOMs.includes(t.at(-1)??""))s.description=p;else if(t){s.description=p.replace(x,"").trim();let i=t[3],o=t[12];i?(s.quantity=(0,u.numericQuantity)(i),s.quantity2=(0,u.numericQuantity)(o)):s.quantity=(0,u.numericQuantity)(o);let r=t.at(-1);if(r){let c="",g="",f=-1;for(;++f<I&&!c;){let{alternates:E,id:d,short:$,plural:b}=l[f];[...E,d,$,b].includes(r)&&(c=r,g=d)}c?(s.unitOfMeasureID=g,s.unitOfMeasure=n.normalizeUOM?g:c):s.description.match(w)&&(s.description+=` ${r}`)}}else s.description=p,(s.description.endsWith(":")||M.test(s.description))&&(s.isGroupHeader=!0)}else{let t=6,i=NaN;for(;t>0&&isNaN(i);)i=(0,u.numericQuantity)(p.substring(0,t).trim()),i>-1&&(s.quantity=i,s.description=p.substring(t).trim()),t--}let U=q.exec(s.description);if(U){let t=U[1].length,i=(0,u.numericQuantity)(s.description.substring(t).trim()[0]);if(!isNaN(i)){let o=7,r=NaN;for(;--o>0&&isNaN(r);)r=(0,u.numericQuantity)(s.description.substring(t,o)),isNaN(r)||(s.quantity2=r,s.description=s.description.substring(o).trim())}}let O=k.exec(s.description);if(O){let t=O[1].replace(/\s+/g," "),i=(O[2]??"").trim();if(i){let o="",r="",c=-1;for(;++c<I&&!o;){let{alternates:g,id:f,short:E,plural:d}=l[c];[...g,f,E,d].filter(b=>!n.ignoreUOMs.includes(b)).includes(t)&&(o=t,r=f)}o&&(s.unitOfMeasureID=r,s.unitOfMeasure=n.normalizeUOM?r:o,s.description=i)}}return!n.allowLeadingOf&&s.description.match(R)&&(s.description=s.description.replace(R,"")),s})};0&&(module.exports={defaultOptions,firstWordRegEx,fors,forsRegEx,fromRegEx,froms,ofRegEx,ofs,parseIngredient,rangeSeparatorRegEx,rangeSeparatorWords,trailingQuantityRegEx,unitsOfMeasure}); //# sourceMappingURL=parse-ingredient.cjs.production.js.map