format-quantity 3.0.0 → 3.2.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.
- package/README.md +79 -19
- package/dist/cjs/format-quantity.cjs.development.d.ts +97 -79
- package/dist/cjs/format-quantity.cjs.development.js +207 -181
- package/dist/cjs/format-quantity.cjs.development.js.map +1 -1
- package/dist/cjs/format-quantity.cjs.production.d.ts +97 -79
- package/dist/cjs/format-quantity.cjs.production.js +1 -1
- package/dist/cjs/format-quantity.cjs.production.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -0
- package/dist/format-quantity.d.mts +97 -79
- package/dist/format-quantity.legacy-esm.d.ts +131 -0
- package/dist/format-quantity.legacy-esm.js +262 -166
- package/dist/format-quantity.legacy-esm.js.map +1 -1
- package/dist/format-quantity.mjs +202 -151
- package/dist/format-quantity.mjs.map +1 -1
- package/dist/format-quantity.production.d.mts +97 -79
- package/dist/format-quantity.production.mjs +1 -1
- package/dist/format-quantity.production.mjs.map +1 -1
- package/dist/format-quantity.umd.min.js +2 -1
- package/dist/format-quantity.umd.min.js.map +1 -1
- package/package.json +28 -21
- package/dist/format-quantity.legacy-esm.d.mts +0 -113
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import{numericQuantity as e}from"numeric-quantity";const t=.0075,n=Object.freeze({vulgarFractions:!1,tolerance:t,fractionSlash:!1,romanNumerals:!1,zeroFormat:``,allowTrailingInvalid:!0}),r=Object.freeze({"¼":`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=Object.freeze([[1/16,`1/16`],[1/10,`⅒`],[1/9,`⅑`],[1/8,`⅛`],[1/7,`⅐`],[1/6,`⅙`],[3/16,`3/16`],[1/5,`⅕`],[1/4,`¼`],[5/16,`5/16`],[1/3,`⅓`],[3/8,`⅜`],[2/5,`⅖`],[7/16,`7/16`],[1/2,`½`],[9/16,`9/16`],[3/5,`⅗`],[5/8,`⅝`],[2/3,`⅔`],[11/16,`11/16`],[3/4,`¾`],[4/5,`⅘`],[13/16,`13/16`],[5/6,`⅚`],[7/8,`⅞`],[15/16,`15/16`]].map(e=>Object.freeze(e))),a=e=>[...e].map(e=>`⁰¹²³⁴⁵⁶⁷⁸⁹`[+e]).join(``),o=e=>[...e].map(e=>`₀₁₂₃₄₅₆₇₈₉`[+e]).join(``),s=(e,{fractionSlash:t,vulgarFractions:n})=>{if(n)return e;let i=r[e]??e;if(t){let[e,t]=i.split(`/`);return`${a(e)}⁄${o(t)}`}return i},c=e=>e===!1||typeof e==`number`&&e>=0,l=e=>{let t={...n,...typeof e==`boolean`?{vulgarFractions:e}:typeof e==`object`&&e?e:{}};return c(t.tolerance)||(t.tolerance=n.tolerance),typeof t.zeroFormat!=`string`&&(t.zeroFormat=n.zeroFormat),t.allowTrailingInvalid!==!1&&(t.allowTrailingInvalid=!0),t},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`&&typeof e!=`bigint`||!(e>=1&&e<4e3))return null;let t=Number(e),n=`${Math.floor(t)}`.split(``),r=``,i=3;for(;i--;)r=`${u[+n.pop()+i*10]||``}${r}`;return`${Array(+n.join(``)+1).join(`M`)}${r}`},f=(t,n)=>{if(typeof t!=`number`&&typeof t!=`string`&&typeof t!=`bigint`)return null;let a=l(n),o=typeof t!=`number`&&typeof t!=`bigint`?e(t,{allowTrailingInvalid:a.allowTrailingInvalid,romanNumerals:a.romanNumerals,bigIntOnOverflow:!0,round:!1}):t;if(typeof o==`number`&&isNaN(o))return null;if(Number(o)===0)return a.zeroFormat;if(a.romanNumerals)return d(o);let c=typeof o==`bigint`?o<0n?-o:o:Math.abs(o),u=typeof c==`bigint`?c:Math.floor(c),f=`${u||``}`,p=typeof c==`bigint`?0:c-u;if(p===0)return`${o}`;let m=null,h=1/0;if(a.tolerance!==!1)for(let[e,t]of i){let n=Math.abs(p-e);if(n<a.tolerance||n===0){if(n===0){m=t;break}n<h&&(m=t,h=n)}}if(m){let e=s(m,a),t=e in r,n=f?a.separator??(t?``:` `):``;return`${o<0?`-`:``}${f}${n}${e}`}return`${o}`};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,EACV,CAAC,KAAO,QAAG,EACX,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,MAAM,EACf,CAAC,MAAQ,OAAO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A,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: Readonly<ResolvedFormatQuantityOptions> = Object.freeze({\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n zeroFormat: '',\n allowTrailingInvalid: true,\n});\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap: Readonly<Record<VulgarFraction, SimpleFraction>> = Object.freeze({\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});\n\n/**\n * Map of \"close enough\" values to the {@link VulgarFraction} or {@link Sixteenth} fraction\n * string matches. The value +/- the `tolerance` option (or {@link defaultTolerance} if not\n * specified) is considered close enough to match the fraction.\n */\nexport const fractionDecimalMatches: readonly (readonly [number, VulgarFraction | Sixteenth])[] =\n Object.freeze(\n (\n [\n [1 / 16, '1/16'],\n [1 / 10, '⅒'],\n [1 / 9, '⅑'],\n [1 / 8, '⅛'],\n [1 / 7, '⅐'],\n [1 / 6, '⅙'],\n [3 / 16, '3/16'],\n [1 / 5, '⅕'],\n [1 / 4, '¼'],\n [5 / 16, '5/16'],\n [1 / 3, '⅓'],\n [3 / 8, '⅜'],\n [2 / 5, '⅖'],\n [7 / 16, '7/16'],\n [1 / 2, '½'],\n [9 / 16, '9/16'],\n [3 / 5, '⅗'],\n [5 / 8, '⅝'],\n [2 / 3, '⅔'],\n [11 / 16, '11/16'],\n [3 / 4, '¾'],\n [4 / 5, '⅘'],\n [13 / 16, '13/16'],\n [5 / 6, '⅚'],\n [7 / 8, '⅞'],\n [15 / 16, '15/16'],\n ] satisfies [number, VulgarFraction | Sixteenth][]\n ).map(entry => Object.freeze(entry))\n );\n","import { numericQuantity } from 'numeric-quantity';\nimport { defaultOptions, fractionDecimalMatches, vulgarToAsciiMap } from './constants';\nimport type {\n FormatQuantity,\n ResolvedFormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\nconst superscriptDigits = '⁰¹²³⁴⁵⁶⁷⁸⁹';\nconst subscriptDigits = '₀₁₂₃₄₅₆₇₈₉';\n\n// oxlint-disable-next-line typescript/no-misused-spread\nconst toSuperscript = (s: string) => [...s].map(c => superscriptDigits[+c]).join('');\n// oxlint-disable-next-line typescript/no-misused-spread\nconst toSubscript = (s: string) => [...s].map(c => subscriptDigits[+c]).join('');\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: ResolvedFormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ?? 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 * Only `false` (matching disabled) or a non-negative number is a valid\n * `tolerance`. Everything else—negative numbers, `NaN`, non-numbers,\n * `null`, `undefined`—resolves to {@link defaultTolerance}.\n */\nconst isValidTolerance = (tolerance: unknown): tolerance is number | false =>\n tolerance === false || (typeof tolerance === 'number' && tolerance >= 0);\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 const opts: ResolvedFormatQuantityOptions = {\n ...defaultOptions,\n ...(typeof options === 'boolean'\n ? { vulgarFractions: options }\n : typeof options === 'object' && options !== null\n ? options\n : {}),\n };\n\n if (!isValidTolerance(opts.tolerance)) {\n opts.tolerance = defaultOptions.tolerance;\n }\n\n if (typeof opts.zeroFormat !== 'string') {\n opts.zeroFormat = defaultOptions.zeroFormat;\n }\n\n if (opts.allowTrailingInvalid !== false) {\n opts.allowTrailingInvalid = true;\n }\n\n return opts;\n};\n\n// oxfmt-ignore\nconst romanNumeralsByPlace = [\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 or bigint as Roman numerals. The number must be between\n * 1 and 3999, inclusive; any other value—including non-numbers,\n * `NaN`, and non-finite numbers—yields `null`.\n */\nexport const formatRomanNumerals = (quantity: number | bigint): string | null => {\n if (\n (typeof quantity !== 'number' && typeof quantity !== 'bigint') ||\n !(quantity >= 1 && quantity < 4000)\n ) {\n return null;\n }\n\n const qty = Number(quantity);\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 = `${romanNumeralsByPlace[+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 = (qty, options) => {\n // Only numbers, bigints, and strings are accepted. Anything else (objects, booleans,\n // nullish, arrays like `[1]` that would coerce to a numeric string) is\n // rejected up front so off-type inputs behave consistently.\n if (typeof qty !== 'number' && typeof qty !== 'string' && typeof qty !== 'bigint') {\n return null;\n }\n\n const opts = normalizeOptions(options);\n\n const qtyAsNumber =\n typeof qty !== 'number' && typeof qty !== 'bigint'\n ? numericQuantity(qty, {\n allowTrailingInvalid: opts.allowTrailingInvalid,\n romanNumerals: opts.romanNumerals,\n bigIntOnOverflow: true,\n round: false,\n })\n : qty;\n\n // Return `null` if input is not number-like.\n if (typeof qtyAsNumber === 'number' && isNaN(qtyAsNumber)) {\n return null;\n }\n\n // Return empty string (or configured `zeroFormat`) if the value is zero.\n if (Number(qtyAsNumber) === 0) {\n return opts.zeroFormat;\n }\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue =\n typeof qtyAsNumber === 'bigint'\n ? qtyAsNumber < 0n\n ? -qtyAsNumber\n : qtyAsNumber\n : Math.abs(qtyAsNumber);\n const flooredAbsVal =\n typeof absoluteValue === 'bigint' ? absoluteValue : Math.floor(absoluteValue);\n const wholeNumberStr = `${flooredAbsVal || ''}`;\n const decimalValue =\n typeof absoluteValue === 'bigint' ? 0 : absoluteValue - (flooredAbsVal as number);\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n let closestMatch: VulgarFraction | Sixteenth | null = null;\n let closestMatchDiff = Infinity;\n // `tolerance: false` disables fraction matching entirely.\n if (opts.tolerance !== false) {\n for (const [num, vf] of fractionDecimalMatches) {\n const diff = Math.abs(decimalValue - num);\n // `diff === 0` keeps exact quotients matching even at `tolerance: 0`.\n if (diff < opts.tolerance || diff === 0) {\n if (diff === 0) {\n closestMatch = vf;\n break;\n }\n if (diff < closestMatchDiff) {\n closestMatch = vf;\n closestMatchDiff = diff;\n }\n }\n }\n }\n\n if (closestMatch) {\n const fraction = getFraction(closestMatch, opts);\n const isVulgar = fraction in vulgarToAsciiMap;\n const sep = wholeNumberStr ? (opts.separator ?? (isVulgar ? '' : ' ')) : '';\n return `${qtyAsNumber < 0 ? '-' : ''}${wholeNumberStr}${sep}${fraction}`;\n }\n\n return `${qtyAsNumber}`;\n};\n"],"mappings":"mDAWA,MAAa,EAAmB,MAKnB,EAA0D,OAAO,OAAO,CACnF,gBAAiB,GACjB,UAAW,EACX,cAAe,GACf,cAAe,GACf,WAAY,GACZ,qBAAsB,EACxB,CAAC,EAKY,EAAqE,OAAO,O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gB,EAAK,CACnB,qBAAsB,EAAK,qBAC3B,cAAe,EAAK,cACpB,iBAAkB,GAClB,MAAO,EACT,CAAC,EACD,EAGN,GAAI,OAAO,GAAgB,UAAY,MAAM,CAAW,EACtD,OAAO,KAIT,GAAI,OAAO,CAAW,IAAM,EAC1B,OAAO,EAAK,WAGd,GAAI,EAAK,cACP,OAAO,EAAoB,CAAW,EAGxC,IAAM,EACJ,OAAO,GAAgB,SACnB,EAAc,GACZ,CAAC,EACD,EACF,KAAK,IAAI,CAAW,EACpB,EACJ,OAAO,GAAkB,SAAW,EAAgB,KAAK,MAAM,CAAa,EACxE,EAAiB,GAAG,GAAiB,KACrC,EACJ,OAAO,GAAkB,SAAW,EAAI,EAAiB,EAG3D,GAAI,IAAiB,EACnB,MAAO,GAAG,IAGZ,IAAI,EAAkD,KAClD,EAAmB,IAEvB,GAAI,EAAK,YAAc,GACrB,IAAK,GAAM,CAAC,EAAK,KAAO,EAAwB,CAC9C,IAAM,EAAO,KAAK,IAAI,EAAe,CAAG,EAExC,GAAI,EAAO,EAAK,WAAa,IAAS,EAAG,CACvC,GAAI,IAAS,EAAG,CACd,EAAe,EACf,KACF,CACI,EAAO,IACT,EAAe,EACf,EAAmB,EAEvB,CACF,CAGF,GAAI,EAAc,CAChB,IAAM,EAAW,EAAY,EAAc,CAAI,EACzC,EAAW,KAAY,EACvB,EAAM,EAAkB,EAAK,YAAc,EAAW,GAAK,KAAQ,GACzE,MAAO,GAAG,EAAc,EAAI,IAAM,KAAK,IAAiB,IAAM,GAChE,CAEA,MAAO,GAAG,GACZ"}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.FormatQuantity={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let t=.0075,n=Object.freeze({vulgarFractions:!1,tolerance:t,fractionSlash:!1,romanNumerals:!1,zeroFormat:``,allowTrailingInvalid:!0}),r=Object.freeze({"¼":`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=Object.freeze([[1/16,`1/16`],[1/10,`⅒`],[1/9,`⅑`],[1/8,`⅛`],[1/7,`⅐`],[1/6,`⅙`],[3/16,`3/16`],[1/5,`⅕`],[1/4,`¼`],[5/16,`5/16`],[1/3,`⅓`],[3/8,`⅜`],[2/5,`⅖`],[7/16,`7/16`],[1/2,`½`],[9/16,`9/16`],[3/5,`⅗`],[5/8,`⅝`],[2/3,`⅔`],[11/16,`11/16`],[3/4,`¾`],[4/5,`⅘`],[13/16,`13/16`],[5/6,`⅚`],[7/8,`⅞`],[15/16,`15/16`]].map(e=>Object.freeze(e))),a=[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],o=e=>e.replace(/\p{Nd}/gu,e=>{let t=e.codePointAt(0);if(t<=57)return e;let n=0,r=a.length-1;for(;n<r;){let e=n+r+1>>>1;a[e]<=t?n=e:r=e-1}return String(t-a[n])}),s={"⁰":`0`,"¹":`1`,"²":`2`,"³":`3`,"⁴":`4`,"⁵":`5`,"⁶":`6`,"⁷":`7`,"⁸":`8`,"⁹":`9`,"₀":`0`,"₁":`1`,"₂":`2`,"₃":`3`,"₄":`4`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ⅺ:`XI`,ⅻ:`XII`,ⅼ:`L`,ⅽ:`C`,ⅾ:`D`,ⅿ:`M`},m=/([ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿ])/gi,h=/^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i,g={round:3,allowTrailingInvalid:!1,romanNumerals:!1,bigIntOnOverflow:!1,decimalSeparator:`.`,allowCurrency:!1,percentage:!1,verbose:!1},_=e=>{let t=e.replace(m,(e,t)=>p[t]).toUpperCase(),n=h.exec(t);if(!n)return NaN;let[,r,i,a,o]=n;return(f[r]??0)+(f[i]??0)+(f[a]??0)+(f[o]??0)},v=/^\s*\//,y=/^([-+]?)\s*(\p{Sc}+)\s*/u,b=/\s*(\p{Sc}+)\s*$/u,x=/\s*%$/,S=BigInt(2**53-1),C=e=>10n**BigInt(e),w=(e,t,n)=>{let r,i=1n;if(!t)r=BigInt(e);else if(t.startsWith(`.`)||t.startsWith(`e`)||t.startsWith(`E`)){let n=t.search(/[eE]/),a=n===-1?0:parseInt(t.slice(n+1));if(!Number.isFinite(a)||Math.abs(a)>1e4)return;let o=(n===-1?t:t.slice(0,n)).slice(1);r=BigInt(`${e}${o}`),o&&(i=C(o.length)),a>0?r*=C(a):a<0&&(i*=C(-a))}else if(v.test(t))r=BigInt(e),i=BigInt(t.replace(`/`,``).trim());else{let[n,a]=t.split(`/`);i=BigInt(a.trim()),r=BigInt(e)*i+BigInt(n.trim())}if(n&&(i*=100n),i===0n)return;let a=(2n*r+i)/(2n*i);return a>S?a:void 0};function T(e,t=g){let n={...g,...t},r;if(typeof e==`string`)r=e;else try{r=String(e)}catch{r=``}let i,a,f,p,m,h,S,C,T=e=>{let t={value:e,input:r};return i&&(t.currencyPrefix=i),a&&(t.currencySuffix=a),f&&(t.percentageSuffix=f),p&&(t.trailingInvalid=p),m&&(t.sign=m),h!==void 0&&(t.whole=h),S!==void 0&&(t.numerator=S),C!==void 0&&(t.denominator=C),t},E=e=>n.verbose?T(e):e,D=e=>f&&n.percentage!==`number`?e/100:e;if(typeof e==`symbol`)return E(NaN);if(typeof e==`number`||typeof e==`bigint`)return E(e);let O=NaN,k=r,A=!0;for(;A;){if(A=!1,n.allowCurrency){let e=y.exec(k);e?.[2]&&(i=(i??``)+e[2],k=(e[1]||``)+k.slice(e[0].length),A=!0);let t=b.exec(k);t&&(a=t[1]+(a??``),k=k.slice(0,-t[0].length),A=!0)}if(!f&&n.percentage){let e=x.exec(k);e&&(f=!0,k=k.slice(0,-e[0].length),A=!0)}}let j=o(k.replace(d,(e,t)=>` ${l[t]}`).replace(c,e=>s[e]).replace(`⁄`,`/`).trim());if(j.length===0)return E(NaN);let M=j;if(n.decimalSeparator===`,`){let e=(j.match(/,/g)||[]).length;if(e===1)M=j.replaceAll(`.`,`_`).replace(`,`,`.`);else if(e>1){let e=j.indexOf(`,`),t=j.indexOf(`,`,e+1);M=j.substring(0,t).replaceAll(`.`,`_`).replace(`,`,`.`)}else M=j.replaceAll(`.`,`_`)}let N=u.exec(M);if(!N)return n.romanNumerals?E(D(_(j))):E(NaN);let P=N[0].length-(N[7]?.length??0),F=j.slice(P).trim();if(F&&(p=F,!n.allowTrailingInvalid))return E(NaN);let[,I,L,R]=N;(I===`-`||I===`+`)&&(m=I);let z=L.replaceAll(`,`,``).replaceAll(`_`,``),B=R?.replaceAll(`,`,``).replaceAll(`_`,``);if(n.bigIntOnOverflow){let e=w(z,B,!!f&&n.percentage!==`number`);if(e!==void 0)return E(I===`-`?-e:e)}if(O=!z&&B&&B.startsWith(`.`)?0:parseInt(z),!B)return O=I===`-`?O*-1:O,E(D(O));let V=typeof n.round==`number`&&Number.isFinite(n.round)?Math.floor(Math.max(0,n.round)):!1,H=V===!1?NaN:10**V,U=Number.isFinite(H)?H:NaN,W=(e,t)=>{let n=Math.round(t)/U;return Number.isFinite(n)||!Number.isFinite(e)?n:e};if(B.startsWith(`.`)||B.startsWith(`e`)||B.startsWith(`E`)){let e=parseFloat(`${O}${B}`);O=isNaN(U)?e:W(e,e*U)}else if(v.test(B)){let e=parseInt(z),t=parseInt(B.replace(`/`,``));S=e,C=t,O=isNaN(U)?e/t:W(e/t,e*U/t)}else{let[e,t]=B.split(`/`).map(e=>parseInt(e));h=O,S=e,C=t,O+=isNaN(U)?e/t:W(e/t,e*U/t)}return O=I===`-`?O*-1:O,E(D(O))}let E=e=>[...e].map(e=>`⁰¹²³⁴⁵⁶⁷⁸⁹`[+e]).join(``),D=e=>[...e].map(e=>`₀₁₂₃₄₅₆₇₈₉`[+e]).join(``),O=(e,{fractionSlash:t,vulgarFractions:n})=>{if(n)return e;let i=r[e]??e;if(t){let[e,t]=i.split(`/`);return`${E(e)}⁄${D(t)}`}return i},k=e=>e===!1||typeof e==`number`&&e>=0,A=e=>{let t={...n,...typeof e==`boolean`?{vulgarFractions:e}:typeof e==`object`&&e?e:{}};return k(t.tolerance)||(t.tolerance=n.tolerance),typeof t.zeroFormat!=`string`&&(t.zeroFormat=n.zeroFormat),t.allowTrailingInvalid!==!1&&(t.allowTrailingInvalid=!0),t},j=`.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(`.`),M=e=>{if(typeof e!=`number`&&typeof e!=`bigint`||!(e>=1&&e<4e3))return null;let t=Number(e),n=`${Math.floor(t)}`.split(``),r=``,i=3;for(;i--;)r=`${j[+n.pop()+i*10]||``}${r}`;return`${Array(+n.join(``)+1).join(`M`)}${r}`};e.defaultOptions=n,e.defaultTolerance=t,e.formatQuantity=(e,t)=>{if(typeof e!=`number`&&typeof e!=`string`&&typeof e!=`bigint`)return null;let n=A(t),a=typeof e!=`number`&&typeof e!=`bigint`?T(e,{allowTrailingInvalid:n.allowTrailingInvalid,romanNumerals:n.romanNumerals,bigIntOnOverflow:!0,round:!1}):e;if(typeof a==`number`&&isNaN(a))return null;if(Number(a)===0)return n.zeroFormat;if(n.romanNumerals)return M(a);let o=typeof a==`bigint`?a<0n?-a:a:Math.abs(a),s=typeof o==`bigint`?o:Math.floor(o),c=`${s||``}`,l=typeof o==`bigint`?0:o-s;if(l===0)return`${a}`;let u=null,d=1/0;if(n.tolerance!==!1)for(let[e,t]of i){let r=Math.abs(l-e);if(r<n.tolerance||r===0){if(r===0){u=t;break}r<d&&(u=t,d=r)}}if(u){let e=O(u,n),t=e in r,i=c?n.separator??(t?``:` `):``;return`${a<0?`-`:``}${c}${i}${e}`}return`${a}`},e.formatRomanNumerals=M,e.fractionDecimalMatches=i,e.vulgarToAsciiMap=r});
|
|
2
|
+
typeof window<"u"&&typeof exports=="object"&&(window.FormatQuantity=exports);
|
|
2
3
|
//# sourceMappingURL=format-quantity.umd.min.js.map
|
|
@@ -1 +1 @@
|
|
|
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,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,KAAO,QAAG,EACX,CAAC,GAAK,QAAG,EACT,CAAC,KAAO,QAAG,EACX,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AK,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"]}
|
|
1
|
+
{"version":3,"file":"format-quantity.umd.min.js","names":["defaultOptions"],"sources":["../src/constants.ts","../node_modules/numeric-quantity/dist/numeric-quantity.mjs","../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: Readonly<ResolvedFormatQuantityOptions> = Object.freeze({\n vulgarFractions: false,\n tolerance: defaultTolerance,\n fractionSlash: false,\n romanNumerals: false,\n zeroFormat: '',\n allowTrailingInvalid: true,\n});\n\n/**\n * Map of vulgar fractions to their traditional ASCII equivalents.\n */\nexport const vulgarToAsciiMap: Readonly<Record<VulgarFraction, SimpleFraction>> = Object.freeze({\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});\n\n/**\n * Map of \"close enough\" values to the {@link VulgarFraction} or {@link Sixteenth} fraction\n * string matches. The value +/- the `tolerance` option (or {@link defaultTolerance} if not\n * specified) is considered close enough to match the fraction.\n */\nexport const fractionDecimalMatches: readonly (readonly [number, VulgarFraction | Sixteenth])[] =\n Object.freeze(\n (\n [\n [1 / 16, '1/16'],\n [1 / 10, '⅒'],\n [1 / 9, '⅑'],\n [1 / 8, '⅛'],\n [1 / 7, '⅐'],\n [1 / 6, '⅙'],\n [3 / 16, '3/16'],\n [1 / 5, '⅕'],\n [1 / 4, '¼'],\n [5 / 16, '5/16'],\n [1 / 3, '⅓'],\n [3 / 8, '⅜'],\n [2 / 5, '⅖'],\n [7 / 16, '7/16'],\n [1 / 2, '½'],\n [9 / 16, '9/16'],\n [3 / 5, '⅗'],\n [5 / 8, '⅝'],\n [2 / 3, '⅔'],\n [11 / 16, '11/16'],\n [3 / 4, '¾'],\n [4 / 5, '⅘'],\n [13 / 16, '13/16'],\n [5 / 6, '⅚'],\n [7 / 8, '⅞'],\n [15 / 16, '15/16'],\n ] satisfies [number, VulgarFraction | Sixteenth][]\n ).map(entry => Object.freeze(entry))\n );\n","//#region src/constants.ts\n/**\n* Unicode decimal digit block start code points.\n* Each block contains 10 contiguous digits (0-9).\n* This list covers all \\p{Nd} (Decimal_Number) blocks through Unicode 17.0.\n* The drift test in index.test.ts validates completeness against the JS engine.\n*/\nconst decimalDigitBlockStarts = [\n\t48,\n\t1632,\n\t1776,\n\t1984,\n\t2406,\n\t2534,\n\t2662,\n\t2790,\n\t2918,\n\t3046,\n\t3174,\n\t3302,\n\t3430,\n\t3558,\n\t3664,\n\t3792,\n\t3872,\n\t4160,\n\t4240,\n\t6112,\n\t6160,\n\t6470,\n\t6608,\n\t6784,\n\t6800,\n\t6992,\n\t7088,\n\t7232,\n\t7248,\n\t42528,\n\t43216,\n\t43264,\n\t43472,\n\t43504,\n\t43600,\n\t44016,\n\t65296,\n\t66720,\n\t68912,\n\t68928,\n\t69734,\n\t69872,\n\t69942,\n\t70096,\n\t70384,\n\t70736,\n\t70864,\n\t71248,\n\t71360,\n\t71376,\n\t71386,\n\t71472,\n\t71904,\n\t72016,\n\t72688,\n\t72784,\n\t73040,\n\t73120,\n\t73184,\n\t73552,\n\t90416,\n\t92768,\n\t92864,\n\t93008,\n\t93552,\n\t118e3,\n\t120782,\n\t120792,\n\t120802,\n\t120812,\n\t120822,\n\t123200,\n\t123632,\n\t124144,\n\t124401,\n\t125264,\n\t130032\n];\n/**\n* Normalizes non-ASCII decimal digits to ASCII digits.\n* Converts characters from Unicode decimal digit blocks (e.g., Arabic-Indic,\n* Devanagari, Bengali) to their ASCII equivalents (0-9).\n*\n* All current Unicode \\p{Nd} blocks are included in decimalDigitBlockStarts.\n*/\nconst normalizeDigits = (str) => str.replace(/\\p{Nd}/gu, (ch) => {\n\tconst cp = ch.codePointAt(0);\n\tif (cp <= 57) return ch;\n\tlet lo = 0;\n\tlet hi = decimalDigitBlockStarts.length - 1;\n\twhile (lo < hi) {\n\t\tconst mid = lo + hi + 1 >>> 1;\n\t\tif (decimalDigitBlockStarts[mid] <= cp) lo = mid;\n\t\telse hi = mid - 1;\n\t}\n\treturn String(cp - decimalDigitBlockStarts[lo]);\n});\n/**\n* Map of Unicode superscript and subscript digit code points to ASCII digits.\n*/\nconst superSubDigitToAsciiMap = {\n\t\"⁰\": \"0\",\n\t\"¹\": \"1\",\n\t\"²\": \"2\",\n\t\"³\": \"3\",\n\t\"⁴\": \"4\",\n\t\"⁵\": \"5\",\n\t\"⁶\": \"6\",\n\t\"⁷\": \"7\",\n\t\"⁸\": \"8\",\n\t\"⁹\": \"9\",\n\t\"₀\": \"0\",\n\t\"₁\": \"1\",\n\t\"₂\": \"2\",\n\t\"₃\": \"3\",\n\t\"₄\": \"4\",\n\t\"₅\": \"5\",\n\t\"₆\": \"6\",\n\t\"₇\": \"7\",\n\t\"₈\": \"8\",\n\t\"₉\": \"9\"\n};\n/**\n* Captures Unicode superscript and subscript digits.\n*/\nconst superSubDigitsRegex = /[⁰¹²³⁴⁵⁶⁷⁸⁹₀₁₂₃₄₅₆₇₈₉]/g;\n/**\n* Map of Unicode fraction code points to their ASCII equivalents.\n*/\nconst vulgarFractionToAsciiMap = {\n\t\"¼\": \"1/4\",\n\t\"½\": \"1/2\",\n\t\"¾\": \"3/4\",\n\t\"⅐\": \"1/7\",\n\t\"⅑\": \"1/9\",\n\t\"⅒\": \"1/10\",\n\t\"⅓\": \"1/3\",\n\t\"⅔\": \"2/3\",\n\t\"⅕\": \"1/5\",\n\t\"⅖\": \"2/5\",\n\t\"⅗\": \"3/5\",\n\t\"⅘\": \"4/5\",\n\t\"⅙\": \"1/6\",\n\t\"⅚\": \"5/6\",\n\t\"⅛\": \"1/8\",\n\t\"⅜\": \"3/8\",\n\t\"⅝\": \"5/8\",\n\t\"⅞\": \"7/8\",\n\t\"⅟\": \"1/\"\n};\n/**\n* Captures the individual elements of a numeric string. Commas and underscores are allowed\n* as separators, as long as they appear between digits and are not consecutive.\n*\n* Capture groups:\n*\n* | # | Description | Example(s) |\n* | --- | -------------------------------------------------- | ----------------------------------------------------------------------- |\n* | `0` | entire match | `\"2 1/3\"` from `\"2 1/3\"` |\n* | `1` | sign (`-` or `+`) | `\"-\"` from `\"-2 1/3\"` |\n* | `2` | whole number or numerator | `\"2\"` from `\"2 1/3\"`; `\"1\"` from `\"1/3\"` |\n* | `3` | entire tail: fraction, decimal, and/or exponent | `\" 1/3\"` from `\"2 1/3\"`; `\".23\"` from `\"1.23\"`; `\".5e3\"` from `\"1.5e3\"` |\n* | `4` | exponent, when there is no decimal portion | `\"e3\"` from `\"1e3\"` |\n* | `5` | exponent, when there is a decimal portion | `\"e-3\"` from `\"1.5e-3\"` |\n* | `6` | numerator of a mixed number (incl. leading space) | `\" 2\"` from `\"1 2/3\"` |\n*\n* Groups 4–6 are subgroups of group 3 and are not used directly by\n* {@link numericQuantity}; only groups 1–3 are consumed.\n*\n* _Capture groups 2 and 3 may include comma/underscore separators._\n*\n* @example\n*\n* ```ts\n* numericRegex.exec(\"1\") // [ \"1\", null, \"1\", null, null, null, null ]\n* numericRegex.exec(\"1.23\") // [ \"1.23\", null, \"1\", \".23\", null, null, null ]\n* numericRegex.exec(\"1 2/3\") // [ \"1 2/3\", null, \"1\", \" 2/3\", null, null, \" 2\" ]\n* numericRegex.exec(\"2/3\") // [ \"2/3\", null, \"2\", \"/3\", null, null, null ]\n* numericRegex.exec(\"2 / 3\") // [ \"2 / 3\", null, \"2\", \" / 3\", null, null, null ]\n* numericRegex.exec(\"-2 1/3\") // [ \"-2 1/3\", \"-\", \"2\", \" 1/3\", null, null, \" 1\" ]\n* numericRegex.exec(\"1e3\") // [ \"1e3\", null, \"1\", \"e3\", \"e3\", null, null ]\n* numericRegex.exec(\"1.5e-3\") // [ \"1.5e-3\", null, \"1\", \".5e-3\", null, \"e-3\", null ]\n* numericRegex.exec(\"1,000\") // [ \"1,000\", null, \"1,000\", null, null, null, null ]\n* ```\n*\n* @remarks\n*\n* This pattern and {@link numericRegexWithTrailingInvalid} are maintained as two separate\n* literals and must be kept in sync. They differ _only_ in the tail: `$` here versus\n* `(\\s*[^.\\d/].*)?` there. `src/index.test.ts` asserts that equality.\n*/\nconst numericRegex = /^(?=[-+]?\\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)*)?$/;\n/**\n* Same as {@link numericRegex}, but allows (and ignores) trailing invalid characters.\n* Capture groups 1–6 are identical to {@link numericRegex}'s; capture group 7 contains the\n* trailing invalid portion.\n*\n* @example\n*\n* ```ts\n* numericRegexWithTrailingInvalid.exec(\"1abc\")\n* // [ \"1abc\", null, \"1\", null, null, null, null, \"abc\" ]\n* numericRegexWithTrailingInvalid.exec(\"1 2/3 xyz\")\n* // [ \"1 2/3 xyz\", null, \"1\", \" 2/3\", null, null, \" 2\", \" xyz\" ]\n* ```\n*\n* @remarks\n*\n* Must be kept in sync with {@link numericRegex} — see that pattern's remarks.\n*/\nconst numericRegexWithTrailingInvalid = /^(?=[-+]?\\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/].*)?/;\n/**\n* Captures any Unicode vulgar fractions.\n*/\nconst vulgarFractionsRegex = /([¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟])/g;\n/**\n* Map of Roman numeral sequences to their decimal equivalents.\n*/\nconst romanNumeralValues = {\n\tMMM: 3e3,\n\tMM: 2e3,\n\tM: 1e3,\n\tCM: 900,\n\tDCCC: 800,\n\tDCC: 700,\n\tDC: 600,\n\tD: 500,\n\tCD: 400,\n\tCCC: 300,\n\tCC: 200,\n\tC: 100,\n\tXC: 90,\n\tLXXX: 80,\n\tLXX: 70,\n\tLX: 60,\n\tL: 50,\n\tXL: 40,\n\tXXX: 30,\n\tXX: 20,\n\tXII: 12,\n\tXI: 11,\n\tX: 10,\n\tIX: 9,\n\tVIII: 8,\n\tVII: 7,\n\tVI: 6,\n\tV: 5,\n\tIV: 4,\n\tIII: 3,\n\tII: 2,\n\tI: 1\n};\n/**\n* Map of Unicode Roman numeral code points to their ASCII equivalents.\n*/\nconst romanNumeralUnicodeToAsciiMap = {\n\tⅠ: \"I\",\n\tⅡ: \"II\",\n\tⅢ: \"III\",\n\tⅣ: \"IV\",\n\tⅤ: \"V\",\n\tⅥ: \"VI\",\n\tⅦ: \"VII\",\n\tⅧ: \"VIII\",\n\tⅨ: \"IX\",\n\tⅩ: \"X\",\n\tⅪ: \"XI\",\n\tⅫ: \"XII\",\n\tⅬ: \"L\",\n\tⅭ: \"C\",\n\tⅮ: \"D\",\n\tⅯ: \"M\",\n\tⅰ: \"I\",\n\tⅱ: \"II\",\n\tⅲ: \"III\",\n\tⅳ: \"IV\",\n\tⅴ: \"V\",\n\tⅵ: \"VI\",\n\tⅶ: \"VII\",\n\tⅷ: \"VIII\",\n\tⅸ: \"IX\",\n\tⅹ: \"X\",\n\tⅺ: \"XI\",\n\tⅻ: \"XII\",\n\tⅼ: \"L\",\n\tⅽ: \"C\",\n\tⅾ: \"D\",\n\tⅿ: \"M\"\n};\n/**\n* Captures all Unicode Roman numeral code points.\n*/\nconst romanNumeralUnicodeRegex = /([ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿ])/gi;\n/**\n* Captures a valid Roman numeral sequence.\n*\n* Capture groups:\n*\n* | # | Description | Example |\n* | --- | --------------- | ------------------------ |\n* | `0` | Entire string | \"MCCXIV\" from \"MCCXIV\" |\n* | `1` | Thousands | \"M\" from \"MCCXIV\" |\n* | `2` | Hundreds | \"CC\" from \"MCCXIV\" |\n* | `3` | Tens | \"X\" from \"MCCXIV\" |\n* | `4` | Ones | \"IV\" from \"MCCXIV\" |\n*\n* @example\n*\n* ```ts\n* romanNumeralRegex.exec(\"M\") // [ \"M\", \"M\", \"\", \"\", \"\" ]\n* romanNumeralRegex.exec(\"XII\") // [ \"XII\", \"\", \"\", \"X\", \"II\" ]\n* romanNumeralRegex.exec(\"MCCXIV\") // [ \"MCCXIV\", \"M\", \"CC\", \"X\", \"IV\" ]\n* ```\n*/\nconst romanNumeralRegex = /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;\n/**\n* Default options for {@link numericQuantity}.\n*/\nconst defaultOptions = {\n\tround: 3,\n\tallowTrailingInvalid: false,\n\tromanNumerals: false,\n\tbigIntOnOverflow: false,\n\tdecimalSeparator: \".\",\n\tallowCurrency: false,\n\tpercentage: false,\n\tverbose: false\n};\n//#endregion\n//#region src/parseRomanNumerals.ts\n/**\n* Converts a string of Roman numerals to a number, like `parseInt`\n* for Roman numerals. Uses modern, strict rules (only 1 to 3999).\n*\n* The string can include ASCII representations of Roman numerals\n* or Unicode Roman numeral code points (`U+2160` through `U+217F`).\n*/\nconst parseRomanNumerals = (romanNumerals) => {\n\tconst normalized = romanNumerals.replace(romanNumeralUnicodeRegex, (_m, rn) => romanNumeralUnicodeToAsciiMap[rn]).toUpperCase();\n\tconst regexResult = romanNumeralRegex.exec(normalized);\n\tif (!regexResult) return NaN;\n\tconst [, thousands, hundreds, tens, ones] = regexResult;\n\treturn (romanNumeralValues[thousands] ?? 0) + (romanNumeralValues[hundreds] ?? 0) + (romanNumeralValues[tens] ?? 0) + (romanNumeralValues[ones] ?? 0);\n};\n//#endregion\n//#region src/numericQuantity.ts\nconst leadingSlashRegex = /^\\s*\\//;\nconst currencyPrefixRegex = /^([-+]?)\\s*(\\p{Sc}+)\\s*/u;\nconst currencySuffixRegex = /\\s*(\\p{Sc}+)\\s*$/u;\nconst percentageSuffixRegex = /\\s*%$/;\nconst maxSafeBigInt = BigInt(Number.MAX_SAFE_INTEGER);\n/**\n* Upper bound on `10 ** exp` magnitudes evaluated exactly. Anything beyond this\n* overflows/underflows the `number` path to `Infinity`/`0` anyway, so bounding it\n* avoids allocating absurdly large `bigint`s (and `BigInt(Infinity)` throwing).\n*/\nconst maxExactExponent = 1e4;\nconst pow10 = (exp) => 10n ** BigInt(exp);\n/**\n* Evaluates the parsed numeric components as an exact `bigint` rational (magnitude only,\n* sign is applied by the caller) and rounds the result half-up.\n*\n* Returns the rounded `bigint` only if its magnitude exceeds `Number.MAX_SAFE_INTEGER`;\n* otherwise returns `undefined`, meaning \"no overflow, use the `number` path\".\n*\n* @param group1 - Whole number or numerator (separators already stripped).\n* @param group2 - Decimal/exponent/fraction tail, if any.\n* @param divideBy100 - Whether a percentage suffix was stripped.\n*/\nconst toRoundedBigInt = (group1, group2, divideBy100) => {\n\tlet numerator;\n\tlet denominator = 1n;\n\tif (!group2) numerator = BigInt(group1);\n\telse if (group2.startsWith(\".\") || group2.startsWith(\"e\") || group2.startsWith(\"E\")) {\n\t\tconst expIndex = group2.search(/[eE]/);\n\t\tconst exponent = expIndex === -1 ? 0 : parseInt(group2.slice(expIndex + 1));\n\t\tif (!Number.isFinite(exponent) || Math.abs(exponent) > maxExactExponent) return void 0;\n\t\tconst fractionDigits = (expIndex === -1 ? group2 : group2.slice(0, expIndex)).slice(1);\n\t\tnumerator = BigInt(`${group1}${fractionDigits}`);\n\t\tif (fractionDigits) denominator = pow10(fractionDigits.length);\n\t\tif (exponent > 0) numerator *= pow10(exponent);\n\t\telse if (exponent < 0) denominator *= pow10(-exponent);\n\t} else if (leadingSlashRegex.test(group2)) {\n\t\tnumerator = BigInt(group1);\n\t\tdenominator = BigInt(group2.replace(\"/\", \"\").trim());\n\t} else {\n\t\tconst [n, d] = group2.split(\"/\");\n\t\tdenominator = BigInt(d.trim());\n\t\tnumerator = BigInt(group1) * denominator + BigInt(n.trim());\n\t}\n\tif (divideBy100) denominator *= 100n;\n\tif (denominator === 0n) return void 0;\n\tconst rounded = (2n * numerator + denominator) / (2n * denominator);\n\treturn rounded > maxSafeBigInt ? rounded : void 0;\n};\nfunction numericQuantity(quantity, options = defaultOptions) {\n\tvar _regexResult$;\n\tconst opts = {\n\t\t...defaultOptions,\n\t\t...options\n\t};\n\tlet originalInput;\n\tif (typeof quantity === \"string\") originalInput = quantity;\n\telse try {\n\t\toriginalInput = String(quantity);\n\t} catch {\n\t\toriginalInput = \"\";\n\t}\n\tlet currencyPrefix;\n\tlet currencySuffix;\n\tlet percentageSuffix;\n\tlet trailingInvalid;\n\tlet parsedSign;\n\tlet parsedWhole;\n\tlet parsedNumerator;\n\tlet parsedDenominator;\n\tconst buildVerboseResult = (value) => {\n\t\tconst result = {\n\t\t\tvalue,\n\t\t\tinput: originalInput\n\t\t};\n\t\tif (currencyPrefix) result.currencyPrefix = currencyPrefix;\n\t\tif (currencySuffix) result.currencySuffix = currencySuffix;\n\t\tif (percentageSuffix) result.percentageSuffix = percentageSuffix;\n\t\tif (trailingInvalid) result.trailingInvalid = trailingInvalid;\n\t\tif (parsedSign) result.sign = parsedSign;\n\t\tif (parsedWhole !== void 0) result.whole = parsedWhole;\n\t\tif (parsedNumerator !== void 0) result.numerator = parsedNumerator;\n\t\tif (parsedDenominator !== void 0) result.denominator = parsedDenominator;\n\t\treturn result;\n\t};\n\tconst returnValue = (value) => opts.verbose ? buildVerboseResult(value) : value;\n\t/**\n\t* Divides by 100 if a percentage suffix was stripped. Exact scaling, no rounding:\n\t* `round` applies to the quantity as written, before this division.\n\t*/\n\tconst applyPercentage = (value) => percentageSuffix && opts.percentage !== \"number\" ? value / 100 : value;\n\tif (typeof quantity === \"symbol\") return returnValue(NaN);\n\tif (typeof quantity === \"number\" || typeof quantity === \"bigint\") return returnValue(quantity);\n\tlet finalResult = NaN;\n\tlet workingString = originalInput;\n\tlet affixStripped = true;\n\twhile (affixStripped) {\n\t\taffixStripped = false;\n\t\tif (opts.allowCurrency) {\n\t\t\tconst prefixMatch = currencyPrefixRegex.exec(workingString);\n\t\t\tif (prefixMatch === null || prefixMatch === void 0 ? void 0 : prefixMatch[2]) {\n\t\t\t\tcurrencyPrefix = (currencyPrefix ?? \"\") + prefixMatch[2];\n\t\t\t\tworkingString = (prefixMatch[1] || \"\") + workingString.slice(prefixMatch[0].length);\n\t\t\t\taffixStripped = true;\n\t\t\t}\n\t\t\tconst suffixMatch = currencySuffixRegex.exec(workingString);\n\t\t\tif (suffixMatch) {\n\t\t\t\tcurrencySuffix = suffixMatch[1] + (currencySuffix ?? \"\");\n\t\t\t\tworkingString = workingString.slice(0, -suffixMatch[0].length);\n\t\t\t\taffixStripped = true;\n\t\t\t}\n\t\t}\n\t\tif (!percentageSuffix && opts.percentage) {\n\t\t\tconst pctMatch = percentageSuffixRegex.exec(workingString);\n\t\t\tif (pctMatch) {\n\t\t\t\tpercentageSuffix = true;\n\t\t\t\tworkingString = workingString.slice(0, -pctMatch[0].length);\n\t\t\t\taffixStripped = true;\n\t\t\t}\n\t\t}\n\t}\n\tconst quantityAsString = normalizeDigits(workingString.replace(vulgarFractionsRegex, (_m, vf) => ` ${vulgarFractionToAsciiMap[vf]}`).replace(superSubDigitsRegex, (ch) => superSubDigitToAsciiMap[ch]).replace(\"⁄\", \"/\").trim());\n\tif (quantityAsString.length === 0) return returnValue(NaN);\n\tlet normalizedString = quantityAsString;\n\tif (opts.decimalSeparator === \",\") {\n\t\tconst commaCount = (quantityAsString.match(/,/g) || []).length;\n\t\tif (commaCount === 1) normalizedString = quantityAsString.replaceAll(\".\", \"_\").replace(\",\", \".\");\n\t\telse if (commaCount > 1) {\n\t\t\tconst firstCommaIndex = quantityAsString.indexOf(\",\");\n\t\t\tconst secondCommaIndex = quantityAsString.indexOf(\",\", firstCommaIndex + 1);\n\t\t\tnormalizedString = quantityAsString.substring(0, secondCommaIndex).replaceAll(\".\", \"_\").replace(\",\", \".\");\n\t\t} else normalizedString = quantityAsString.replaceAll(\".\", \"_\");\n\t}\n\tconst regexResult = numericRegexWithTrailingInvalid.exec(normalizedString);\n\tif (!regexResult) {\n\t\tif (!opts.romanNumerals) return returnValue(NaN);\n\t\treturn returnValue(applyPercentage(parseRomanNumerals(quantityAsString)));\n\t}\n\tconst consumedLength = regexResult[0].length - (((_regexResult$ = regexResult[7]) === null || _regexResult$ === void 0 ? void 0 : _regexResult$.length) ?? 0);\n\tconst rawTrailing = quantityAsString.slice(consumedLength).trim();\n\tif (rawTrailing) {\n\t\ttrailingInvalid = rawTrailing;\n\t\tif (!opts.allowTrailingInvalid) return returnValue(NaN);\n\t}\n\tconst [, sign, rawGroup1, rawGroup2] = regexResult;\n\tif (sign === \"-\" || sign === \"+\") parsedSign = sign;\n\tconst numberGroup1 = rawGroup1.replaceAll(\",\", \"\").replaceAll(\"_\", \"\");\n\tconst numberGroup2 = rawGroup2 === null || rawGroup2 === void 0 ? void 0 : rawGroup2.replaceAll(\",\", \"\").replaceAll(\"_\", \"\");\n\tif (opts.bigIntOnOverflow) {\n\t\tconst asBigInt = toRoundedBigInt(numberGroup1, numberGroup2, !!percentageSuffix && opts.percentage !== \"number\");\n\t\tif (asBigInt !== void 0) return returnValue(sign === \"-\" ? -asBigInt : asBigInt);\n\t}\n\tfinalResult = !numberGroup1 && numberGroup2 && numberGroup2.startsWith(\".\") ? 0 : parseInt(numberGroup1);\n\tif (!numberGroup2) {\n\t\tfinalResult = sign === \"-\" ? finalResult * -1 : finalResult;\n\t\treturn returnValue(applyPercentage(finalResult));\n\t}\n\tconst roundTo = typeof opts.round === \"number\" && Number.isFinite(opts.round) ? Math.floor(Math.max(0, opts.round)) : false;\n\tconst roundingFactorRaw = roundTo === false ? NaN : 10 ** roundTo;\n\tconst roundingFactor = Number.isFinite(roundingFactorRaw) ? roundingFactorRaw : NaN;\n\t/** Rounds to `roundTo` decimal places, falling back to `value` if the scaling overflows. */\n\tconst round = (value, scaled) => {\n\t\tconst result = Math.round(scaled) / roundingFactor;\n\t\treturn Number.isFinite(result) || !Number.isFinite(value) ? result : value;\n\t};\n\tif (numberGroup2.startsWith(\".\") || numberGroup2.startsWith(\"e\") || numberGroup2.startsWith(\"E\")) {\n\t\tconst decimalValue = parseFloat(`${finalResult}${numberGroup2}`);\n\t\tfinalResult = isNaN(roundingFactor) ? decimalValue : round(decimalValue, decimalValue * roundingFactor);\n\t} else if (leadingSlashRegex.test(numberGroup2)) {\n\t\tconst numerator = parseInt(numberGroup1);\n\t\tconst denominator = parseInt(numberGroup2.replace(\"/\", \"\"));\n\t\tparsedNumerator = numerator;\n\t\tparsedDenominator = denominator;\n\t\tfinalResult = isNaN(roundingFactor) ? numerator / denominator : round(numerator / denominator, numerator * roundingFactor / denominator);\n\t} else {\n\t\tconst [numerator, denominator] = numberGroup2.split(\"/\").map((v) => parseInt(v));\n\t\tparsedWhole = finalResult;\n\t\tparsedNumerator = numerator;\n\t\tparsedDenominator = denominator;\n\t\tfinalResult += isNaN(roundingFactor) ? numerator / denominator : round(numerator / denominator, numerator * roundingFactor / denominator);\n\t}\n\tfinalResult = sign === \"-\" ? finalResult * -1 : finalResult;\n\treturn returnValue(applyPercentage(finalResult));\n}\n//#endregion\n//#region src/isNumericQuantity.ts\n/**\n* Checks if a value represents a valid numeric quantity.\n*\n* Returns `true` if the value can be parsed as a number, `false` otherwise.\n* Accepts the same options as `numericQuantity`.\n*/\nconst isNumericQuantity = (quantity, options) => {\n\tconst result = numericQuantity(quantity, {\n\t\t...options,\n\t\tverbose: false\n\t});\n\treturn typeof result === \"bigint\" || !isNaN(result);\n};\n//#endregion\nexport { defaultOptions, isNumericQuantity, normalizeDigits, numericQuantity, numericRegex, numericRegexWithTrailingInvalid, parseRomanNumerals, romanNumeralRegex, romanNumeralUnicodeRegex, romanNumeralUnicodeToAsciiMap, romanNumeralValues, superSubDigitToAsciiMap, superSubDigitsRegex, vulgarFractionToAsciiMap, vulgarFractionsRegex };\n\n//# sourceMappingURL=numeric-quantity.mjs.map","import { numericQuantity } from 'numeric-quantity';\nimport { defaultOptions, fractionDecimalMatches, vulgarToAsciiMap } from './constants';\nimport type {\n FormatQuantity,\n ResolvedFormatQuantityOptions,\n SimpleFraction,\n Sixteenth,\n VulgarFraction,\n} from './types';\n\nconst superscriptDigits = '⁰¹²³⁴⁵⁶⁷⁸⁹';\nconst subscriptDigits = '₀₁₂₃₄₅₆₇₈₉';\n\n// oxlint-disable-next-line typescript/no-misused-spread\nconst toSuperscript = (s: string) => [...s].map(c => superscriptDigits[+c]).join('');\n// oxlint-disable-next-line typescript/no-misused-spread\nconst toSubscript = (s: string) => [...s].map(c => subscriptDigits[+c]).join('');\n\n/**\n * Applies the `vulgarFractions` or `fractionSlash` options as necessary.\n */\nconst getFraction = (\n vulgarFractionOrSixteenth: VulgarFraction | Sixteenth,\n { fractionSlash, vulgarFractions }: ResolvedFormatQuantityOptions\n) => {\n if (vulgarFractions) {\n return vulgarFractionOrSixteenth;\n }\n\n const plainFraction: SimpleFraction =\n vulgarToAsciiMap[vulgarFractionOrSixteenth as VulgarFraction] ?? 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 * Only `false` (matching disabled) or a non-negative number is a valid\n * `tolerance`. Everything else—negative numbers, `NaN`, non-numbers,\n * `null`, `undefined`—resolves to {@link defaultTolerance}.\n */\nconst isValidTolerance = (tolerance: unknown): tolerance is number | false =>\n tolerance === false || (typeof tolerance === 'number' && tolerance >= 0);\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 const opts: ResolvedFormatQuantityOptions = {\n ...defaultOptions,\n ...(typeof options === 'boolean'\n ? { vulgarFractions: options }\n : typeof options === 'object' && options !== null\n ? options\n : {}),\n };\n\n if (!isValidTolerance(opts.tolerance)) {\n opts.tolerance = defaultOptions.tolerance;\n }\n\n if (typeof opts.zeroFormat !== 'string') {\n opts.zeroFormat = defaultOptions.zeroFormat;\n }\n\n if (opts.allowTrailingInvalid !== false) {\n opts.allowTrailingInvalid = true;\n }\n\n return opts;\n};\n\n// oxfmt-ignore\nconst romanNumeralsByPlace = [\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 or bigint as Roman numerals. The number must be between\n * 1 and 3999, inclusive; any other value—including non-numbers,\n * `NaN`, and non-finite numbers—yields `null`.\n */\nexport const formatRomanNumerals = (quantity: number | bigint): string | null => {\n if (\n (typeof quantity !== 'number' && typeof quantity !== 'bigint') ||\n !(quantity >= 1 && quantity < 4000)\n ) {\n return null;\n }\n\n const qty = Number(quantity);\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 = `${romanNumeralsByPlace[+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 = (qty, options) => {\n // Only numbers, bigints, and strings are accepted. Anything else (objects, booleans,\n // nullish, arrays like `[1]` that would coerce to a numeric string) is\n // rejected up front so off-type inputs behave consistently.\n if (typeof qty !== 'number' && typeof qty !== 'string' && typeof qty !== 'bigint') {\n return null;\n }\n\n const opts = normalizeOptions(options);\n\n const qtyAsNumber =\n typeof qty !== 'number' && typeof qty !== 'bigint'\n ? numericQuantity(qty, {\n allowTrailingInvalid: opts.allowTrailingInvalid,\n romanNumerals: opts.romanNumerals,\n bigIntOnOverflow: true,\n round: false,\n })\n : qty;\n\n // Return `null` if input is not number-like.\n if (typeof qtyAsNumber === 'number' && isNaN(qtyAsNumber)) {\n return null;\n }\n\n // Return empty string (or configured `zeroFormat`) if the value is zero.\n if (Number(qtyAsNumber) === 0) {\n return opts.zeroFormat;\n }\n\n if (opts.romanNumerals) {\n return formatRomanNumerals(qtyAsNumber);\n }\n\n const absoluteValue =\n typeof qtyAsNumber === 'bigint'\n ? qtyAsNumber < 0n\n ? -qtyAsNumber\n : qtyAsNumber\n : Math.abs(qtyAsNumber);\n const flooredAbsVal =\n typeof absoluteValue === 'bigint' ? absoluteValue : Math.floor(absoluteValue);\n const wholeNumberStr = `${flooredAbsVal || ''}`;\n const decimalValue =\n typeof absoluteValue === 'bigint' ? 0 : absoluteValue - (flooredAbsVal as number);\n\n // For integers just return the given value as a string.\n if (decimalValue === 0) {\n return `${qtyAsNumber}`;\n }\n\n let closestMatch: VulgarFraction | Sixteenth | null = null;\n let closestMatchDiff = Infinity;\n // `tolerance: false` disables fraction matching entirely.\n if (opts.tolerance !== false) {\n for (const [num, vf] of fractionDecimalMatches) {\n const diff = Math.abs(decimalValue - num);\n // `diff === 0` keeps exact quotients matching even at `tolerance: 0`.\n if (diff < opts.tolerance || diff === 0) {\n if (diff === 0) {\n closestMatch = vf;\n break;\n }\n if (diff < closestMatchDiff) {\n closestMatch = vf;\n closestMatchDiff = diff;\n }\n }\n }\n }\n\n if (closestMatch) {\n const fraction = getFraction(closestMatch, opts);\n const isVulgar = fraction in vulgarToAsciiMap;\n const sep = wholeNumberStr ? (opts.separator ?? (isVulgar ? '' : ' ')) : '';\n return `${qtyAsNumber < 0 ? '-' : ''}${wholeNumberStr}${sep}${fraction}`;\n }\n\n return `${qtyAsNumber}`;\n};\n"],"x_google_ignoreList":[1],"mappings":"sRAWA,IAAa,EAAmB,MAKnB,EAA0D,OAAO,OAAO,CACnF,gBAAiB,GACjB,UAAW,EACX,cAAe,GACf,cAAe,GACf,WAAY,GACZ,qBAAsB,EACxB,CAAC,EAKY,EAAqE,OAAO,OAAO,CAC9F,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,KACP,CAAC,EAOY,EACX,OAAO,OAEH,CACE,CAAC,EAAI,GAAI,MAAM,EACf,CAAC,EAAI,GAAI,GAAG,EACZ,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,GAAI,MAAM,EACf,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,GAAI,MAAM,EACf,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,GAAI,MAAM,EACf,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,GAAI,MAAM,EACf,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,GAAK,GAAI,OAAO,EACjB,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,GAAK,GAAI,OAAO,EACjB,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,EAAI,EAAG,GAAG,EACX,CAAC,GAAK,GAAI,OAAO,CACnB,CAAC,CACD,IAAI,GAAS,OAAO,OAAO,CAAK,CAAC,CACrC,EC/EI,EAA0B,CAC/B,GACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,KACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,MACA,OACA,OACA,OACA,OACA,OACA,OACA,OACA,OACA,OACA,OACA,MACD,EAQM,EAAmB,GAAQ,EAAI,QAAQ,WAAa,GAAO,CAChE,IAAM,EAAK,EAAG,YAAY,CAAC,EAC3B,GAAI,GAAM,GAAI,OAAO,EACrB,IAAI,EAAK,EACL,EAAK,EAAwB,OAAS,EAC1C,KAAO,EAAK,GAAI,CACf,IAAM,EAAM,EAAK,EAAK,IAAM,EACxB,EAAwB,IAAQ,EAAI,EAAK,EACxC,EAAK,EAAM,CACjB,CACA,OAAO,OAAO,EAAK,EAAwB,EAAG,CAC/C,CAAC,EAIK,EAA0B,CAC/B,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAAK,IACL,IAA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IAAI,EAAkD,KAClD,EAAmB,IAEvB,GAAI,EAAK,YAAc,GACrB,IAAK,GAAM,CAAC,EAAK,KAAO,EAAwB,CAC9C,IAAM,EAAO,KAAK,IAAI,EAAe,CAAG,EAExC,GAAI,EAAO,EAAK,WAAa,IAAS,EAAG,CACvC,GAAI,IAAS,EAAG,CACd,EAAe,EACf,KACF,CACI,EAAO,IACT,EAAe,EACf,EAAmB,EAEvB,CACF,CAGF,GAAI,EAAc,CAChB,IAAM,EAAW,EAAY,EAAc,CAAI,EACzC,EAAW,KAAY,EACvB,EAAM,EAAkB,EAAK,YAAc,EAAW,GAAK,KAAQ,GACzE,MAAO,GAAG,EAAc,EAAI,IAAM,KAAK,IAAiB,IAAM,GAChE,CAEA,MAAO,GAAG,GACZ"}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "3.
|
|
2
|
+
"version": "3.2.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",
|
|
6
|
+
"type": "commonjs",
|
|
6
7
|
"files": [
|
|
7
8
|
"dist"
|
|
8
9
|
],
|
|
@@ -16,12 +17,13 @@
|
|
|
16
17
|
"default": "./dist/format-quantity.mjs"
|
|
17
18
|
},
|
|
18
19
|
"require": {
|
|
19
|
-
"types": "./dist/cjs/
|
|
20
|
+
"types": "./dist/cjs/index.d.ts",
|
|
20
21
|
"default": "./dist/cjs/index.js"
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
},
|
|
24
|
-
"types": "./dist/
|
|
25
|
+
"types": "./dist/format-quantity.legacy-esm.d.ts",
|
|
26
|
+
"sideEffects": false,
|
|
25
27
|
"unpkg": "./dist/format-quantity.umd.min.js",
|
|
26
28
|
"license": "MIT",
|
|
27
29
|
"keywords": [
|
|
@@ -42,33 +44,38 @@
|
|
|
42
44
|
"homepage": "https://github.com/jakeboone02/format-quantity",
|
|
43
45
|
"repository": {
|
|
44
46
|
"type": "git",
|
|
45
|
-
"url": "https://github.com/jakeboone02/format-quantity"
|
|
47
|
+
"url": "git+https://github.com/jakeboone02/format-quantity.git"
|
|
46
48
|
},
|
|
47
49
|
"scripts": {
|
|
48
|
-
"start": "bun --hot
|
|
49
|
-
"build": "
|
|
50
|
-
"docs": "
|
|
50
|
+
"start": "bun --hot main.html",
|
|
51
|
+
"build": "bun --bun x tsdown",
|
|
52
|
+
"docs:plugins": "bun build ./node_modules/typedoc-plugin-katex/src/index.tsx --outdir=. --target=node --external=typedoc --entry-naming=typedoc-plugin-katex.mjs",
|
|
53
|
+
"docs": "typedoc",
|
|
51
54
|
"test": "bun test",
|
|
52
55
|
"watch": "bun test --watch",
|
|
56
|
+
"lint": "bunx oxlint",
|
|
53
57
|
"publish:npm": "np",
|
|
54
|
-
"
|
|
58
|
+
"fmt": "oxfmt"
|
|
55
59
|
},
|
|
56
60
|
"devDependencies": {
|
|
57
|
-
"@types/
|
|
58
|
-
"@types/
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
61
|
+
"@types/bun": "^1.4.0",
|
|
62
|
+
"@types/node": "^26.4.0",
|
|
63
|
+
"@types/web": "^0.0.354",
|
|
64
|
+
"@typescript/native-preview": "^7.0.0-dev.20260707.2",
|
|
65
|
+
"np": "^12.0.1",
|
|
66
|
+
"oxfmt": "^0.65.0",
|
|
67
|
+
"oxlint": "^1.80.0",
|
|
68
|
+
"oxlint-tsgolint": "^7.0.2001",
|
|
69
|
+
"pkg-pr-new": "^0.0.88",
|
|
70
|
+
"tsdown": "^0.22.14",
|
|
71
|
+
"typedoc": "^0.28.20",
|
|
65
72
|
"typedoc-plugin-katex": "^0.1.2",
|
|
66
|
-
"typescript": "^
|
|
67
|
-
},
|
|
68
|
-
"publishConfig": {
|
|
69
|
-
"registry": "https://registry.npmjs.org"
|
|
73
|
+
"typescript": "^6.0.3"
|
|
70
74
|
},
|
|
71
75
|
"engines": {
|
|
72
|
-
"node": ">=
|
|
76
|
+
"node": ">=20"
|
|
77
|
+
},
|
|
78
|
+
"dependencies": {
|
|
79
|
+
"numeric-quantity": "^3.3.1"
|
|
73
80
|
}
|
|
74
81
|
}
|
|
@@ -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 };
|