format-quantity 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- var d=.0075,e={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},s={"\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"},m=[[.33,"\u2153"],[.66,"\u2154"],[.2,"\u2155"],[.4,"\u2156"],[.6,"\u2157"],[.8,"\u2158"],[.166,"\u2159"],[.833,"\u215A"],[.143,"\u2150"],[.111,"\u2151"],[.1,"\u2152"],[.125,"\u215B"],[.25,"\xBC"],[.375,"\u215C"],[.5,"\xBD"],[.625,"\u215D"],[.75,"\xBE"],[.875,"\u215E"],[.0625,"1/16"],[.1875,"3/16"],[.3125,"5/16"],[.4375,"7/16"],[.5625,"9/16"],[.6875,"11/16"],[.8125,"13/16"],[.9375,"15/16"]];var F=(t,a,o)=>Math.abs(t-a)<o,V=(t,{fractionSlash:a,vulgarFractions:o})=>{if(o)return t;let r=s[t]??t;return a?r.replace("/","\u2044"):r},C=t=>({...e,...typeof t=="boolean"?{vulgarFractions:t}:t}),X=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],y=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let o=`${Math.floor(t)}`.split(""),r="",n=3;for(;n--;)r=`${X[+o.pop()+n*10]||""}${r}`;return`${Array(+o.join("")+1).join("M")}${r}`},I=(t,a=e)=>{let o=typeof t=="string"?parseFloat(t):t;if(isNaN(o)||o===null)return null;if(o===0)return"";let r=C(a??e);if(r.romanNumerals)return y(o);let n=Math.abs(o),i=Math.floor(n),l=`${o<0?"-":""}${i===0?"":`${i} `}`,c=n-i;if(c===0)return`${o}`;for(let[f,p]of m)if(F(c,f,r.tolerance)){let u=V(p,r);return`${u in s?l.trim():l}${u}`}return`${o}`};export{e as defaultOptions,d as defaultTolerance,I as formatQuantity,m as fractionDecimalMatches,s as vulgarToAsciiMap};
1
+ import{numericQuantity as e}from"numeric-quantity";const t=.0075,n={vulgarFractions:!1,tolerance:t,fractionSlash:!1,romanNumerals:!1},r={"¼":`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`},i=[[.33,`⅓`],[.66,`⅔`],[.2,`⅕`],[.4,`⅖`],[.6,`⅗`],[.8,`⅘`],[.166,`⅙`],[.833,`⅚`],[.143,`⅐`],[.111,`⅑`],[.1,`⅒`],[.125,`⅛`],[.25,`¼`],[.375,`⅜`],[.5,`½`],[.625,`⅝`],[.75,`¾`],[.875,`⅞`],[.0625,`1/16`],[.1875,`3/16`],[.3125,`5/16`],[.4375,`7/16`],[.5625,`9/16`],[.6875,`11/16`],[.8125,`13/16`],[.9375,`15/16`]],a=(e,t,n)=>Math.abs(e-t)<n,o=e=>{let t=``;for(let n=0;n<e.length;n++)t+=`⁰¹²³⁴⁵⁶⁷⁸⁹`[+e[n]];return t},s=e=>{let t=``;for(let n=0;n<e.length;n++)t+=`₀₁₂₃₄₅₆₇₈₉`[+e[n]];return t},c=(e,{fractionSlash:t,vulgarFractions:n})=>{if(n)return e;let i=r[e]??e;if(t){let[e,t]=i.split(`/`);return`${o(e)}⁄${s(t)}`}return i},l=e=>({...n,...typeof e==`boolean`?{vulgarFractions:e}:e}),u=`.C.CC.CCC.CD.D.DC.DCC.DCCC.CM..X.XX.XXX.XL.L.LX.LXX.LXXX.XC..I.II.III.IV.V.VI.VII.VIII.IX`.split(`.`),d=e=>{if(typeof e!=`number`||isNaN(e))return null;if(e<1||e>=4e3)return``;let t=`${Math.floor(e)}`.split(``),n=``,r=3;for(;r--;)n=`${u[+t.pop()+r*10]||``}${n}`;return`${Array(+t.join(``)+1).join(`M`)}${n}`},f=(t,o=n)=>{let s=typeof t==`string`?e(t,{round:!1,allowTrailingInvalid:!0}):t;if(isNaN(s)||s===null)return null;if(s===0)return``;let u=l(o??n);if(u.romanNumerals)return d(s);let f=Math.abs(s),p=Math.floor(f),m=s<0?`-`:``,h=p===0?``:`${p}`,g=f-p;if(g===0)return`${s}`;for(let[e,t]of i)if(a(g,e,u.tolerance)){let e=c(t,u),n=e in r;return`${m}${h}${h?u.separator??(n?``:` `):``}${e}`}return`${s}`};export{n as defaultOptions,t as defaultTolerance,f as formatQuantity,d as formatRomanNumerals,i as fractionDecimalMatches,r as vulgarToAsciiMap};
2
2
  //# sourceMappingURL=format-quantity.production.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"AAWO,IAAMA,EAAmB,MAKnBC,EAAiB,CAC5B,gBAAiB,GACjB,UAAW,MACX,cAAe,GACf,cAAe,EACjB,EAKaC,EAAmB,CAC9B,OAAK,MACL,OAAK,MACL,OAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,OACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,KACP,EAMaC,EAAyB,CACpC,CAAC,IAAM,QAAG,EACV,CAAC,IAAM,QAAG,EACV,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,GAAK,QAAG,EACT,CAAC,KAAO,QAAG,EACX,CAAC,IAAM,MAAG,EACV,CAAC,KAAO,QAAG,EACX,CAAC,GAAK,MAAG,EACT,CAAC,KAAO,QAAG,EACX,CAAC,IAAM,MAAG,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ASaE,EAAiC,CAC5CJ,EACAJ,EAAUC,IACP,CAEH,IAAMQ,EAAc,OAAOL,GAAQ,SAAW,WAAWA,CAAG,EAAIA,EAGhE,GAAI,MAAMK,CAAW,GAAKA,IAAgB,KACxC,OAAO,KAIT,GAAIA,IAAgB,EAClB,MAAO,GAMT,IAAMC,EAAOX,EAAiBC,GAAWC,CAAc,EAEvD,GAAIS,EAAK,cACP,OAAOP,EAAoBM,CAAW,EAGxC,IAAME,EAAgB,KAAK,IAAIF,CAAW,EACpCG,EAAgB,KAAK,MAAMD,CAAa,EACxCE,EAAmB,GAAGJ,EAAc,EAAI,IAAM,EAAE,GACpDG,IAAkB,EAAI,GAAK,GAAGA,CAAa,GAC7C,GACME,EAAeH,EAAgBC,EAGrC,GAAIE,IAAiB,EACnB,MAAO,GAAGL,CAAW,GAGvB,OAAW,CAACM,EAAKC,CAAE,IAAKC,EACtB,GAAI5B,EAAYyB,EAAcC,EAAKL,EAAK,SAAS,EAAG,CAClD,IAAMQ,EAAWzB,EAAYuB,EAAIN,CAAI,EAKrC,MAAO,GAHLQ,KAAYpB,EACRe,EAAiB,KAAK,EACtBA,CACO,GAAGK,CAAQ,EAC1B,CAGF,MAAO,GAAGT,CAAW,EACvB","names":["defaultTolerance","defaultOptions","vulgarToAsciiMap","fractionDecimalMatches","closeEnough","n1","n2","tolerance","getFraction","vulgarFractionOrSixteenth","fractionSlash","vulgarFractions","plainFraction","vulgarToAsciiMap","normalizeOptions","options","defaultOptions","romanNumeralValueKey","formatRomanNumerals","qty","digits","roman","i","formatQuantity","qtyAsNumber","opts","absoluteValue","flooredAbsVal","flooredAbsValStr","decimalValue","num","vf","fractionDecimalMatches","fraction"]}
