UNPKG

numeric-quantity

Version:

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

2 lines 5.32 kB
var NumericQuantity=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=[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],n=e=>e.replace(/\p{Nd}/gu,e=>{let n=e.codePointAt(0);if(n<=57)return e;let r=0,i=t.length-1;for(;r<i;){let e=r+i+1>>>1;t[e]<=n?r=e:i=e-1}return String(n-t[r])}),r={"⁰":`0`,"¹":`1`,"²":`2`,"³":`3`,"⁴":`4`,"⁵":`5`,"⁶":`6`,"⁷":`7`,"⁸":`8`,"⁹":`9`,"₀":`0`,"₁":`1`,"₂":`2`,"₃":`3`,"₄":`4`,"₅":`5`,"₆":`6`,"₇":`7`,"₈":`8`,"₉":`9`},i=/[⁰¹²³⁴⁵⁶⁷⁸⁹₀₁₂₃₄₅₆₇₈₉]/g,a={"¼":`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/`},o=/^(?=[-+]?\s*\.\d|[-+]?\s*\d)([-+])?\s*((?:\d(?:[,_]\d|\d)*)*)(([eE][+-]?\d(?:[,_]\d|\d)*)?|\.\d(?:[,_]\d|\d)*([eE][+-]?\d(?:[,_]\d|\d)*)?|(\s+\d(?:[,_]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1,romanNumerals:!1,bigIntOnOverflow:!1,decimalSeparator:`.`,allowCurrency:!1,percentage:!1,verbose:!1},m=e=>{let t=`${e}`.replace(d,(e,t)=>u[t]).toUpperCase(),n=f.exec(t);if(!n)return NaN;let[,r,i,a,o]=n;return(l[r]??0)+(l[i]??0)+(l[a]??0)+(l[o]??0)},h=/^\s*\//,g=/^([-+]?)\s*(\p{Sc}+)\s*/u,_=/\s*(\p{Sc}+)$/u,v=/%$/;function y(e,t=p){let o={...p,...t},l=typeof e==`string`?e:`${e}`,u,d,f,y,b,x,S,C,w=e=>{let t={value:e,input:l};return u&&(t.currencyPrefix=u),d&&(t.currencySuffix=d),f&&(t.percentageSuffix=f),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=>o.verbose?w(e):e;if(typeof e==`number`||typeof e==`bigint`)return T(e);let E=NaN,D=l;if(o.allowCurrency){let e=g.exec(D);e&&e[2]&&(u=e[2],D=(e[1]||``)+D.slice(e[0].length))}if(o.allowCurrency){let e=_.exec(D);e&&(d=e[1],D=D.slice(0,-e[0].length))}o.percentage&&v.test(D)&&(f=!0,D=D.slice(0,-1));let O=n(D.replace(c,(e,t)=>` ${a[t]}`).replace(i,e=>r[e]).replace(`⁄`,`/`).trim());if(O.length===0)return T(NaN);let k=O;if(o.decimalSeparator===`,`){let e=(O.match(/,/g)||[]).length;if(e===1)k=O.replaceAll(`.`,`_`).replace(`,`,`.`);else if(e>1){if(!o.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=o.allowTrailingInvalid?n+`&`+r:n}else k=O.replaceAll(`.`,`_`)}let A=s.exec(k);if(!A)return T(o.romanNumerals?m(O):NaN);let j=(A[7]||k.slice(A[0].length)).trim();if(j&&(y=j,!o.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(o.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,f&&o.percentage!==`number`&&(E/=100),T(E);let L=o.round===!1?NaN:parseFloat(`1e${Math.floor(Math.max(0,o.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(h.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,f&&o.percentage!==`number`&&(E=isNaN(L)?E/100:Math.round(E/100*L)/L),T(E)}return e.defaultOptions=p,e.isNumericQuantity=(e,t)=>{let n=y(e,{...t,verbose:!1});return typeof n==`bigint`||!isNaN(n)},e.normalizeDigits=n,e.numericQuantity=y,e.numericRegex=o,e.numericRegexWithTrailingInvalid=s,e.parseRomanNumerals=m,e.romanNumeralRegex=f,e.romanNumeralUnicodeRegex=d,e.romanNumeralUnicodeToAsciiMap=u,e.romanNumeralValues=l,e.superSubDigitToAsciiMap=r,e.superSubDigitsRegex=i,e.vulgarFractionToAsciiMap=a,e.vulgarFractionsRegex=c,e})({}); //# sourceMappingURL=numeric-quantity.iife.umd.min.js.map