parse-ingredient
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -70,2 +70,26 @@ /** | ||
/** | ||
* An array of strings to ignore as units of measure when parsing ingredients. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* parseIngredient('2 small eggs', { | ||
* ignoreUOMs: ['small', 'medium', 'large'] | ||
* }) | ||
* // [ | ||
* // { | ||
* // quantity: 2, | ||
* // quantity2: null, | ||
* // unitOfMeasure: null, | ||
* // unitOfMeasureID: null, | ||
* // description: 'small eggs', | ||
* // isGroupHeader: false, | ||
* // } | ||
* // ] | ||
* ``` | ||
* | ||
* @default [] | ||
*/ | ||
ignoreUOMs?: string[]; | ||
/** | ||
* If `true`, ingredient descriptions that start with "of " will not be | ||
@@ -91,2 +115,3 @@ * modified. (By default, a leading "of " will be removed from all descriptions.) | ||
readonly normalizeUOM: false; | ||
readonly ignoreUOMs: []; | ||
}; | ||
@@ -93,0 +118,0 @@ /** |
@@ -45,3 +45,4 @@ "use strict"; | ||
allowLeadingOf: false, | ||
normalizeUOM: false | ||
normalizeUOM: false, | ||
ignoreUOMs: [] | ||
}; | ||
@@ -297,3 +298,5 @@ var fors = ["For"]; | ||
const trailingQtyResult = trailingQuantityRegEx.exec(line); | ||
if (trailingQtyResult) { | ||
if (trailingQtyResult && opts.ignoreUOMs.includes(trailingQtyResult.at(-1) ?? "")) { | ||
oIng.description = line; | ||
} else if (trailingQtyResult) { | ||
oIng.description = line.replace(trailingQuantityRegEx, "").trim(); | ||
@@ -370,3 +373,5 @@ const firstQty = trailingQtyResult[3]; | ||
const { alternates, id, short, plural } = uomArray[i]; | ||
const versions = [...alternates, id, short, plural]; | ||
const versions = [...alternates, id, short, plural].filter( | ||
(unit) => !opts.ignoreUOMs.includes(unit) | ||
); | ||
if (versions.includes(firstWord)) { | ||
@@ -373,0 +378,0 @@ uom = firstWord; |
@@ -70,2 +70,26 @@ /** | ||
/** | ||
* An array of strings to ignore as units of measure when parsing ingredients. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* parseIngredient('2 small eggs', { | ||
* ignoreUOMs: ['small', 'medium', 'large'] | ||
* }) | ||
* // [ | ||
* // { | ||
* // quantity: 2, | ||
* // quantity2: null, | ||
* // unitOfMeasure: null, | ||
* // unitOfMeasureID: null, | ||
* // description: 'small eggs', | ||
* // isGroupHeader: false, | ||
* // } | ||
* // ] | ||
* ``` | ||
* | ||
* @default [] | ||
*/ | ||
ignoreUOMs?: string[]; | ||
/** | ||
* If `true`, ingredient descriptions that start with "of " will not be | ||
@@ -91,2 +115,3 @@ * modified. (By default, a leading "of " will be removed from all descriptions.) | ||
readonly normalizeUOM: false; | ||
readonly ignoreUOMs: []; | ||
}; | ||
@@ -93,0 +118,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
"use strict";var b=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var j=(a,e)=>{for(var l in e)b(a,l,{get:e[l],enumerable:!0})},P=(a,e,l,m)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of A(e))!S.call(a,o)&&o!==l&&b(a,o,{get:()=>e[o],enumerable:!(m=W(e,o))||m.enumerable});return a};var T=a=>P(b({},"__esModule",{value:!0}),a);var H={};j(H,{defaultOptions:()=>h,firstWordRegEx:()=>E,fors:()=>$,forsRegEx:()=>M,ofRegEx:()=>O,ofs:()=>L,parseIngredient:()=>G,rangeSeparatorRegEx:()=>q,rangeSeparatorWords:()=>D,trailingQuantityRegEx:()=>x,unitsOfMeasure:()=>N});module.exports=T(H);var u=require("numeric-quantity");var U=require("numeric-quantity"),h={additionalUOMs:{},allowLeadingOf:!1,normalizeUOM:!1},$=["For"],M=new RegExp(`^(?:${$.join("|")})\\s`,"i"),D=["or","to"],v=`(-|\u2013|\u2014|(?:${D.join("|")})\\s)`,q=new RegExp(`^${v}`,"i"),E=/^(fl(?:uid)?(?:\s+|-)(?:oz|ounces?)|\w+[-.]?)(.+)?/,z=U.numericRegex.source.replace(/^\^/,"").replace(/\$$/,""),x=new RegExp(`(,|:|-|\u2013|\u2014|x|\u2A2F)?\\s*((${z})\\s*(${v}))?\\s*(${z})\\s*(fl(?:uid)?(?:\\s+|-)(?:oz|ounces?)|\\w+)?$`,"i"),L=["of"],O=new RegExp(`^(?:${L.join("|")})\\s+`,"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:[]},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"]},piece:{short:"piece",plural:"pieces",alternates:["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."]},teaspoon:{short:"tsp",plural:"teaspoons",alternates:["tsp.","t"]},yard:{short:"yd",plural:"yards",alternates:["yd.","yds."]}};var C=/\r?\n/,F=([a,e])=>({id:a,...e}),G=(a,e=h)=>{let l={...h,...e},m={...N,...l.additionalUOMs},o=Object.entries(m).map(F),k=o.length;return a.split(C).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){s.description=p.replace(x,"").trim();let r=t[3],n=t[12];r?(s.quantity=(0,u.numericQuantity)(r),s.quantity2=(0,u.numericQuantity)(n)):s.quantity=(0,u.numericQuantity)(n);let i=t.at(-1);if(i){let c="",g="",f=-1;for(;++f<k&&!c;){let{alternates:R,id:d,short:I,plural:Q}=o[f];[...R,d,I,Q].includes(i)&&(c=i,g=d)}c&&(s.unitOfMeasureID=g,s.unitOfMeasure=l.normalizeUOM?g:c)}}else s.description=p,(s.description.endsWith(":")||M.test(s.description))&&(s.isGroupHeader=!0)}else{let t=6,r=NaN;for(;t>0&&isNaN(r);)r=(0,u.numericQuantity)(p.substring(0,t).trim()),r>-1&&(s.quantity=r,s.description=p.substring(t).trim()),t--}let w=q.exec(s.description);if(w){let t=w[1].length,r=(0,u.numericQuantity)(s.description.substring(t).trim()[0]);if(!isNaN(r)){let n=7,i=NaN;for(;--n>0&&isNaN(i);)i=(0,u.numericQuantity)(s.description.substring(t,n)),isNaN(i)||(s.quantity2=i,s.description=s.description.substring(n).trim())}}let y=E.exec(s.description);if(y){let t=y[1].replace(/\s+/g," "),r=(y[2]??"").trim();if(r){let n="",i="",c=-1;for(;++c<k&&!n;){let{alternates:g,id:f,short:R,plural:d}=o[c];[...g,f,R,d].includes(t)&&(n=t,i=f)}n&&(s.unitOfMeasureID=i,s.unitOfMeasure=l.normalizeUOM?i:n,s.description=r)}}return!l.allowLeadingOf&&s.description.match(O)&&(s.description=s.description.replace(O,"")),s})};0&&(module.exports={defaultOptions,firstWordRegEx,fors,forsRegEx,ofRegEx,ofs,parseIngredient,rangeSeparatorRegEx,rangeSeparatorWords,trailingQuantityRegEx,unitsOfMeasure}); | ||
"use strict";var M=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var j=(a,e)=>{for(var n in e)M(a,n,{get:e[n],enumerable:!0})},P=(a,e,n,d)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of A(e))!S.call(a,o)&&o!==n&&M(a,o,{get:()=>e[o],enumerable:!(d=W(e,o))||d.enumerable});return a};var T=a=>P(M({},"__esModule",{value:!0}),a);var H={};j(H,{defaultOptions:()=>h,firstWordRegEx:()=>N,fors:()=>D,forsRegEx:()=>q,ofRegEx:()=>O,ofs:()=>Q,parseIngredient:()=>G,rangeSeparatorRegEx:()=>E,rangeSeparatorWords:()=>v,trailingQuantityRegEx:()=>x,unitsOfMeasure:()=>k});module.exports=T(H);var u=require("numeric-quantity");var $=require("numeric-quantity"),h={additionalUOMs:{},allowLeadingOf:!1,normalizeUOM:!1,ignoreUOMs:[]},D=["For"],q=new RegExp(`^(?:${D.join("|")})\\s`,"i"),v=["or","to"],L=`(-|\u2013|\u2014|(?:${v.join("|")})\\s)`,E=new RegExp(`^${L}`,"i"),N=/^(fl(?:uid)?(?:\s+|-)(?:oz|ounces?)|\w+[-.]?)(.+)?/,z=$.numericRegex.source.replace(/^\^/,"").replace(/\$$/,""),x=new RegExp(`(,|:|-|\u2013|\u2014|x|\u2A2F)?\\s*((${z})\\s*(${L}))?\\s*(${z})\\s*(fl(?:uid)?(?:\\s+|-)(?:oz|ounces?)|\\w+)?$`,"i"),Q=["of"],O=new RegExp(`^(?:${Q.join("|")})\\s+`,"i"),k={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:[]},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"]},piece:{short:"piece",plural:"pieces",alternates:["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."]},teaspoon:{short:"tsp",plural:"teaspoons",alternates:["tsp.","t"]},yard:{short:"yd",plural:"yards",alternates:["yd.","yds."]}};var C=/\r?\n/,F=([a,e])=>({id:a,...e}),G=(a,e=h)=>{let n={...h,...e},d={...k,...n.additionalUOMs},o=Object.entries(d).map(F),w=o.length;return a.split(C).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 r=t[3],l=t[12];r?(s.quantity=(0,u.numericQuantity)(r),s.quantity2=(0,u.numericQuantity)(l)):s.quantity=(0,u.numericQuantity)(l);let i=t.at(-1);if(i){let c="",g="",f=-1;for(;++f<w&&!c;){let{alternates:R,id:m,short:U,plural:b}=o[f];[...R,m,U,b].includes(i)&&(c=i,g=m)}c&&(s.unitOfMeasureID=g,s.unitOfMeasure=n.normalizeUOM?g:c)}}else s.description=p,(s.description.endsWith(":")||q.test(s.description))&&(s.isGroupHeader=!0)}else{let t=6,r=NaN;for(;t>0&&isNaN(r);)r=(0,u.numericQuantity)(p.substring(0,t).trim()),r>-1&&(s.quantity=r,s.description=p.substring(t).trim()),t--}let I=E.exec(s.description);if(I){let t=I[1].length,r=(0,u.numericQuantity)(s.description.substring(t).trim()[0]);if(!isNaN(r)){let l=7,i=NaN;for(;--l>0&&isNaN(i);)i=(0,u.numericQuantity)(s.description.substring(t,l)),isNaN(i)||(s.quantity2=i,s.description=s.description.substring(l).trim())}}let y=N.exec(s.description);if(y){let t=y[1].replace(/\s+/g," "),r=(y[2]??"").trim();if(r){let l="",i="",c=-1;for(;++c<w&&!l;){let{alternates:g,id:f,short:R,plural:m}=o[c];[...g,f,R,m].filter(b=>!n.ignoreUOMs.includes(b)).includes(t)&&(l=t,i=f)}l&&(s.unitOfMeasureID=i,s.unitOfMeasure=n.normalizeUOM?i:l,s.description=r)}}return!n.allowLeadingOf&&s.description.match(O)&&(s.description=s.description.replace(O,"")),s})};0&&(module.exports={defaultOptions,firstWordRegEx,fors,forsRegEx,ofRegEx,ofs,parseIngredient,rangeSeparatorRegEx,rangeSeparatorWords,trailingQuantityRegEx,unitsOfMeasure}); | ||
//# sourceMappingURL=parse-ingredient.cjs.production.js.map |
@@ -26,3 +26,4 @@ var __defProp = Object.defineProperty; | ||
allowLeadingOf: false, | ||
normalizeUOM: false | ||
normalizeUOM: false, | ||
ignoreUOMs: [] | ||
}; | ||
@@ -268,3 +269,3 @@ var fors = ["For"]; | ||
return ingredientArray.map((line) => { | ||
var _a; | ||
var _a, _b; | ||
const oIng = { | ||
@@ -280,3 +281,5 @@ quantity: null, | ||
const trailingQtyResult = trailingQuantityRegEx.exec(line); | ||
if (trailingQtyResult) { | ||
if (trailingQtyResult && opts.ignoreUOMs.includes((_a = trailingQtyResult.at(-1)) != null ? _a : "")) { | ||
oIng.description = line; | ||
} else if (trailingQtyResult) { | ||
oIng.description = line.replace(trailingQuantityRegEx, "").trim(); | ||
@@ -346,3 +349,3 @@ const firstQty = trailingQtyResult[3]; | ||
const firstWord = firstWordREMatches[1].replace(/\s+/g, " "); | ||
const remainingDesc = ((_a = firstWordREMatches[2]) != null ? _a : "").trim(); | ||
const remainingDesc = ((_b = firstWordREMatches[2]) != null ? _b : "").trim(); | ||
if (remainingDesc) { | ||
@@ -354,3 +357,5 @@ let uom = ""; | ||
const { alternates, id, short, plural } = uomArray[i]; | ||
const versions = [...alternates, id, short, plural]; | ||
const versions = [...alternates, id, short, plural].filter( | ||
(unit) => !opts.ignoreUOMs.includes(unit) | ||
); | ||
if (versions.includes(firstWord)) { | ||
@@ -357,0 +362,0 @@ uom = firstWord; |
@@ -1,2 +0,2 @@ | ||
"use strict";var ParseIngredient=(()=>{var v=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var B=Object.prototype.hasOwnProperty;var Q=(r,e)=>{for(var s in e)v(r,s,{get:e[s],enumerable:!0})},G=(r,e,s,c)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of T(e))!B.call(r,o)&&o!==s&&v(r,o,{get:()=>e[o],enumerable:!(c=S(e,o))||c.enumerable});return r};var H=r=>G(v({},"__esModule",{value:!0}),r);var ls={};Q(ls,{defaultOptions:()=>O,firstWordRegEx:()=>C,fors:()=>A,forsRegEx:()=>E,ofRegEx:()=>y,ofs:()=>j,parseIngredient:()=>os,rangeSeparatorRegEx:()=>X,rangeSeparatorWords:()=>P,trailingQuantityRegEx:()=>R,unitsOfMeasure:()=>w});var J=Object.defineProperty,V=Object.getOwnPropertySymbols,K=Object.prototype.hasOwnProperty,Y=Object.prototype.propertyIsEnumerable,$=(r,e,s)=>e in r?J(r,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):r[e]=s,q=(r,e)=>{for(var s in e||(e={}))K.call(e,s)&&$(r,s,e[s]);if(V)for(var s of V(e))Y.call(e,s)&&$(r,s,e[s]);return r},k={"\xBC":"1/4","\xBD":"1/2","\xBE":"3/4","\u2150":"1/7","\u2151":"1/9","\u2152":"1/10","\u2153":"1/3","\u2154":"2/3","\u2155":"1/5","\u2156":"2/5","\u2157":"3/5","\u2158":"4/5","\u2159":"1/6","\u215A":"5/6","\u215B":"1/8","\u215C":"3/8","\u215D":"5/8","\u215E":"7/8","\u215F":"1/"},_=/^(?=-?\s*\.\d|-?\s*\d)(-)?\s*((?:\d(?:[\d,_]*\d)?)*)(([eE][+-]?\d(?:[\d,_]*\d)?)?|\.\d(?:[\d,_]*\d)?([eE][+-]?\d(?:[\d,_]*\d)?)?|(\s+\d(?:[\d,_]*\d)?\s*)?\s*\/\s*\d(?:[\d,_]*\d)?)?$/,Z=/^(?=-?\s*\.\d|-?\s*\d)(-)?\s*((?:\d(?:[\d,_]*\d)?)*)(([eE][+-]?\d(?:[\d,_]*\d)?)?|\.\d(?:[\d,_]*\d)?([eE][+-]?\d(?:[\d,_]*\d)?)?|(\s+\d(?:[\d,_]*\d)?\s*)?\s*\/\s*\d(?:[\d,_]*\d)?)?(?:\s*[^\.\d\/].*)?/,ss=new RegExp(`(${Object.keys(k).join("|")})`),N={MMM:3e3,MM:2e3,M:1e3,CM:900,DCCC:800,DCC:700,DC:600,D:500,CD:400,CCC:300,CC:200,C:100,XC:90,LXXX:80,LXX:70,LX:60,L:50,XL:40,XXX:30,XX:20,XII:12,XI:11,X:10,IX:9,VIII:8,VII:7,VI:6,V:5,IV:4,III:3,II:2,I:1},U={"\u2160":"I","\u2161":"II","\u2162":"III","\u2163":"IV","\u2164":"V","\u2165":"VI","\u2166":"VII","\u2167":"VIII","\u2168":"IX","\u2169":"X","\u216A":"XI","\u216B":"XII","\u216C":"L","\u216D":"C","\u216E":"D","\u216F":"M","\u2170":"I","\u2171":"II","\u2172":"III","\u2173":"IV","\u2174":"V","\u2175":"VI","\u2176":"VII","\u2177":"VIII","\u2178":"IX","\u2179":"X","\u217A":"XI","\u217B":"XII","\u217C":"L","\u217D":"C","\u217E":"D","\u217F":"M"},ts=new RegExp(`(${Object.keys(U).join("|")})`,"gi"),es=/^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i,L={round:3,allowTrailingInvalid:!1,romanNumerals:!1},rs=r=>{var e,s,c,o;let m=`${r}`.replace(ts,(a,i)=>U[i]).toUpperCase(),h=es.exec(m);if(!h)return NaN;let[,p,t,g,l]=h;return((e=N[p])!=null?e:0)+((s=N[t])!=null?s:0)+((c=N[g])!=null?c:0)+((o=N[l])!=null?o:0)},as=/^\s*\//,f=(r,e=L)=>{if(typeof r=="number"||typeof r=="bigint")return r;let s=NaN,c=`${r}`.replace(ss,(i,n)=>` ${k[n]}`).replace("\u2044","/").trim();if(c.length===0)return NaN;let o=q(q({},L),e),m=(o.allowTrailingInvalid?Z:_).exec(c);if(!m)return o.romanNumerals?rs(c):NaN;let[,h,p,t]=m,g=p.replace(/[,_]/g,""),l=t==null?void 0:t.replace(/[,_]/g,"");if(!g&&l&&l.startsWith(".")?s=0:s=parseInt(g),!l)return h?s*-1:s;let a=o.round===!1?NaN:parseFloat(`1e${Math.floor(Math.max(0,o.round))}`);if(l.startsWith(".")||l.startsWith("e")||l.startsWith("E")){let i=parseFloat(`${s}${l}`);s=isNaN(a)?i:Math.round(i*a)/a}else if(as.test(l)){let i=parseInt(g),n=parseInt(l.replace("/",""));s=isNaN(a)?i/n:Math.round(i*a/n)/a}else{let i=l.split("/"),[n,u]=i.map(d=>parseInt(d));s+=isNaN(a)?n/u:Math.round(n*a/u)/a}return h?s*-1:s};var O={additionalUOMs:{},allowLeadingOf:!1,normalizeUOM:!1},A=["For"],E=new RegExp(`^(?:${A.join("|")})\\s`,"i"),P=["or","to"],W=`(-|\u2013|\u2014|(?:${P.join("|")})\\s)`,X=new RegExp(`^${W}`,"i"),C=/^(fl(?:uid)?(?:\s+|-)(?:oz|ounces?)|\w+[-.]?)(.+)?/,z=_.source.replace(/^\^/,"").replace(/\$$/,""),R=new RegExp(`(,|:|-|\u2013|\u2014|x|\u2A2F)?\\s*((${z})\\s*(${W}))?\\s*(${z})\\s*(fl(?:uid)?(?:\\s+|-)(?:oz|ounces?)|\\w+)?$`,"i"),j=["of"],y=new RegExp(`^(?:${j.join("|")})\\s+`,"i"),w={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:[]},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"]},piece:{short:"piece",plural:"pieces",alternates:["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."]},teaspoon:{short:"tsp",plural:"teaspoons",alternates:["tsp.","t"]},yard:{short:"yd",plural:"yards",alternates:["yd.","yds."]}};var is=/\r?\n/,ns=([r,e])=>({id:r,...e}),os=(r,e=O)=>{let s={...O,...e},c={...w,...s.additionalUOMs},o=Object.entries(c).map(ns),m=o.length;return r.split(is).map(p=>p.trim()).filter(Boolean).map(p=>{let t={quantity:null,quantity2:null,unitOfMeasureID:null,unitOfMeasure:null,description:"",isGroupHeader:!1};if(isNaN(f(p[0]))){let a=R.exec(p);if(a){t.description=p.replace(R,"").trim();let i=a[3],n=a[12];i?(t.quantity=f(i),t.quantity2=f(n)):t.quantity=f(n);let u=a.at(-1);if(u){let d="",I="",x=-1;for(;++x<m&&!d;){let{alternates:b,id:M,short:D,plural:F}=o[x];[...b,M,D,F].includes(u)&&(d=u,I=M)}d&&(t.unitOfMeasureID=I,t.unitOfMeasure=s.normalizeUOM?I:d)}}else t.description=p,(t.description.endsWith(":")||E.test(t.description))&&(t.isGroupHeader=!0)}else{let a=6,i=NaN;for(;a>0&&isNaN(i);)i=f(p.substring(0,a).trim()),i>-1&&(t.quantity=i,t.description=p.substring(a).trim()),a--}let g=X.exec(t.description);if(g){let a=g[1].length,i=f(t.description.substring(a).trim()[0]);if(!isNaN(i)){let n=7,u=NaN;for(;--n>0&&isNaN(u);)u=f(t.description.substring(a,n)),isNaN(u)||(t.quantity2=u,t.description=t.description.substring(n).trim())}}let l=C.exec(t.description);if(l){let a=l[1].replace(/\s+/g," "),i=(l[2]??"").trim();if(i){let n="",u="",d=-1;for(;++d<m&&!n;){let{alternates:I,id:x,short:b,plural:M}=o[d];[...I,x,b,M].includes(a)&&(n=a,u=x)}n&&(t.unitOfMeasureID=u,t.unitOfMeasure=s.normalizeUOM?u:n,t.description=i)}}return!s.allowLeadingOf&&t.description.match(y)&&(t.description=t.description.replace(y,"")),t})};return H(ls);})(); | ||
"use strict";var ParseIngredient=(()=>{var E=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var j=Object.prototype.hasOwnProperty;var B=(a,n)=>{for(var e in n)E(a,e,{get:n[e],enumerable:!0})},Q=(a,n,e,c)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of T(n))!j.call(a,o)&&o!==e&&E(a,o,{get:()=>n[o],enumerable:!(c=_(n,o))||c.enumerable});return a};var S=a=>Q(E({},"__esModule",{value:!0}),a);var es={};B(es,{defaultOptions:()=>O,firstWordRegEx:()=>$,fors:()=>z,forsRegEx:()=>v,ofRegEx:()=>y,ofs:()=>F,parseIngredient:()=>ts,rangeSeparatorRegEx:()=>D,rangeSeparatorWords:()=>A,trailingQuantityRegEx:()=>b,unitsOfMeasure:()=>w});var q={"\xBC":"1/4","\xBD":"1/2","\xBE":"3/4","\u2150":"1/7","\u2151":"1/9","\u2152":"1/10","\u2153":"1/3","\u2154":"2/3","\u2155":"1/5","\u2156":"2/5","\u2157":"3/5","\u2158":"4/5","\u2159":"1/6","\u215A":"5/6","\u215B":"1/8","\u215C":"3/8","\u215D":"5/8","\u215E":"7/8","\u215F":"1/"},R=/^(?=-?\s*\.\d|-?\s*\d)(-)?\s*((?:\d(?:[\d,_]*\d)?)*)(([eE][+-]?\d(?:[\d,_]*\d)?)?|\.\d(?:[\d,_]*\d)?([eE][+-]?\d(?:[\d,_]*\d)?)?|(\s+\d(?:[\d,_]*\d)?\s*)?\s*\/\s*\d(?:[\d,_]*\d)?)?$/,G=new RegExp(R.source.replace(/\$$/,"(?:\\s*[^\\.\\d\\/].*)?")),P=new RegExp(`(${Object.keys(q).join("|")})`),N={MMM:3e3,MM:2e3,M:1e3,CM:900,DCCC:800,DCC:700,DC:600,D:500,CD:400,CCC:300,CC:200,C:100,XC:90,LXXX:80,LXX:70,LX:60,L:50,XL:40,XXX:30,XX:20,XII:12,XI:11,X:10,IX:9,VIII:8,VII:7,VI:6,V:5,IV:4,III:3,II:2,I:1},L={"\u2160":"I","\u2161":"II","\u2162":"III","\u2163":"IV","\u2164":"V","\u2165":"VI","\u2166":"VII","\u2167":"VIII","\u2168":"IX","\u2169":"X","\u216A":"XI","\u216B":"XII","\u216C":"L","\u216D":"C","\u216E":"D","\u216F":"M","\u2170":"I","\u2171":"II","\u2172":"III","\u2173":"IV","\u2174":"V","\u2175":"VI","\u2176":"VII","\u2177":"VIII","\u2178":"IX","\u2179":"X","\u217A":"XI","\u217B":"XII","\u217C":"L","\u217D":"C","\u217E":"D","\u217F":"M"},H=new RegExp(`(${Object.keys(L).join("|")})`,"gi"),J=/^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i,k={round:3,allowTrailingInvalid:!1,romanNumerals:!1},K=a=>{let n=`${a}`.replace(H,(p,s)=>L[s]).toUpperCase(),e=J.exec(n);if(!e)return NaN;let[,c,o,d,h]=e;return(N[c]??0)+(N[o]??0)+(N[d]??0)+(N[h]??0)},Y=/^\s*\//,f=(a,n=k)=>{if(typeof a=="number"||typeof a=="bigint")return a;let e=NaN,c=`${a}`.replace(P,(r,i)=>` ${q[i]}`).replace("\u2044","/").trim();if(c.length===0)return NaN;let o={...k,...n},d=(o.allowTrailingInvalid?G:R).exec(c);if(!d)return o.romanNumerals?K(c):NaN;let[,h,p,s]=d,m=p.replace(/[,_]/g,""),u=s==null?void 0:s.replace(/[,_]/g,"");if(!m&&u&&u.startsWith(".")?e=0:e=parseInt(m),!u)return h?e*-1:e;let t=o.round===!1?NaN:parseFloat(`1e${Math.floor(Math.max(0,o.round))}`);if(u.startsWith(".")||u.startsWith("e")||u.startsWith("E")){let r=parseFloat(`${e}${u}`);e=isNaN(t)?r:Math.round(r*t)/t}else if(Y.test(u)){let r=parseInt(m),i=parseInt(u.replace("/",""));e=isNaN(t)?r/i:Math.round(r*t/i)/t}else{let r=u.split("/"),[i,l]=r.map(g=>parseInt(g));e+=isNaN(t)?i/l:Math.round(i*t/l)/t}return h?e*-1:e};var O={additionalUOMs:{},allowLeadingOf:!1,normalizeUOM:!1,ignoreUOMs:[]},z=["For"],v=new RegExp(`^(?:${z.join("|")})\\s`,"i"),A=["or","to"],W=`(-|\u2013|\u2014|(?:${A.join("|")})\\s)`,D=new RegExp(`^${W}`,"i"),$=/^(fl(?:uid)?(?:\s+|-)(?:oz|ounces?)|\w+[-.]?)(.+)?/,U=R.source.replace(/^\^/,"").replace(/\$$/,""),b=new RegExp(`(,|:|-|\u2013|\u2014|x|\u2A2F)?\\s*((${U})\\s*(${W}))?\\s*(${U})\\s*(fl(?:uid)?(?:\\s+|-)(?:oz|ounces?)|\\w+)?$`,"i"),F=["of"],y=new RegExp(`^(?:${F.join("|")})\\s+`,"i"),w={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:[]},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"]},piece:{short:"piece",plural:"pieces",alternates:["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."]},teaspoon:{short:"tsp",plural:"teaspoons",alternates:["tsp.","t"]},yard:{short:"yd",plural:"yards",alternates:["yd.","yds."]}};var Z=/\r?\n/,ss=([a,n])=>({id:a,...n}),ts=(a,n=O)=>{let e={...O,...n},c={...w,...e.additionalUOMs},o=Object.entries(c).map(ss),d=o.length;return a.split(Z).map(p=>p.trim()).filter(Boolean).map(p=>{let s={quantity:null,quantity2:null,unitOfMeasureID:null,unitOfMeasure:null,description:"",isGroupHeader:!1};if(isNaN(f(p[0]))){let t=b.exec(p);if(t&&e.ignoreUOMs.includes(t.at(-1)??""))s.description=p;else if(t){s.description=p.replace(b,"").trim();let r=t[3],i=t[12];r?(s.quantity=f(r),s.quantity2=f(i)):s.quantity=f(i);let l=t.at(-1);if(l){let g="",I="",x=-1;for(;++x<d&&!g;){let{alternates:X,id:M,short:V,plural:C}=o[x];[...X,M,V,C].includes(l)&&(g=l,I=M)}g&&(s.unitOfMeasureID=I,s.unitOfMeasure=e.normalizeUOM?I:g)}}else s.description=p,(s.description.endsWith(":")||v.test(s.description))&&(s.isGroupHeader=!0)}else{let t=6,r=NaN;for(;t>0&&isNaN(r);)r=f(p.substring(0,t).trim()),r>-1&&(s.quantity=r,s.description=p.substring(t).trim()),t--}let m=D.exec(s.description);if(m){let t=m[1].length,r=f(s.description.substring(t).trim()[0]);if(!isNaN(r)){let i=7,l=NaN;for(;--i>0&&isNaN(l);)l=f(s.description.substring(t,i)),isNaN(l)||(s.quantity2=l,s.description=s.description.substring(i).trim())}}let u=$.exec(s.description);if(u){let t=u[1].replace(/\s+/g," "),r=(u[2]??"").trim();if(r){let i="",l="",g=-1;for(;++g<d&&!i;){let{alternates:I,id:x,short:X,plural:M}=o[g];[...I,x,X,M].filter(C=>!e.ignoreUOMs.includes(C)).includes(t)&&(i=t,l=x)}i&&(s.unitOfMeasureID=l,s.unitOfMeasure=e.normalizeUOM?l:i,s.description=r)}}return!e.allowLeadingOf&&s.description.match(y)&&(s.description=s.description.replace(y,"")),s})};return S(es);})(); | ||
//# sourceMappingURL=parse-ingredient.umd.min.js.map |
{ | ||
"name": "parse-ingredient", | ||
"author": "Jake Boone", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "description": "Recipe ingredient parser with support for mixed numbers and vulgar fractions", |
@@ -67,3 +67,3 @@ [![npm][badge-npm]](https://www.npmjs.com/package/parse-ingredient) | ||
<script> | ||
console.log(ParseIngredient.parseIngredient('1 1/2 cups sugar')); | ||
ParseIngredient.parseIngredient('1 1/2 cups sugar'); | ||
// [ | ||
@@ -87,3 +87,3 @@ // { | ||
console.log(parseIngredient('1-2 pears')); | ||
parseIngredient('1-2 pears'); | ||
// [ | ||
@@ -99,7 +99,6 @@ // { | ||
// ] | ||
console.log( | ||
parseIngredient( | ||
`2/3 cup flour | ||
parseIngredient( | ||
`2/3 cup flour | ||
1 tsp baking powder` | ||
) | ||
); | ||
@@ -124,3 +123,3 @@ // [ | ||
// ] | ||
console.log(parseIngredient('For cake:')); | ||
parseIngredient('For cake:'); | ||
// [ | ||
@@ -136,3 +135,3 @@ // { | ||
// ] | ||
console.log(parseIngredient('Ripe tomato x2')); | ||
parseIngredient('Ripe tomato x2'); | ||
// [ | ||
@@ -156,6 +155,4 @@ // { | ||
Example: | ||
```js | ||
console.log(parseIngredient('1 c sugar', { normalizeUOM: true })); | ||
parseIngredient('1 c sugar', { normalizeUOM: true }); | ||
// [ | ||
@@ -177,16 +174,12 @@ // { | ||
Example: | ||
```js | ||
console.log( | ||
parseIngredient('2 buckets of widgets', { | ||
additionalUOMs: { | ||
bucket: { | ||
short: 'bkt', | ||
plural: 'buckets', | ||
versions: ['bk'], | ||
}, | ||
parseIngredient('2 buckets of widgets', { | ||
additionalUOMs: { | ||
bucket: { | ||
short: 'bkt', | ||
plural: 'buckets', | ||
versions: ['bk'], | ||
}, | ||
}) | ||
); | ||
}, | ||
}); | ||
// [ | ||
@@ -208,6 +201,4 @@ // { | ||
Example: | ||
```js | ||
console.log(parseIngredient('1 cup of sugar', { allowLeadingOf: true })); | ||
parseIngredient('1 cup of sugar', { allowLeadingOf: true }); | ||
// [ | ||
@@ -225,2 +216,20 @@ // { | ||
### `ignoreUOMs` | ||
An array of strings to ignore as units of measure when parsing ingredients. | ||
```js | ||
parseIngredient('2 large eggs', { ignoreUOMs: ['large'] }); | ||
// [ | ||
// { | ||
// quantity: 2, | ||
// quantity2: null, | ||
// unitOfMeasure: null, | ||
// unitOfMeasureID: null, | ||
// description: 'large eggs', | ||
// isGroupHeader: false, | ||
// } | ||
// ] | ||
``` | ||
## Contributors ✨ | ||
@@ -252,3 +261,3 @@ | ||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-3-orange.svg?style=flat-square)](#contributors-) | ||
[badge-all-contributors]: https://img.shields.io/badge/all_contributors-3-orange.svg | ||
<!-- ALL-CONTRIBUTORS-BADGE:END --> | ||
@@ -255,0 +264,0 @@ <!-- prettier-ignore-end --> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
240468
1929
257