1
+ {"version":3,"file":"format-quantity.production.mjs","names":[],"sources":["../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import type {\n ResolvedFormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions: ResolvedFormatQuantityOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap: Record<VulgarFraction, SimpleFraction> = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches: [number, VulgarFraction | Sixteenth][] = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] as const;\n","import { numericQuantity } from 'numeric-quantity';\nimport {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n ResolvedFormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\nconst superscriptDigits = '⁰¹²³⁴⁵⁶⁷⁸⁹';\nconst subscriptDigits = '₀₁₂₃₄₅₆₇₈₉';\n\nconst toSuperscript = (s: string) => {\n let r = '';\n for (let i = 0; i < s.length; i++) r += superscriptDigits[+s[i]];\n return r;\n};\nconst toSubscript = (s: string) => {\n let r = '';\n for (let i = 0; i < s.length; i++) r += subscriptDigits[+s[i]];\n return r;\n};\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n const [num, den] = plainFraction.split('/');\n return `${toSuperscript(num)}⁄${toSubscript(den)}`;\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): ResolvedFormatQuantityOptions => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number): string | null => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n const qtyAsNumber =\n typeof qty === 'string'\n ? numericQuantity(qty, { round: false, allowTrailingInvalid: true })\n : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n // TODO: Consider a `zeroDisplay` option (e.g. `{ zeroDisplay: \"0\" }`) so\n // callers outside the recipe-ingredient use case can get \"0\" instead of \"\".\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const sign = qtyAsNumber < 0 ? '-' : '';\n const wholeStr = flooredAbsVal === 0 ? '' : `${flooredAbsVal}`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const isVulgar = fraction in vulgarToAsciiMap;\n const sep = wholeStr\n ? (opts.separator ?? (isVulgar ? '' : ' '))\n : '';\n return `${sign}${wholeStr}${sep}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"mDAWA,MAAa,EAAmB,MAKnB,EAAgD,CAC3D,gBAAiB,GACjB,UAAW,EACX,cAAe,GACf,cAAe,GAChB,CAKY,EAA2D,CACtE,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,OACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACL,IAAK,MACN,CAMY,EAAiE,CAC5E,CAAC,IAAM,IAAI,CACX,CAAC,IAAM,IAAI,CACX,CAAC,GAAK,IAAI,CACV,CAAC,GAAK,IAAI,CACV,CAAC,GAAK,IAAI,CACV,CAAC,GAAK,IAAI,CACV,CAAC,KAAO,IAAI,CACZ,CAAC,KAAO,IAAI,CACZ,CAAC,KAAO,IAAI,CACZ,CAAC,KAAO,IAAI,CACZ,CAAC,GAAK,IAAI,CACV,CAAC,KAAO,IAAI,CACZ,CAAC,IAAM,IAAI,CACX,CAAC,KAAO,IAAI,CACZ,CAAC,GAAK,IAAI,CACV,CAAC,KAAO,IAAI,CACZ,CAAC,IAAM,IAAI,CACX,CAAC,KAAO,IAAI,CACZ,CAAC,MAAQ,OAAO,CAChB,CAAC,MAAQ,OAAO,CAChB,CAAC,MAAQ,OAAO,CAChB,CAAC,MAAQ,OAAO,CAChB,CAAC,MAAQ,OAAO,CAChB,CAAC,MAAQ,QAAQ,CACjB,CAAC,MAAQ,QAAQ,CACjB,CAAC,MAAQ,QAAQ,CAClB,CC3DK,GAAe,EAAY,EAAY,IAC3C,KAAK,IAAI,EAAK,EAAG,CAAG,EAKhB,EAAiB,GAAc,CACnC,IAAI,EAAI,GACR,IAAK,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CACrC,EAAgB,KAAK,MAAM,EAAc,CACzC,EAAO,EAAc,EAAI,IAAM,GAC/B,EAAW,IAAkB,EAAI,GAAK,GAAG,IACzC,EAAe,EAAgB,EAGrC,GAAI,IAAiB,EACnB,MAAO,GAAG,IAGZ,IAAK,GAAM,CAAC,EAAK,KAAO,EACtB,GAAI,EAAY,EAAc,EAAK,EAAK,UAAU,CAAE,CAClD,IAAM,EAAW,EAAY,EAAI,EAAK,CAChC,EAAW,KAAY,EAI7B,MAAO,GAAG,IAAO,IAHL,EACP,EAAK,YAAc,EAAW,GAAK,KACpC,KAC8B,IAItC,MAAO,GAAG"}
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "3.0.0",
2
+ "version": "3.1.0",
3
3
  "name": "format-quantity",
4
4
  "author": "Jake Boone <jakeboone02@gmail.com>",
5
5
  "description": "Number formatter for imperial measurements with support for vulgar fractions",
@@ -16,12 +16,12 @@
16
16
  "default": "./dist/format-quantity.mjs"
17
17
  },
18
18
  "require": {
19
- "types": "./dist/cjs/format-quantity.cjs.development.d.ts",
19
+ "types": "./dist/cjs/index.d.ts",
20
20
  "default": "./dist/cjs/index.js"
21
21
  }
22
22
  }
