UNPKG

numeric-quantity

Version:

Number parser with support for mixed numbers, vulgar fractions, and Roman numerals

2 lines 2.89 kB
var N={"\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/"},g=/^(?=-?\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)?)?$/,y=new RegExp(g.source.replace(/\$$/,"(?:\\s*[^\\.\\d\\/].*)?")),V=new RegExp(`(${Object.keys(N).join("|")})`),c={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},d={"\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"},X=new RegExp(`(${Object.keys(d).join("|")})`,"gi"),b=/^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i,R={round:3,allowTrailingInvalid:!1,romanNumerals:!1,bigIntOnOverflow:!1};var M=i=>{let p=`${i}`.replace(X,(f,I)=>d[I]).toUpperCase(),e=b.exec(p);if(!e)return NaN;let[,s,a,l,u]=e;return(c[s]??0)+(c[a]??0)+(c[l]??0)+(c[u]??0)};var A=/^\s*\//;function _(i,p=R){if(typeof i=="number"||typeof i=="bigint")return i;let e=NaN,s=`${i}`.replace(V,(n,o)=>` ${N[o]}`).replace("\u2044","/").trim();if(s.length===0)return NaN;let a={...R,...p},l=(a.allowTrailingInvalid?y:g).exec(s);if(!l)return a.romanNumerals?M(s):NaN;let[,u,f,I]=l,m=f.replace(/[,_]/g,""),r=I?.replace(/[,_]/g,"");if(!m&&r&&r.startsWith("."))e=0;else{if(a.bigIntOnOverflow){let n=BigInt(u?`-${m}`:m);if(n>BigInt(Number.MAX_SAFE_INTEGER)||n<BigInt(Number.MIN_SAFE_INTEGER))return n}e=parseInt(m)}if(!r)return u?e*-1:e;let t=a.round===!1?NaN:parseFloat(`1e${Math.floor(Math.max(0,a.round))}`);if(r.startsWith(".")||r.startsWith("e")||r.startsWith("E")){let n=parseFloat(`${e}${r}`);e=isNaN(t)?n:Math.round(n*t)/t}else if(A.test(r)){let n=parseInt(m),o=parseInt(r.replace("/",""));e=isNaN(t)?n/o:Math.round(n*t/o)/t}else{let n=r.split("/"),[o,x]=n.map($=>parseInt($));e+=isNaN(t)?o/x:Math.round(o*t/x)/t}return u?e*-1:e}export{R as defaultOptions,_ as numericQuantity,g as numericRegex,y as numericRegexWithTrailingInvalid,M as parseRomanNumerals,b as romanNumeralRegex,X as romanNumeralUnicodeRegex,d as romanNumeralUnicodeToAsciiMap,c as romanNumeralValues,N as vulgarFractionToAsciiMap,V as vulgarFractionsRegex}; //# sourceMappingURL=numeric-quantity.production.mjs.map