UNPKG

numeric-quantity

Version:

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

2 lines 5.25 kB
const e=[48,1632,1776,1984,2406,2534,2662,2790,2918,3046,3174,3302,3430,3558,3664,3792,3872,4160,4240,6112,6160,6470,6608,6784,6800,6992,7088,7232,7248,42528,43216,43264,43472,43504,43600,44016,65296,66720,68912,68928,69734,69872,69942,70096,70384,70736,70864,71248,71360,71376,71386,71472,71904,72016,72688,72784,73040,73120,73184,73552,90416,92768,92864,93008,93552,118e3,120782,120792,120802,120812,120822,123200,123632,124144,124401,125264,130032],t=t=>t.replace(/\p{Nd}/gu,t=>{let n=t.codePointAt(0);if(n<=57)return t;let r=0,i=e.length-1;for(;r<i;){let t=r+i+1>>>1;e[t]<=n?r=t:i=t-1}return String(n-e[r])}),n={"⁰":`0`,"¹":`1`,"²":`2`,"³":`3`,"⁴":`4`,"⁵":`5`,"⁶":`6`,"⁷":`7`,"⁸":`8`,"⁹":`9`,"₀":`0`,"₁":`1`,"₂":`2`,"₃":`3`,"₄":`4`,"₅":`5`,"₆":`6`,"₇":`7`,"₈":`8`,"₉":`9`},r=/[⁰¹²³⁴⁵⁶⁷⁸⁹₀₁₂₃₄₅₆₇₈₉]/g,i={"¼":`1/4`,"½":`1/2`,"¾":`3/4`,"⅐":`1/7`,"⅑":`1/9`,"⅒":`1/10`,"⅓":`1/3`,"⅔":`2/3`,"⅕":`1/5`,"⅖":`2/5`,"⅗":`3/5`,"⅘":`4/5`,"⅙":`1/6`,"⅚":`5/6`,"⅛":`1/8`,"⅜":`3/8`,"⅝":`5/8`,"⅞":`7/8`,"⅟":`1/`},a=/^(?=[-+]?\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)*)?$/,o=/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:!1,percentage:!1,verbose:!1},p=e=>{let t=`${e}`.replace(u,(e,t)=>l[t]).toUpperCase(),n=d.exec(t);if(!n)return NaN;let[,r,i,a,o]=n;return(c[r]??0)+(c[i]??0)+(c[a]??0)+(c[o]??0)},m=/^\s*\//,h=/^([-+]?)\s*(\p{Sc}+)\s*/u,g=/\s*(\p{Sc}+)$/u,_=/%$/;function v(e,a=f){let c={...f,...a},l=typeof e==`string`?e:`${e}`,u,d,v,y,b,x,S,C,w=e=>{let t={value:e,input:l};return u&&(t.currencyPrefix=u),d&&(t.currencySuffix=d),v&&(t.percentageSuffix=v),y&&(t.trailingInvalid=y),b&&(t.sign=b),x!==void 0&&(t.whole=x),S!==void 0&&(t.numerator=S),C!==void 0&&(t.denominator=C),t},T=e=>c.verbose?w(e):e;if(typeof e==`number`||typeof e==`bigint`)return T(e);let E=NaN,D=l;if(c.allowCurrency){let e=h.exec(D);e&&e[2]&&(u=e[2],D=(e[1]||``)+D.slice(e[0].length))}if(c.allowCurrency){let e=g.exec(D);e&&(d=e[1],D=D.slice(0,-e[0].length))}c.percentage&&_.test(D)&&(v=!0,D=D.slice(0,-1));let O=t(D.replace(s,(e,t)=>` ${i[t]}`).replace(r,e=>n[e]).replace(`⁄`,`/`).trim());if(O.length===0)return T(NaN);let k=O;if(c.decimalSeparator===`,`){let e=(O.match(/,/g)||[]).length;if(e===1)k=O.replaceAll(`.`,`_`).replace(`,`,`.`);else if(e>1){if(!c.allowTrailingInvalid)return T(NaN);let e=O.indexOf(`,`),t=O.indexOf(`,`,e+1),n=O.substring(0,t).replaceAll(`.`,`_`).replace(`,`,`.`),r=O.substring(t+1);k=c.allowTrailingInvalid?n+`&`+r:n}else k=O.replaceAll(`.`,`_`)}let A=o.exec(k);if(!A)return T(c.romanNumerals?p(O):NaN);let j=(A[7]||k.slice(A[0].length)).trim();if(j&&(y=j,!c.allowTrailingInvalid))return T(NaN);let[,M,N,P]=A;(M===`-`||M===`+`)&&(b=M);let F=N.replaceAll(`,`,``).replaceAll(`_`,``),I=P==null?void 0:P.replaceAll(`,`,``).replaceAll(`_`,``);if(!F&&I&&I.startsWith(`.`))E=0;else{if(c.bigIntOnOverflow){let e=BigInt(M===`-`?`-${F}`:F);if(e>BigInt(2**53-1)||e<BigInt(-(2**53-1)))return T(e)}E=parseInt(F)}if(!I)return E=M===`-`?E*-1:E,v&&c.percentage!==`number`&&(E/=100),T(E);let L=c.round===!1?NaN:parseFloat(`1e${Math.floor(Math.max(0,c.round))}`);if(I.startsWith(`.`)||I.startsWith(`e`)||I.startsWith(`E`)){let e=parseFloat(`${E}${I}`);E=isNaN(L)?e:Math.round(e*L)/L}else if(m.test(I)){let e=parseInt(F),t=parseInt(I.replace(`/`,``));S=e,C=t,E=isNaN(L)?e/t:Math.round(e*L/t)/L}else{let[e,t]=I.split(`/`).map(e=>parseInt(e));x=E,S=e,C=t,E+=isNaN(L)?e/t:Math.round(e*L/t)/L}return E=M===`-`?E*-1:E,v&&c.percentage!==`number`&&(E=isNaN(L)?E/100:Math.round(E/100*L)/L),T(E)}const y=(e,t)=>{let n=v(e,{...t,verbose:!1});return typeof n==`bigint`||!isNaN(n)};export{f as defaultOptions,y as isNumericQuantity,t as normalizeDigits,v as numericQuantity,a as numericRegex,o as numericRegexWithTrailingInvalid,p as parseRomanNumerals,d as romanNumeralRegex,u as romanNumeralUnicodeRegex,l as romanNumeralUnicodeToAsciiMap,c as romanNumeralValues,n as superSubDigitToAsciiMap,r as superSubDigitsRegex,i as vulgarFractionToAsciiMap,s as vulgarFractionsRegex}; //# sourceMappingURL=numeric-quantity.production.mjs.map