23
23
  },
24
- "types": "./dist/cjs/format-quantity.cjs.production.d.ts",
24
+ "types": "./dist/format-quantity.legacy-esm.d.ts",
25
25
  "unpkg": "./dist/format-quantity.umd.min.js",
26
26
  "license": "MIT",
27
27
  "keywords": [
@@ -45,30 +45,36 @@
45
45
  "url": "https://github.com/jakeboone02/format-quantity"
46
46
  },
47
47
  "scripts": {
48
- "start": "bun --hot ./server.ts",
49
- "build": "tsup",
50
- "docs": "bunx typedoc",
48
+ "start": "bun --hot main.html",
49
+ "build": "bun --bun x tsdown",
50
+ "docs:plugins": "bun build ./node_modules/typedoc-plugin-katex/src/index.tsx --outdir=. --target=node --external=typedoc --entry-naming=typedoc-plugin-katex.mjs",
51
+ "docs": "typedoc",
51
52
  "test": "bun test",
52
53
  "watch": "bun test --watch",
54
+ "lint": "bunx oxlint --type-aware --report-unused-disable-directives --type-check",
53
55
  "publish:npm": "np",
54
- "pretty-print": "prettier --write *.{mjs,ts,json} src/*.*"
56
+ "codesandbox-ci": "bash .codesandbox/ci.sh",
57
+ "pretty-print": "prettier --write '*.{html,ts,json}' 'src/*.*'"
55
58
  },
56
59
  "devDependencies": {
57
- "@types/node": "^20.11.2",
58
- "@types/web": "^0.0.135",
59
- "bun-types": "^1.0.22",
60
- "np": "^9.2.0",
61
- "open": "^10.0.3",
62
- "prettier": "^3.2.2",
63
- "tsup": "^8.0.1",
64
- "typedoc": "^0.25.7",
60
+ "@types/bun": "^1.3.9",
61
+ "@types/node": "^25.2.3",
62
+ "@types/web": "^0.0.332",
63
+ "@typescript/native-preview": "^7.0.0-dev.20260212.1",
64
+ "np": "^11.0.2",
65
+ "oxlint": "^1.47.0",
66
+ "oxlint-tsgolint": "^0.12.1",
67
+ "prettier": "3.8.1",
68
+ "prettier-plugin-organize-imports": "4.3.0",
69
+ "tsdown": "^0.20.3",
70
+ "typedoc": "^0.28.16",
65
71
  "typedoc-plugin-katex": "^0.1.2",
66
- "typescript": "^5.3.3"
67
- },
68
- "publishConfig": {
69
- "registry": "https://registry.npmjs.org"
72
+ "typescript": "^5.9.3"
70
73
  },
71
74
  "engines": {
72
75
  "node": ">=16"
76
+ },
77
+ "dependencies": {
78
+ "numeric-quantity": "^3.2.0"
73
79
  }
74
80
  }
@@ -1,113 +0,0 @@
1
- interface FormatQuantityOptions {
2
- /**
3
- * Output vulgar fractions, like "½" instead of "1/2", when appropriate.
4
- * Overrides the `fractionSlash` option.
5
- */
6
- vulgarFractions?: boolean;
7
- /**
8
- * Amount by which a number can deviate from the calculated quotient to be
9
- * considered a match. For example, 0.66 is close enough to 2 ÷ 3 (which
10
- * is 0.66666... repeating) to be considered equivalent so the function
11
- * will return "2/3". The smaller this number, the higher the likelihood that
12
- * the function will return a decimal instead of a fraction or mixed number.
13
- *
14
- * @default 0.0075
15
- */
16
- tolerance?: number;
17
- /**
18
- * Output the fraction slash character (⁄) instead of the "solidus"
19
- * slash (/) for fractions. Results appear like "1⁄2" instead of "1/2".
20
- * Overridden by the `vulgarFractions` option.
21
- */
22
- fractionSlash?: boolean;
23
- /**
24
- * Output in Roman numerals. Provided value must be between 1 and 3999, inclusive.
25
- * Decimal values will be ignored (`Math.floor` is used to remove them). Overrides
26
- * all other options.
27
- */
28
- romanNumerals?: boolean;
29
- }
30
- /**
31
- * Function signature of {@link formatQuantity}.
32
- */
33
- interface FormatQuantity {
34
- (qty: string | number, options?: boolean | FormatQuantityOptions): string | null;
35
- }
36
- /** Any numeric character. */
37
- type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
38
- /** Any numeric character except '0'. */
39
- type NonZeroDigit = Exclude<Digit, '0'>;
40
- /**
41
- * Fraction string with either one or two numeric characters in both the
42
- * numerator and denominator (but not two characters in the numerator while
43
- * the denominator only has one).
44
- */
45
- type SimpleFraction = `${NonZeroDigit}/${NonZeroDigit}` | `${NonZeroDigit}/${NonZeroDigit}${Digit}` | `${NonZeroDigit}${Digit}/${NonZeroDigit}${Digit}`;
46
- /**
47
- * Odd numerator sixteenth fraction strings.
48
- */
49
- type Sixteenth = `${'1' | '3' | '5' | '7' | '9' | '11' | '13' | '15'}/16`;
50
- /**
51
- * Unicode vulgar fraction code points.
52
- */
53
- type VulgarFraction = '¼' | '½' | '¾' | '⅐' | '⅑' | '⅒' | '⅓' | '⅔' | '⅕' | '⅖' | '⅗' | '⅘' | '⅙' | '⅚' | '⅛' | '⅜' | '⅝' | '⅞';
54
- /** @hidden */
55
- type FormatQuantityTests = Record<string, ([Parameters<FormatQuantity>[0], ReturnType<FormatQuantity>] | [
56
- Parameters<FormatQuantity>[0],
57
- ReturnType<FormatQuantity>,
58
- Parameters<FormatQuantity>[1]
59
- ])[]>;
60
-
61
- /**
62
- * Formats a number (or string that appears to be a number)
63
- * as one would see it written in imperial measurements, e.g.
64
- * "1 1/2" instead of "1.5". To use vulgar fraction characters
65
- * like "½", pass `true` as the second argument. For other options
66
- * see {@link FormatQuantityOptions}.
67
- */
68
- declare const formatQuantity: FormatQuantity;
69
-
70
- /**
71
- * Default tolerance used by {@link formatQuantity} when determining if a number
72
- * is close enough to a fraction value to be considered equivalent.
73
- */
74
- declare const defaultTolerance: 0.0075;
75
- /**
76
- * Default options for {@link formatQuantity}.
77
- */
78
- declare const defaultOptions: {
79
- readonly vulgarFractions: false;
80
- readonly tolerance: 0.0075;
81
- readonly fractionSlash: false;
82
- readonly romanNumerals: false;
83
- };
84
- /**
85
- * Map of vulgar fractions to their traditional ASCII equivalents.
86
- */
87
- declare const vulgarToAsciiMap: {
88
- readonly '\u00BC': "1/4";
89
- readonly '\u00BD': "1/2";
90
- readonly '\u00BE': "3/4";
91
- readonly '\u2150': "1/7";
92
- readonly '\u2151': "1/9";
93
- readonly '\u2152': "1/10";
94
- readonly '\u2153': "1/3";
95
- readonly '\u2154': "2/3";
96
- readonly '\u2155': "1/5";
97
- readonly '\u2156': "2/5";
98
- readonly '\u2157': "3/5";
99
- readonly '\u2158': "4/5";
100
- readonly '\u2159': "1/6";
101
- readonly '\u215A': "5/6";
102
- readonly '\u215B': "1/8";
103
- readonly '\u215C': "3/8";
104
- readonly '\u215D': "5/8";
105
- readonly '\u215E': "7/8";
106
- };
107
- /**
108
- * Map of "close enough" decimal values to the {@link VulgarFraction} or
109
- * {@link Sixteenth} fraction string matches.
110
- */
111
- declare const fractionDecimalMatches: ([number, "⅓"] | [number, "⅔"] | [number, "⅕"] | [number, "⅖"] | [number, "⅗"] | [number, "⅘"] | [number, "⅙"] | [number, "⅚"] | [number, "⅐"] | [number, "⅑"] | [number, "⅒"] | [number, "⅛"] | [number, "¼"] | [number, "⅜"] | [number, "½"] | [number, "⅝"] | [number, "¾"] | [number, "⅞"] | [number, "1/16"] | [number, "3/16"] | [number, "5/16"] | [number, "7/16"] | [number, "9/16"] | [number, "11/16"] | [number, "13/16"] | [number, "15/16"])[];
112
-
113
- export { type Digit, type FormatQuantity, type FormatQuantityOptions, type FormatQuantityTests, type NonZeroDigit, type SimpleFraction, type Sixteenth, type VulgarFraction, defaultOptions, defaultTolerance, formatQuantity, fractionDecimalMatches, vulgarToAsciiMap };
@@ -1,2 +0,0 @@
1
- "use strict";var FormatQuantity=(()=>{var l=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var X=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var I=(t,r)=>{for(var o in r)l(t,o,{get:r[o],enumerable:!0})},b=(t,r,o,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of X(r))!y.call(t,n)&&n!==o&&l(t,n,{get:()=>r[n],enumerable:!(a=C(r,n))||a.enumerable});return t};var d=t=>b(l({},"__esModule",{value:!0}),t);var Q={};I(Q,{defaultOptions:()=>e,defaultTolerance:()=>D,formatQuantity:()=>p,fractionDecimalMatches:()=>c,vulgarToAsciiMap:()=>i});var D=.0075,e={vulgarFractions:!1,tolerance:.0075,fractionSlash:!1,romanNumerals:!1},i={"\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"},c=[[.33,"\u2153"],[.66,"\u2154"],[.2,"\u2155"],[.4,"\u2156"],[.6,"\u2157"],[.8,"\u2158"],[.166,"\u2159"],[.833,"\u215A"],[.143,"\u2150"],[.111,"\u2151"],[.1,"\u2152"],[.125,"\u215B"],[.25,"\xBC"],[.375,"\u215C"],[.5,"\xBD"],[.625,"\u215D"],[.75,"\xBE"],[.875,"\u215E"],[.0625,"1/16"],[.1875,"3/16"],[.3125,"5/16"],[.4375,"7/16"],[.5625,"9/16"],[.6875,"11/16"],[.8125,"13/16"],[.9375,"15/16"]];var g=(t,r,o)=>Math.abs(t-r)<o,$=(t,{fractionSlash:r,vulgarFractions:o})=>{if(o)return t;let a=i[t]??t;return r?a.replace("/","\u2044"):a},h=t=>({...e,...typeof t=="boolean"?{vulgarFractions:t}:t}),x=["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM","","X","XX","XXX","XL","L","LX","LXX","LXXX","XC","","I","II","III","IV","V","VI","VII","VIII","IX"],M=t=>{if(typeof t!="number"||isNaN(t))return null;if(t<1||t>=4e3)return"";let o=`${Math.floor(t)}`.split(""),a="",n=3;for(;n--;)a=`${x[+o.pop()+n*10]||""}${a}`;return`${Array(+o.join("")+1).join("M")}${a}`},p=(t,r=e)=>{let o=typeof t=="string"?parseFloat(t):t;if(isNaN(o)||o===null)return null;if(o===0)return"";let a=h(r??e);if(a.romanNumerals)return M(o);let n=Math.abs(o),s=Math.floor(n),u=`${o<0?"-":""}${s===0?"":`${s} `}`,m=n-s;if(m===0)return`${o}`;for(let[F,V]of c)if(g(m,F,a.tolerance)){let f=$(V,a);return`${f in i?u.trim():u}${f}`}return`${o}`};return d(Q);})();
2
- //# sourceMappingURL=format-quantity.umd.min.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts","../src/constants.ts","../src/formatQuantity.ts"],"sourcesContent":["import { formatQuantity } from './formatQuantity';\nexport * from './constants';\nexport * from './types';\nexport { formatQuantity };\n","import type {\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Default tolerance used by {@link formatQuantity} when determining if a number\n * is close enough to a fraction value to be considered equivalent.\n */\nexport const defaultTolerance = 0.0075 as const;\n\n/**\n * Default options for {@link formatQuantity}.\n */\nexport const defaultOptions = {\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n} as const satisfies Required<FormatQuantityOptions>;\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap = {\n '¼': '1/4',\n '½': '1/2',\n '¾': '3/4',\n '⅐': '1/7',\n '⅑': '1/9',\n '⅒': '1/10',\n '⅓': '1/3',\n '⅔': '2/3',\n '⅕': '1/5',\n '⅖': '2/5',\n '⅗': '3/5',\n '⅘': '4/5',\n '⅙': '1/6',\n '⅚': '5/6',\n '⅛': '1/8',\n '⅜': '3/8',\n '⅝': '5/8',\n '⅞': '7/8',\n} as const satisfies Record<VulgarFraction, SimpleFraction>;\n\n/**\n * Map of \"close enough\" decimal values to the {@link VulgarFraction} or\n * {@link Sixteenth} fraction string matches.\n */\nexport const fractionDecimalMatches = [\n [0.33, '⅓'],\n [0.66, '⅔'],\n [0.2, '⅕'],\n [0.4, '⅖'],\n [0.6, '⅗'],\n [0.8, '⅘'],\n [0.166, '⅙'],\n [0.833, '⅚'],\n [0.143, '⅐'],\n [0.111, '⅑'],\n [0.1, '⅒'],\n [0.125, '⅛'],\n [0.25, '¼'],\n [0.375, '⅜'],\n [0.5, '½'],\n [0.625, '⅝'],\n [0.75, '¾'],\n [0.875, '⅞'],\n [0.0625, '1/16'],\n [0.1875, '3/16'],\n [0.3125, '5/16'],\n [0.4375, '7/16'],\n [0.5625, '9/16'],\n [0.6875, '11/16'],\n [0.8125, '13/16'],\n [0.9375, '15/16'],\n] satisfies [number, VulgarFraction | Sixteenth][];\n","import {\n defaultOptions,\n fractionDecimalMatches,\n vulgarToAsciiMap,\n} from './constants';\nimport type {\n FormatQuantity,\n FormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\n/**\n * Determines if two numbers are close enough to consider\n * them equal for the purposes of this package.\n */\nconst closeEnough = (n1: number, n2: number, tolerance: number) =>\n Math.abs(n1 - n2) < tolerance;\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: FormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ??\n vulgarFractionOrSixteenth;\n\n if (fractionSlash) {\n return plainFraction.replace('/', '⁄');\n }\n\n return plainFraction;\n};\n\n/**\n * Merges options object with default options, converting boolean to object if necessary.\n */\nconst normalizeOptions = (\n options: Parameters<FormatQuantity>[1]\n): Required<FormatQuantityOptions> => ({\n ...defaultOptions,\n ...(typeof options === 'boolean' ? { vulgarFractions: options } : options),\n});\n\n// prettier-ignore\nconst romanNumeralValueKey = [\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\",\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\",\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\",\n] as const;\n\n/**\n * Formats a number as Roman numerals. The number must be between\n * 1 and 3999, inclusive.\n */\nexport const formatRomanNumerals = (qty: number) => {\n if (typeof qty !== 'number' || isNaN(qty)) {\n return null;\n }\n\n if (qty < 1 || qty >= 4000) {\n return '';\n }\n\n const floored = Math.floor(qty);\n\n const digits = `${floored}`.split('');\n let roman = '';\n let i = 3;\n while (i--) {\n roman = `${romanNumeralValueKey[+digits.pop()! + i * 10] || ''}${roman}`;\n }\n\n return `${Array(+digits.join('') + 1).join('M')}${roman}`;\n};\n\n/**\n * Formats a number (or string that appears to be a number)\n * as one would see it written in imperial measurements, e.g.\n * \"1 1/2\" instead of \"1.5\". To use vulgar fraction characters\n * like \"½\", pass `true` as the second argument. For other options\n * see {@link FormatQuantityOptions}.\n */\nexport const formatQuantity: FormatQuantity = (\n qty,\n options = defaultOptions\n) => {\n // TODO: use numericQuantity instead of parseFloat?\n const qtyAsNumber = typeof qty === 'string' ? parseFloat(qty) : qty;\n\n // Return `null` if input is not number-like.\n if (isNaN(qtyAsNumber) || qtyAsNumber === null) {\n return null;\n }\n\n // Return an empty string if the value is zero.\n if (qtyAsNumber === 0) {\n return '';\n }\n\n // The default options parameter in the function signature only takes effect\n // if the parameter is `undefined`. The nullish coalescing operator below\n // covers the `null` case.\n const opts = normalizeOptions(options ?? defaultOptions);\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue = Math.abs(qtyAsNumber);\n const flooredAbsVal = Math.floor(absoluteValue);\n const flooredAbsValStr = `${qtyAsNumber < 0 ? '-' : ''}${\n flooredAbsVal === 0 ? '' : `${flooredAbsVal} `\n }`;\n const decimalValue = absoluteValue - flooredAbsVal;\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n for (const [num, vf] of fractionDecimalMatches) {\n if (closeEnough(decimalValue, num, opts.tolerance)) {\n const fraction = getFraction(vf, opts);\n const int =\n fraction in vulgarToAsciiMap\n ? flooredAbsValStr.trim()\n : flooredAbsValStr;\n return `${int}${fraction}`;\n }\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"kcAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,qBAAAC,EAAA,mBAAAC,EAAA,2BAAAC,EAAA,qBAAAC,ICWO,IAAMC,EAAmB,MAKnBC,EAAiB,CAC5B,gBAAiB,GACjB,UAAW,MACX,cAAe,GACf,cAAe,EACjB,EAKaC,EAAmB,CAC9B,OAAK,MACL,OAAK,MACL,OAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,OACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,MACL,SAAK,KACP,EAMaC,EAAyB,CACpC,CAAC,IAAM,QAAG,EACV,CAAC,IAAM,QAAG,EACV,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,GAAK,QAAG,EACT,CAAC,K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AE,EAChCE,EAAQ,GACRC,EAAI,EACR,KAAOA,KACLD,EAAQ,GAAGJ,EAAqB,CAACG,EAAO,IAAI,EAAKE,EAAI,EAAE,GAAK,EAAE,GAAGD,CAAK,GAGxE,MAAO,GAAG,MAAM,CAACD,EAAO,KAAK,EAAE,EAAI,CAAC,EAAE,KAAK,GAAG,CAAC,GAAGC,CAAK,EACzD,EASaE,EAAiC,CAC5CJ,EACAJ,EAAUC,IACP,CAEH,IAAMQ,EAAc,OAAOL,GAAQ,SAAW,WAAWA,CAAG,EAAIA,EAGhE,GAAI,MAAMK,CAAW,GAAKA,IAAgB,KACxC,OAAO,KAIT,GAAIA,IAAgB,EAClB,MAAO,GAMT,IAAMC,EAAOX,EAAiBC,GAAWC,CAAc,EAEvD,GAAIS,EAAK,cACP,OAAOP,EAAoBM,CAAW,EAGxC,IAAME,EAAgB,KAAK,IAAIF,CAAW,EACpCG,EAAgB,KAAK,MAAMD,CAAa,EACxCE,EAAmB,GAAGJ,EAAc,EAAI,IAAM,EAAE,GACpDG,IAAkB,EAAI,GAAK,GAAGA,CAAa,GAC7C,GACME,EAAeH,EAAgBC,EAGrC,GAAIE,IAAiB,EACnB,MAAO,GAAGL,CAAW,GAGvB,OAAW,CAACM,EAAKC,CAAE,IAAKC,EACtB,GAAI5B,EAAYyB,EAAcC,EAAKL,EAAK,SAAS,EAAG,CAClD,IAAMQ,EAAWzB,EAAYuB,EAAIN,CAAI,EAKrC,MAAO,GAHLQ,KAAYpB,EACRe,EAAiB,KAAK,EACtBA,CACO,GAAGK,CAAQ,EAC1B,CAGF,MAAO,GAAGT,CAAW,EACvB","names":["src_exports","__export","defaultOptions","defaultTolerance","formatQuantity","fractionDecimalMatches","vulgarToAsciiMap","defaultTolerance","defaultOptions","vulgarToAsciiMap","fractionDecimalMatches","closeEnough","n1","n2","tolerance","getFraction","vulgarFractionOrSixteenth","fractionSlash","vulgarFractions","plainFraction","vulgarToAsciiMap","normalizeOptions","options","defaultOptions","romanNumeralValueKey","formatRomanNumerals","qty","digits","roman","i","formatQuantity","qtyAsNumber","opts","absoluteValue","flooredAbsVal","flooredAbsValStr","decimalValue","num","vf","fractionDecimalMatches","fraction"]}