numeric-quantity 2.1.0 → 3.0.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 +1 @@
1
- {"version":3,"sources":["../src/constants.ts","../src/parseRomanNumerals.ts","../src/numericQuantity.ts"],"sourcesContent":["import type {\n NumericQuantityOptions,\n RomanNumeralAscii,\n RomanNumeralUnicode,\n VulgarFraction,\n} from './types';\n\n// #region Arabic numerals\n/**\n * Map of Unicode fraction code points to their ASCII equivalents.\n */\nexport const vulgarFractionToAsciiMap: Record<\n VulgarFraction,\n `${number}/${number | ''}`\n> = {\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 '⅟': '1/',\n} as const;\n\n/**\n * Captures the individual elements of a numeric string.\n *\n * Capture groups:\n *\n * | # | Description | Example(s) |\n * | --- | ------------------------------------------------ | ------------------------------------------------------------------- |\n * | `0` | entire string | `\"2 1/3\"` from `\"2 1/3\"` |\n * | `1` | \"negative\" dash | `\"-\"` from `\"-2 1/3\"` |\n * | `2` | whole number or numerator | `\"2\"` from `\"2 1/3\"`; `\"1\"` from `\"1/3\"` |\n * | `3` | entire fraction, decimal portion, or denominator | `\" 1/3\"` from `\"2 1/3\"`; `\".33\"` from `\"2.33\"`; `\"/3\"` from `\"1/3\"` |\n *\n * _Capture group 2 may include comma/underscore separators._\n *\n * @example\n *\n * ```ts\n * numericRegex.exec(\"1\") // [ \"1\", \"1\", null, null ]\n * numericRegex.exec(\"1.23\") // [ \"1.23\", \"1\", \".23\", null ]\n * numericRegex.exec(\"1 2/3\") // [ \"1 2/3\", \"1\", \" 2/3\", \" 2\" ]\n * numericRegex.exec(\"2/3\") // [ \"2/3\", \"2\", \"/3\", null ]\n * numericRegex.exec(\"2 / 3\") // [ \"2 / 3\", \"2\", \"/ 3\", null ]\n * ```\n */\nexport const numericRegex: RegExp =\n /^(?=-?\\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 */\nexport const numericRegexWithTrailingInvalid: RegExp = new RegExp(\n numericRegex.source.replace(/\\$$/, '(?:\\\\s*[^\\\\.\\\\d\\\\/].*)?')\n);\n\n/**\n * Captures any Unicode vulgar fractions.\n */\nexport const vulgarFractionsRegex: RegExp = new RegExp(\n `(${Object.keys(vulgarFractionToAsciiMap).join('|')})`\n);\n// #endregion\n\n// #region Roman numerals\ntype RomanNumeralSequenceFragment =\n | `${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`;\n\n/**\n * Map of Roman numeral sequences to their decimal equivalents.\n */\nexport const romanNumeralValues: {\n [k in RomanNumeralSequenceFragment]?: number;\n} = {\n MMM: 3000,\n MM: 2000,\n M: 1000,\n CM: 900,\n DCCC: 800,\n DCC: 700,\n DC: 600,\n D: 500,\n CD: 400,\n CCC: 300,\n CC: 200,\n C: 100,\n XC: 90,\n LXXX: 80,\n LXX: 70,\n LX: 60,\n L: 50,\n XL: 40,\n XXX: 30,\n XX: 20,\n XII: 12, // only here for tests; not used in practice\n XI: 11, // only here for tests; not used in practice\n X: 10,\n IX: 9,\n VIII: 8,\n VII: 7,\n VI: 6,\n V: 5,\n IV: 4,\n III: 3,\n II: 2,\n I: 1,\n} as const;\n\n/**\n * Map of Unicode Roman numeral code points to their ASCII equivalents.\n */\nexport const romanNumeralUnicodeToAsciiMap: Record<\n RomanNumeralUnicode,\n keyof typeof romanNumeralValues\n> = {\n // Roman Numeral One (U+2160)\n Ⅰ: 'I',\n // Roman Numeral Two (U+2161)\n Ⅱ: 'II',\n // Roman Numeral Three (U+2162)\n Ⅲ: 'III',\n // Roman Numeral Four (U+2163)\n Ⅳ: 'IV',\n // Roman Numeral Five (U+2164)\n Ⅴ: 'V',\n // Roman Numeral Six (U+2165)\n Ⅵ: 'VI',\n // Roman Numeral Seven (U+2166)\n Ⅶ: 'VII',\n // Roman Numeral Eight (U+2167)\n Ⅷ: 'VIII',\n // Roman Numeral Nine (U+2168)\n Ⅸ: 'IX',\n // Roman Numeral Ten (U+2169)\n Ⅹ: 'X',\n // Roman Numeral Eleven (U+216A)\n Ⅺ: 'XI',\n // Roman Numeral Twelve (U+216B)\n Ⅻ: 'XII',\n // Roman Numeral Fifty (U+216C)\n Ⅼ: 'L',\n // Roman Numeral One Hundred (U+216D)\n Ⅽ: 'C',\n // Roman Numeral Five Hundred (U+216E)\n Ⅾ: 'D',\n // Roman Numeral One Thousand (U+216F)\n Ⅿ: 'M',\n // Small Roman Numeral One (U+2170)\n ⅰ: 'I',\n // Small Roman Numeral Two (U+2171)\n ⅱ: 'II',\n // Small Roman Numeral Three (U+2172)\n ⅲ: 'III',\n // Small Roman Numeral Four (U+2173)\n ⅳ: 'IV',\n // Small Roman Numeral Five (U+2174)\n ⅴ: 'V',\n // Small Roman Numeral Six (U+2175)\n ⅵ: 'VI',\n // Small Roman Numeral Seven (U+2176)\n ⅶ: 'VII',\n // Small Roman Numeral Eight (U+2177)\n ⅷ: 'VIII',\n // Small Roman Numeral Nine (U+2178)\n ⅸ: 'IX',\n // Small Roman Numeral Ten (U+2179)\n ⅹ: 'X',\n // Small Roman Numeral Eleven (U+217A)\n ⅺ: 'XI',\n // Small Roman Numeral Twelve (U+217B)\n ⅻ: 'XII',\n // Small Roman Numeral Fifty (U+217C)\n ⅼ: 'L',\n // Small Roman Numeral One Hundred (U+217D)\n ⅽ: 'C',\n // Small Roman Numeral Five Hundred (U+217E)\n ⅾ: 'D',\n // Small Roman Numeral One Thousand (U+217F)\n ⅿ: 'M',\n} as const;\n\n/**\n * Captures all Unicode Roman numeral code points.\n */\nexport const romanNumeralUnicodeRegex: RegExp = new RegExp(\n `(${Object.keys(romanNumeralUnicodeToAsciiMap).join('|')})`,\n 'gi'\n);\n\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 */\nexport const romanNumeralRegex: RegExp =\n /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;\n// #endregion\n\n/**\n * Default options for {@link numericQuantity}.\n */\nexport const defaultOptions: Required<NumericQuantityOptions> = {\n round: 3,\n allowTrailingInvalid: false,\n romanNumerals: false,\n bigIntOnOverflow: false,\n} as const;\n","import {\n romanNumeralRegex,\n romanNumeralUnicodeRegex,\n romanNumeralUnicodeToAsciiMap,\n romanNumeralValues,\n} from './constants';\n\n// Just a shorthand type alias\ntype RNV = keyof typeof romanNumeralValues;\n\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 */\nexport const parseRomanNumerals = (romanNumerals: string): number => {\n const normalized = `${romanNumerals}`\n // Convert Unicode Roman numerals to ASCII\n .replace(\n romanNumeralUnicodeRegex,\n (_m, rn: keyof typeof romanNumeralUnicodeToAsciiMap) =>\n romanNumeralUnicodeToAsciiMap[rn]\n )\n // Normalize to uppercase (more common for Roman numerals)\n .toUpperCase();\n\n const regexResult = romanNumeralRegex.exec(normalized);\n\n if (!regexResult) {\n return NaN;\n }\n\n const [, thousands, hundreds, tens, ones] = regexResult;\n\n return (\n (romanNumeralValues[thousands as RNV] ?? 0) +\n (romanNumeralValues[hundreds as RNV] ?? 0) +\n (romanNumeralValues[tens as RNV] ?? 0) +\n (romanNumeralValues[ones as RNV] ?? 0)\n );\n};\n","import {\n defaultOptions,\n numericRegex,\n numericRegexWithTrailingInvalid,\n vulgarFractionToAsciiMap,\n vulgarFractionsRegex,\n} from './constants';\nimport { parseRomanNumerals } from './parseRomanNumerals';\nimport type { NumericQuantityOptions } from './types';\n\nconst spaceThenSlashRegex = /^\\s*\\//;\n\n/**\n * Converts a string to a number, like an enhanced version of `parseFloat`.\n *\n * The string can include mixed numbers, vulgar fractions, or Roman numerals.\n */\nfunction numericQuantity(quantity: string | number): number;\nfunction numericQuantity(quantity: string | number): number;\nfunction numericQuantity(\n quantity: string | number,\n options: NumericQuantityOptions & { bigIntOnOverflow: true }\n): number | bigint;\nfunction numericQuantity(\n quantity: string | number,\n options?: NumericQuantityOptions\n): number;\nfunction numericQuantity(\n quantity: string | number,\n options: NumericQuantityOptions = defaultOptions\n) {\n if (typeof quantity === 'number' || typeof quantity === 'bigint') {\n return quantity;\n }\n\n let finalResult = NaN;\n\n // Coerce to string in case qty is a number\n const quantityAsString = `${quantity}`\n // Convert vulgar fractions to ASCII, with a leading space\n // to keep the whole number and the fraction separate\n .replace(\n vulgarFractionsRegex,\n (_m, vf: keyof typeof vulgarFractionToAsciiMap) =>\n ` ${vulgarFractionToAsciiMap[vf]}`\n )\n // Convert fraction slash to standard slash\n .replace('⁄', '/')\n .trim();\n\n // Bail out if the string was only white space\n if (quantityAsString.length === 0) {\n return NaN;\n }\n\n const opts: Required<NumericQuantityOptions> = {\n ...defaultOptions,\n ...options,\n };\n\n const regexResult = (\n opts.allowTrailingInvalid ? numericRegexWithTrailingInvalid : numericRegex\n ).exec(quantityAsString);\n\n // If the Arabic numeral regex fails, try Roman numerals\n if (!regexResult) {\n return opts.romanNumerals ? parseRomanNumerals(quantityAsString) : NaN;\n }\n\n const [, dash, ng1temp, ng2temp] = regexResult;\n const numberGroup1 = ng1temp.replace(/[,_]/g, '');\n const numberGroup2 = ng2temp?.replace(/[,_]/g, '');\n\n // Numerify capture group 1\n if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith('.')) {\n finalResult = 0;\n } else {\n if (opts.bigIntOnOverflow) {\n const asBigInt = dash ? BigInt(`-${numberGroup1}`) : BigInt(numberGroup1);\n if (\n asBigInt > BigInt(Number.MAX_SAFE_INTEGER) ||\n asBigInt < BigInt(Number.MIN_SAFE_INTEGER)\n ) {\n return asBigInt;\n }\n }\n\n finalResult = parseInt(numberGroup1);\n }\n\n // If capture group 2 is null, then we're dealing with an integer\n // and there is nothing left to process\n if (!numberGroup2) {\n return dash ? finalResult * -1 : finalResult;\n }\n\n const roundingFactor =\n opts.round === false\n ? NaN\n : parseFloat(`1e${Math.floor(Math.max(0, opts.round))}`);\n\n if (\n numberGroup2.startsWith('.') ||\n numberGroup2.startsWith('e') ||\n numberGroup2.startsWith('E')\n ) {\n // If first char of `numberGroup2` is \".\" or \"e\"/\"E\", it's a decimal\n const decimalValue = parseFloat(`${finalResult}${numberGroup2}`);\n finalResult = isNaN(roundingFactor)\n ? decimalValue\n : Math.round(decimalValue * roundingFactor) / roundingFactor;\n } else if (spaceThenSlashRegex.test(numberGroup2)) {\n // If the first non-space char is \"/\" it's a pure fraction (e.g. \"1/2\")\n const numerator = parseInt(numberGroup1);\n const denominator = parseInt(numberGroup2.replace('/', ''));\n finalResult = isNaN(roundingFactor)\n ? numerator / denominator\n : Math.round((numerator * roundingFactor) / denominator) / roundingFactor;\n } else {\n // Otherwise it's a mixed fraction (e.g. \"1 2/3\")\n const fractionArray = numberGroup2.split('/');\n const [numerator, denominator] = fractionArray.map(v => parseInt(v));\n finalResult += isNaN(roundingFactor)\n ? numerator / denominator\n : Math.round((numerator * roundingFactor) / denominator) / roundingFactor;\n }\n\n return dash ? finalResult * -1 : finalResult;\n}\n\nexport { numericQuantity };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAWO,IAAM,2BAGT;AAAA,EACF,QAAK;AAAA,EACL,QAAK;AAAA,EACL,QAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AAAA,EACL,UAAK;AACP;AA0BO,IAAM,eACX;AAIK,IAAM,kCAA0C,IAAI;AAAA,EACzD,aAAa,OAAO,QAAQ,OAAO,yBAAyB;AAC9D;AAKO,IAAM,uBAA+B,IAAI;AAAA,EAC9C,IAAI,OAAO,KAAK,wBAAwB,EAAE,KAAK,GAAG,CAAC;AACrD;AAaO,IAAM,qBAET;AAAA,EACF,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,KAAK;AAAA,EACL,IAAI;AAAA,EACJ,GAAG;AAAA,EACH,IAAI;AAAA,EACJ,K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W,IAAI;AAAA,EAC/D;AAEA,SAAO,OAAO,cAAc,KAAK;AACnC;","names":[]}
1
+ {"version":3,"file":"numeric-quantity.legacy-esm.js","names":["vulgarFractionToAsciiMap: Record<\n VulgarFraction,\n `${number}/${number | ''}`\n>","numericRegex: RegExp","numericRegexWithTrailingInvalid: RegExp","vulgarFractionsRegex: RegExp","romanNumeralValues: {\n [k in RomanNumeralSequenceFragment]?: number;\n}","romanNumeralUnicodeToAsciiMap: Record<\n RomanNumeralUnicode,\n keyof typeof romanNumeralValues\n>","romanNumeralUnicodeRegex: RegExp","romanNumeralRegex: RegExp","defaultOptions: Required<NumericQuantityOptions>","o","r","opts: Required<NumericQuantityOptions>"],"sources":["../src/constants.ts","../src/parseRomanNumerals.ts","../node_modules/@oxc-project/runtime/src/helpers/esm/typeof.js","../node_modules/@oxc-project/runtime/src/helpers/esm/toPrimitive.js","../node_modules/@oxc-project/runtime/src/helpers/esm/toPropertyKey.js","../node_modules/@oxc-project/runtime/src/helpers/esm/defineProperty.js","../node_modules/@oxc-project/runtime/src/helpers/esm/objectSpread2.js","../src/numericQuantity.ts"],"sourcesContent":["import type {\n NumericQuantityOptions,\n RomanNumeralAscii,\n RomanNumeralUnicode,\n VulgarFraction,\n} from './types';\n\n// #region Arabic numerals\n/**\n * Map of Unicode fraction code points to their ASCII equivalents.\n */\nexport const vulgarFractionToAsciiMap: Record<\n VulgarFraction,\n `${number}/${number | ''}`\n> = {\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 '⅟': '1/',\n} as const;\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 string | `\"2 1/3\"` from `\"2 1/3\"` |\n * | `1` | \"negative\" dash | `\"-\"` from `\"-2 1/3\"` |\n * | `2` | whole number or numerator | `\"2\"` from `\"2 1/3\"`; `\"1\"` from `\"1/3\"` |\n * | `3` | entire fraction, decimal portion, or denominator | `\" 1/3\"` from `\"2 1/3\"`; `\".33\"` from `\"2.33\"`; `\"/3\"` from `\"1/3\"` |\n *\n * _Capture group 2 may include comma/underscore separators._\n *\n * @example\n *\n * ```ts\n * numericRegex.exec(\"1\") // [ \"1\", \"1\", null, null ]\n * numericRegex.exec(\"1.23\") // [ \"1.23\", \"1\", \".23\", null ]\n * numericRegex.exec(\"1 2/3\") // [ \"1 2/3\", \"1\", \" 2/3\", \" 2\" ]\n * numericRegex.exec(\"2/3\") // [ \"2/3\", \"2\", \"/3\", null ]\n * numericRegex.exec(\"2 / 3\") // [ \"2 / 3\", \"2\", \"/ 3\", null ]\n * ```\n */\nexport const numericRegex: RegExp =\n /^(?=-?\\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 */\nexport const numericRegexWithTrailingInvalid: RegExp =\n /^(?=-?\\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/**\n * Captures any Unicode vulgar fractions.\n */\nexport const vulgarFractionsRegex: RegExp = /([¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟}])/g;\n// #endregion\n\n// #region Roman numerals\ntype RomanNumeralSequenceFragment =\n | `${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`\n | `${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}${RomanNumeralAscii}`;\n\n/**\n * Map of Roman numeral sequences to their decimal equivalents.\n */\nexport const romanNumeralValues: {\n [k in RomanNumeralSequenceFragment]?: number;\n} = {\n MMM: 3000,\n MM: 2000,\n M: 1000,\n CM: 900,\n DCCC: 800,\n DCC: 700,\n DC: 600,\n D: 500,\n CD: 400,\n CCC: 300,\n CC: 200,\n C: 100,\n XC: 90,\n LXXX: 80,\n LXX: 70,\n LX: 60,\n L: 50,\n XL: 40,\n XXX: 30,\n XX: 20,\n XII: 12, // only here for tests; not used in practice\n XI: 11, // only here for tests; not used in practice\n X: 10,\n IX: 9,\n VIII: 8,\n VII: 7,\n VI: 6,\n V: 5,\n IV: 4,\n III: 3,\n II: 2,\n I: 1,\n} as const;\n\n/**\n * Map of Unicode Roman numeral code points to their ASCII equivalents.\n */\nexport const romanNumeralUnicodeToAsciiMap: Record<\n RomanNumeralUnicode,\n keyof typeof romanNumeralValues\n> = {\n // Roman Numeral One (U+2160)\n Ⅰ: 'I',\n // Roman Numeral Two (U+2161)\n Ⅱ: 'II',\n // Roman Numeral Three (U+2162)\n Ⅲ: 'III',\n // Roman Numeral Four (U+2163)\n Ⅳ: 'IV',\n // Roman Numeral Five (U+2164)\n Ⅴ: 'V',\n // Roman Numeral Six (U+2165)\n Ⅵ: 'VI',\n // Roman Numeral Seven (U+2166)\n Ⅶ: 'VII',\n // Roman Numeral Eight (U+2167)\n Ⅷ: 'VIII',\n // Roman Numeral Nine (U+2168)\n Ⅸ: 'IX',\n // Roman Numeral Ten (U+2169)\n Ⅹ: 'X',\n // Roman Numeral Eleven (U+216A)\n Ⅺ: 'XI',\n // Roman Numeral Twelve (U+216B)\n Ⅻ: 'XII',\n // Roman Numeral Fifty (U+216C)\n Ⅼ: 'L',\n // Roman Numeral One Hundred (U+216D)\n Ⅽ: 'C',\n // Roman Numeral Five Hundred (U+216E)\n Ⅾ: 'D',\n // Roman Numeral One Thousand (U+216F)\n Ⅿ: 'M',\n // Small Roman Numeral One (U+2170)\n ⅰ: 'I',\n // Small Roman Numeral Two (U+2171)\n ⅱ: 'II',\n // Small Roman Numeral Three (U+2172)\n ⅲ: 'III',\n // Small Roman Numeral Four (U+2173)\n ⅳ: 'IV',\n // Small Roman Numeral Five (U+2174)\n ⅴ: 'V',\n // Small Roman Numeral Six (U+2175)\n ⅵ: 'VI',\n // Small Roman Numeral Seven (U+2176)\n ⅶ: 'VII',\n // Small Roman Numeral Eight (U+2177)\n ⅷ: 'VIII',\n // Small Roman Numeral Nine (U+2178)\n ⅸ: 'IX',\n // Small Roman Numeral Ten (U+2179)\n ⅹ: 'X',\n // Small Roman Numeral Eleven (U+217A)\n ⅺ: 'XI',\n // Small Roman Numeral Twelve (U+217B)\n ⅻ: 'XII',\n // Small Roman Numeral Fifty (U+217C)\n ⅼ: 'L',\n // Small Roman Numeral One Hundred (U+217D)\n ⅽ: 'C',\n // Small Roman Numeral Five Hundred (U+217E)\n ⅾ: 'D',\n // Small Roman Numeral One Thousand (U+217F)\n ⅿ: 'M',\n} as const;\n\n/**\n * Captures all Unicode Roman numeral code points.\n */\nexport const romanNumeralUnicodeRegex: RegExp =\n /([ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿ])/gi;\n\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 */\nexport const romanNumeralRegex: RegExp =\n /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;\n// #endregion\n\n/**\n * Default options for {@link numericQuantity}.\n */\nexport const defaultOptions: Required<NumericQuantityOptions> = {\n round: 3,\n allowTrailingInvalid: false,\n romanNumerals: false,\n bigIntOnOverflow: false,\n decimalSeparator: '.',\n} as const;\n","import {\n romanNumeralRegex,\n romanNumeralUnicodeRegex,\n romanNumeralUnicodeToAsciiMap,\n romanNumeralValues,\n} from './constants';\n\n// Just a shorthand type alias\ntype RNV = keyof typeof romanNumeralValues;\n\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 */\nexport const parseRomanNumerals = (romanNumerals: string): number => {\n const normalized = `${romanNumerals}`\n // Convert Unicode Roman numerals to ASCII\n .replace(\n romanNumeralUnicodeRegex,\n (_m, rn: keyof typeof romanNumeralUnicodeToAsciiMap) =>\n romanNumeralUnicodeToAsciiMap[rn]\n )\n // Normalize to uppercase (more common for Roman numerals)\n .toUpperCase();\n\n const regexResult = romanNumeralRegex.exec(normalized);\n\n if (!regexResult) {\n return NaN;\n }\n\n const [, thousands, hundreds, tens, ones] = regexResult;\n\n return (\n (romanNumeralValues[thousands as RNV] ?? 0) +\n (romanNumeralValues[hundreds as RNV] ?? 0) +\n (romanNumeralValues[tens as RNV] ?? 0) +\n (romanNumeralValues[ones as RNV] ?? 0)\n );\n};\n","function _typeof(o) {\n \"@babel/helpers - typeof\";\n\n return _typeof = \"function\" == typeof Symbol && \"symbol\" == typeof Symbol.iterator ? function (o) {\n return typeof o;\n } : function (o) {\n return o && \"function\" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? \"symbol\" : typeof o;\n }, _typeof(o);\n}\nexport { _typeof as default };","import _typeof from \"./typeof.js\";\nfunction toPrimitive(t, r) {\n if (\"object\" != _typeof(t) || !t) return t;\n var e = t[Symbol.toPrimitive];\n if (void 0 !== e) {\n var i = e.call(t, r || \"default\");\n if (\"object\" != _typeof(i)) return i;\n throw new TypeError(\"@@toPrimitive must return a primitive value.\");\n }\n return (\"string\" === r ? String : Number)(t);\n}\nexport { toPrimitive as default };","import _typeof from \"./typeof.js\";\nimport toPrimitive from \"./toPrimitive.js\";\nfunction toPropertyKey(t) {\n var i = toPrimitive(t, \"string\");\n return \"symbol\" == _typeof(i) ? i : i + \"\";\n}\nexport { toPropertyKey as default };","import toPropertyKey from \"./toPropertyKey.js\";\nfunction _defineProperty(e, r, t) {\n return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {\n value: t,\n enumerable: !0,\n configurable: !0,\n writable: !0\n }) : e[r] = t, e;\n}\nexport { _defineProperty as default };","import defineProperty from \"./defineProperty.js\";\nfunction ownKeys(e, r) {\n var t = Object.keys(e);\n if (Object.getOwnPropertySymbols) {\n var o = Object.getOwnPropertySymbols(e);\n r && (o = o.filter(function (r) {\n return Object.getOwnPropertyDescriptor(e, r).enumerable;\n })), t.push.apply(t, o);\n }\n return t;\n}\nfunction _objectSpread2(e) {\n for (var r = 1; r < arguments.length; r++) {\n var t = null != arguments[r] ? arguments[r] : {};\n r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {\n defineProperty(e, r, t[r]);\n }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {\n Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));\n });\n }\n return e;\n}\nexport { _objectSpread2 as default };","import {\n defaultOptions,\n numericRegex,\n numericRegexWithTrailingInvalid,\n vulgarFractionToAsciiMap,\n vulgarFractionsRegex,\n} from './constants';\nimport { parseRomanNumerals } from './parseRomanNumerals';\nimport type { NumericQuantityOptions } from './types';\n\nconst spaceThenSlashRegex = /^\\s*\\//;\n\n/**\n * Converts a string to a number, like an enhanced version of `parseFloat`.\n *\n * The string can include mixed numbers, vulgar fractions, or Roman numerals.\n */\nfunction numericQuantity(quantity: string | number): number;\nfunction numericQuantity(\n quantity: string | number,\n options: NumericQuantityOptions & { bigIntOnOverflow: true }\n): number | bigint;\nfunction numericQuantity(\n quantity: string | number,\n options?: NumericQuantityOptions\n): number;\nfunction numericQuantity(\n quantity: string | number,\n options: NumericQuantityOptions = defaultOptions\n) {\n if (typeof quantity === 'number' || typeof quantity === 'bigint') {\n return quantity;\n }\n\n let finalResult = NaN;\n\n // Coerce to string in case qty is a number\n const quantityAsString = `${quantity}`\n // Convert vulgar fractions to ASCII, with a leading space\n // to keep the whole number and the fraction separate\n .replace(\n vulgarFractionsRegex,\n (_m, vf: keyof typeof vulgarFractionToAsciiMap) =>\n ` ${vulgarFractionToAsciiMap[vf]}`\n )\n // Convert fraction slash to standard slash\n .replace('⁄', '/')\n .trim();\n\n // Bail out if the string was only white space\n if (quantityAsString.length === 0) {\n return NaN;\n }\n\n const opts: Required<NumericQuantityOptions> = {\n ...defaultOptions,\n ...options,\n };\n\n let normalizedString = quantityAsString;\n\n if (opts.decimalSeparator === ',') {\n const commaCount = (quantityAsString.match(/,/g) || []).length;\n if (commaCount === 1) {\n // Treat lone comma as decimal separator; remove all \".\" since they represent\n // thousands/whatever separators\n normalizedString = quantityAsString\n .replaceAll('.', '_')\n .replace(',', '.');\n } else if (commaCount > 1) {\n // The second comma and everything after is \"trailing invalid\"\n if (!opts.allowTrailingInvalid) {\n // Bail out if trailing invalid is not allowed\n return NaN;\n }\n\n const firstCommaIndex = quantityAsString.indexOf(',');\n const secondCommaIndex = quantityAsString.indexOf(\n ',',\n firstCommaIndex + 1\n );\n const beforeSecondComma = quantityAsString\n .substring(0, secondCommaIndex)\n .replaceAll('.', '_')\n .replace(',', '.');\n const afterSecondComma = quantityAsString.substring(secondCommaIndex + 1);\n normalizedString = opts.allowTrailingInvalid\n ? beforeSecondComma + '&' + afterSecondComma\n : beforeSecondComma;\n } else {\n // No comma as decimal separator, so remove all \".\" since they represent\n // thousands/whatever separators\n normalizedString = quantityAsString.replaceAll('.', '_');\n }\n }\n\n const regexResult = (\n opts.allowTrailingInvalid ? numericRegexWithTrailingInvalid : numericRegex\n ).exec(normalizedString);\n\n // If the Arabic numeral regex fails, try Roman numerals\n if (!regexResult) {\n return opts.romanNumerals ? parseRomanNumerals(quantityAsString) : NaN;\n }\n\n const [, dash, ng1temp, ng2temp] = regexResult;\n const numberGroup1 = ng1temp.replaceAll(',', '').replaceAll('_', '');\n const numberGroup2 = ng2temp?.replaceAll(',', '').replaceAll('_', '');\n\n // Numerify capture group 1\n if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith('.')) {\n finalResult = 0;\n } else {\n if (opts.bigIntOnOverflow) {\n const asBigInt = dash ? BigInt(`-${numberGroup1}`) : BigInt(numberGroup1);\n if (\n asBigInt > BigInt(Number.MAX_SAFE_INTEGER) ||\n asBigInt < BigInt(Number.MIN_SAFE_INTEGER)\n ) {\n return asBigInt;\n }\n }\n\n finalResult = parseInt(numberGroup1);\n }\n\n // If capture group 2 is null, then we're dealing with an integer\n // and there is nothing left to process\n if (!numberGroup2) {\n return dash ? finalResult * -1 : finalResult;\n }\n\n const roundingFactor =\n opts.round === false\n ? NaN\n : parseFloat(`1e${Math.floor(Math.max(0, opts.round))}`);\n\n if (\n numberGroup2.startsWith('.') ||\n numberGroup2.startsWith('e') ||\n numberGroup2.startsWith('E')\n ) {\n // If first char of `numberGroup2` is \".\" or \"e\"/\"E\", it's a decimal\n const decimalValue = parseFloat(`${finalResult}${numberGroup2}`);\n finalResult = isNaN(roundingFactor)\n ? decimalValue\n : Math.round(decimalValue * roundingFactor) / roundingFactor;\n } else if (spaceThenSlashRegex.test(numberGroup2)) {\n // If the first non-space char is \"/\" it's a pure fraction (e.g. \"1/2\")\n const numerator = parseInt(numberGroup1);\n const denominator = parseInt(numberGroup2.replace('/', ''));\n finalResult = isNaN(roundingFactor)\n ? numerator / denominator\n : Math.round((numerator * roundingFactor) / denominator) / roundingFactor;\n } else {\n // Otherwise it's a mixed fraction (e.g. \"1 2/3\")\n const fractionArray = numberGroup2.split('/');\n const [numerator, denominator] = fractionArray.map(v => parseInt(v));\n finalResult += isNaN(roundingFactor)\n ? numerator / denominator\n : Math.round((numerator * roundingFactor) / denominator) / roundingFactor;\n }\n\n return dash ? finalResult * -1 : finalResult;\n}\n\nexport { numericQuantity };\n"],"x_google_ignoreList":[2,3,4,5,6],"mappings":";;;;AAWA,MAAaA,2BAGT;CACF,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;CACL,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BP,MAAaC,eACX;;;;AAIF,MAAaC,kCACX;;;;AAKF,MAAaC,uBAA+B;;;;AAa5C,MAAaC,qBAET;CACF,KAAK;CACL,IAAI;CACJ,GAAG;CACH,IAAI;CACJ,MAAM;CACN,KAAK;CAC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,KAAK;CACjE,MAAM,iEAAe,QAAS,WAAW,KAAK,IAAI,WAAW,KAAK;AAGlE,KAAI,CAAC,gBAAgB,gBAAgB,aAAa,WAAW,KAC3D,eAAc;MACT;AACL,MAAI,KAAK,kBAAkB;GACzB,MAAM,WAAW,OAAO,OAAO,IAAI,kBAAkB,OAAO;AAC5D,OACE,WAAW,OAAO,OAAO,qBACzB,WAAW,OAAO,OAAO,kBAEzB,QAAO;;AAIX,gBAAc,SAAS;;AAKzB,KAAI,CAAC,aACH,QAAO,OAAO,cAAc,KAAK;CAGnC,MAAM,iBACJ,KAAK,UAAU,QACX,MACA,WAAW,KAAK,KAAK,MAAM,KAAK,IAAI,GAAG,KAAK;AAElD,KACE,aAAa,WAAW,QACxB,aAAa,WAAW,QACxB,aAAa,WAAW,MACxB;EAEA,MAAM,eAAe,WAAW,GAAG,cAAc;AACjD,gBAAc,MAAM,kBAChB,eACA,KAAK,MAAM,eAAe,kBAAkB;YACvC,oBAAoB,KAAK,eAAe;EAEjD,MAAM,YAAY,SAAS;EAC3B,MAAM,cAAc,SAAS,aAAa,QAAQ,KAAK;AACvD,gBAAc,MAAM,kBAChB,YAAY,cACZ,KAAK,MAAO,YAAY,iBAAkB,eAAe;QACxD;EAEL,MAAM,gBAAgB,aAAa,MAAM;EACzC,MAAM,CAAC,WAAW,eAAe,cAAc,KAAI,MAAK,SAAS;AACjE,iBAAe,MAAM,kBACjB,YAAY,cACZ,KAAK,MAAO,YAAY,iBAAkB,eAAe;;AAG/D,QAAO,OAAO,cAAc,KAAK"}
@@ -1,225 +1,245 @@
1
- // src/constants.ts
2
- var vulgarFractionToAsciiMap = {
3
- "\xBC": "1/4",
4
- "\xBD": "1/2",
5
- "\xBE": "3/4",
6
- "\u2150": "1/7",
7
- "\u2151": "1/9",
8
- "\u2152": "1/10",
9
- "\u2153": "1/3",
10
- "\u2154": "2/3",
11
- "\u2155": "1/5",
12
- "\u2156": "2/5",
13
- "\u2157": "3/5",
14
- "\u2158": "4/5",
15
- "\u2159": "1/6",
16
- "\u215A": "5/6",
17
- "\u215B": "1/8",
18
- "\u215C": "3/8",
19
- "\u215D": "5/8",
20
- "\u215E": "7/8",
21
- "\u215F": "1/"
1
+ //#region src/constants.ts
2
+ /**
3
+ * Map of Unicode fraction code points to their ASCII equivalents.
4
+ */
5
+ const vulgarFractionToAsciiMap = {
6
+ "¼": "1/4",
7
+ "½": "1/2",
8
+ "¾": "3/4",
9
+ "": "1/7",
10
+ "": "1/9",
11
+ "": "1/10",
12
+ "": "1/3",
13
+ "": "2/3",
14
+ "": "1/5",
15
+ "": "2/5",
16
+ "": "3/5",
17
+ "": "4/5",
18
+ "": "1/6",
19
+ "": "5/6",
20
+ "": "1/8",
21
+ "": "3/8",
22
+ "⅝": "5/8",
23
+ "⅞": "7/8",
24
+ "⅟": "1/"
22
25
  };
23
- var 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)?)?$/;
24
- var numericRegexWithTrailingInvalid = new RegExp(
25
- numericRegex.source.replace(/\$$/, "(?:\\s*[^\\.\\d\\/].*)?")
26
- );
27
- var vulgarFractionsRegex = new RegExp(
28
- `(${Object.keys(vulgarFractionToAsciiMap).join("|")})`
29
- );
30
- var romanNumeralValues = {
31
- MMM: 3e3,
32
- MM: 2e3,
33
- M: 1e3,
34
- CM: 900,
35
- DCCC: 800,
36
- DCC: 700,
37
- DC: 600,
38
- D: 500,
39
- CD: 400,
40
- CCC: 300,
41
- CC: 200,
42
- C: 100,
43
- XC: 90,
44
- LXXX: 80,
45
- LXX: 70,
46
- LX: 60,
47
- L: 50,
48
- XL: 40,
49
- XXX: 30,
50
- XX: 20,
51
- XII: 12,
52
- // only here for tests; not used in practice
53
- XI: 11,
54
- // only here for tests; not used in practice
55
- X: 10,
56
- IX: 9,
57
- VIII: 8,
58
- VII: 7,
59
- VI: 6,
60
- V: 5,
61
- IV: 4,
62
- III: 3,
63
- II: 2,
64
- I: 1
26
+ /**
27
+ * Captures the individual elements of a numeric string. Commas and underscores are allowed
28
+ * as separators, as long as they appear between digits and are not consecutive.
29
+ *
30
+ * Capture groups:
31
+ *
32
+ * | # | Description | Example(s) |
33
+ * | --- | ------------------------------------------------ | ------------------------------------------------------------------- |
34
+ * | `0` | entire string | `"2 1/3"` from `"2 1/3"` |
35
+ * | `1` | "negative" dash | `"-"` from `"-2 1/3"` |
36
+ * | `2` | whole number or numerator | `"2"` from `"2 1/3"`; `"1"` from `"1/3"` |
37
+ * | `3` | entire fraction, decimal portion, or denominator | `" 1/3"` from `"2 1/3"`; `".33"` from `"2.33"`; `"/3"` from `"1/3"` |
38
+ *
39
+ * _Capture group 2 may include comma/underscore separators._
40
+ *
41
+ * @example
42
+ *
43
+ * ```ts
44
+ * numericRegex.exec("1") // [ "1", "1", null, null ]
45
+ * numericRegex.exec("1.23") // [ "1.23", "1", ".23", null ]
46
+ * numericRegex.exec("1 2/3") // [ "1 2/3", "1", " 2/3", " 2" ]
47
+ * numericRegex.exec("2/3") // [ "2/3", "2", "/3", null ]
48
+ * numericRegex.exec("2 / 3") // [ "2 / 3", "2", "/ 3", null ]
49
+ * ```
50
+ */
51
+ const 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)*)?$/;
52
+ /**
53
+ * Same as {@link numericRegex}, but allows (and ignores) trailing invalid characters.
54
+ */
55
+ const 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/].*)?/;
56
+ /**
57
+ * Captures any Unicode vulgar fractions.
58
+ */
59
+ const vulgarFractionsRegex = /([¼½¾⅐⅑⅒⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟}])/g;
60
+ /**
61
+ * Map of Roman numeral sequences to their decimal equivalents.
62
+ */
63
+ const romanNumeralValues = {
64
+ MMM: 3e3,
65
+ MM: 2e3,
66
+ M: 1e3,
67
+ CM: 900,
68
+ DCCC: 800,
69
+ DCC: 700,
70
+ DC: 600,
71
+ D: 500,
72
+ CD: 400,
73
+ CCC: 300,
74
+ CC: 200,
75
+ C: 100,
76
+ XC: 90,
77
+ LXXX: 80,
78
+ LXX: 70,
79
+ LX: 60,
80
+ L: 50,
81
+ XL: 40,
82
+ XXX: 30,
83
+ XX: 20,
84
+ XII: 12,
85
+ XI: 11,
86
+ X: 10,
87
+ IX: 9,
88
+ VIII: 8,
89
+ VII: 7,
90
+ VI: 6,
91
+ V: 5,
92
+ IV: 4,
93
+ III: 3,
94
+ II: 2,
95
+ I: 1
65
96
  };
66
- var romanNumeralUnicodeToAsciiMap = {
67
- // Roman Numeral One (U+2160)
68
- "\u2160": "I",
69
- // Roman Numeral Two (U+2161)
70
- "\u2161": "II",
71
- // Roman Numeral Three (U+2162)
72
- "\u2162": "III",
73
- // Roman Numeral Four (U+2163)
74
- "\u2163": "IV",
75
- // Roman Numeral Five (U+2164)
76
- "\u2164": "V",
77
- // Roman Numeral Six (U+2165)
78
- "\u2165": "VI",
79
- // Roman Numeral Seven (U+2166)
80
- "\u2166": "VII",
81
- // Roman Numeral Eight (U+2167)
82
- "\u2167": "VIII",
83
- // Roman Numeral Nine (U+2168)
84
- "\u2168": "IX",
85
- // Roman Numeral Ten (U+2169)
86
- "\u2169": "X",
87
- // Roman Numeral Eleven (U+216A)
88
- "\u216A": "XI",
89
- // Roman Numeral Twelve (U+216B)
90
- "\u216B": "XII",
91
- // Roman Numeral Fifty (U+216C)
92
- "\u216C": "L",
93
- // Roman Numeral One Hundred (U+216D)
94
- "\u216D": "C",
95
- // Roman Numeral Five Hundred (U+216E)
96
- "\u216E": "D",
97
- // Roman Numeral One Thousand (U+216F)
98
- "\u216F": "M",
99
- // Small Roman Numeral One (U+2170)
100
- "\u2170": "I",
101
- // Small Roman Numeral Two (U+2171)
102
- "\u2171": "II",
103
- // Small Roman Numeral Three (U+2172)
104
- "\u2172": "III",
105
- // Small Roman Numeral Four (U+2173)
106
- "\u2173": "IV",
107
- // Small Roman Numeral Five (U+2174)
108
- "\u2174": "V",
109
- // Small Roman Numeral Six (U+2175)
110
- "\u2175": "VI",
111
- // Small Roman Numeral Seven (U+2176)
112
- "\u2176": "VII",
113
- // Small Roman Numeral Eight (U+2177)
114
- "\u2177": "VIII",
115
- // Small Roman Numeral Nine (U+2178)
116
- "\u2178": "IX",
117
- // Small Roman Numeral Ten (U+2179)
118
- "\u2179": "X",
119
- // Small Roman Numeral Eleven (U+217A)
120
- "\u217A": "XI",
121
- // Small Roman Numeral Twelve (U+217B)
122
- "\u217B": "XII",
123
- // Small Roman Numeral Fifty (U+217C)
124
- "\u217C": "L",
125
- // Small Roman Numeral One Hundred (U+217D)
126
- "\u217D": "C",
127
- // Small Roman Numeral Five Hundred (U+217E)
128
- "\u217E": "D",
129
- // Small Roman Numeral One Thousand (U+217F)
130
- "\u217F": "M"
97
+ /**
98
+ * Map of Unicode Roman numeral code points to their ASCII equivalents.
99
+ */
100
+ const romanNumeralUnicodeToAsciiMap = {
101
+ Ⅰ: "I",
102
+ Ⅱ: "II",
103
+ Ⅲ: "III",
104
+ Ⅳ: "IV",
105
+ Ⅴ: "V",
106
+ Ⅵ: "VI",
107
+ Ⅶ: "VII",
108
+ Ⅷ: "VIII",
109
+ Ⅸ: "IX",
110
+ Ⅹ: "X",
111
+ Ⅺ: "XI",
112
+ Ⅻ: "XII",
113
+ Ⅼ: "L",
114
+ Ⅽ: "C",
115
+ Ⅾ: "D",
116
+ Ⅿ: "M",
117
+ ⅰ: "I",
118
+ ⅱ: "II",
119
+ ⅲ: "III",
120
+ ⅳ: "IV",
121
+ ⅴ: "V",
122
+ ⅵ: "VI",
123
+ ⅶ: "VII",
124
+ ⅷ: "VIII",
125
+ ⅸ: "IX",
126
+ ⅹ: "X",
127
+ ⅺ: "XI",
128
+ ⅻ: "XII",
129
+ ⅼ: "L",
130
+ ⅽ: "C",
131
+ ⅾ: "D",
132
+ ⅿ: "M"
131
133
  };
132
- var romanNumeralUnicodeRegex = new RegExp(
133
- `(${Object.keys(romanNumeralUnicodeToAsciiMap).join("|")})`,
134
- "gi"
135
- );
136
- var romanNumeralRegex = /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;
137
- var defaultOptions = {
138
- round: 3,
139
- allowTrailingInvalid: false,
140
- romanNumerals: false,
141
- bigIntOnOverflow: false
134
+ /**
135
+ * Captures all Unicode Roman numeral code points.
136
+ */
137
+ const romanNumeralUnicodeRegex = /([ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅬⅭⅮⅯⅰⅱⅲⅳⅴⅵⅶⅷⅸⅹⅺⅻⅼⅽⅾⅿ])/gi;
138
+ /**
139
+ * Captures a valid Roman numeral sequence.
140
+ *
141
+ * Capture groups:
142
+ *
143
+ * | # | Description | Example |
144
+ * | --- | --------------- | ------------------------ |
145
+ * | `0` | Entire string | "MCCXIV" from "MCCXIV" |
146
+ * | `1` | Thousands | "M" from "MCCXIV" |
147
+ * | `2` | Hundreds | "CC" from "MCCXIV" |
148
+ * | `3` | Tens | "X" from "MCCXIV" |
149
+ * | `4` | Ones | "IV" from "MCCXIV" |
150
+ *
151
+ * @example
152
+ *
153
+ * ```ts
154
+ * romanNumeralRegex.exec("M") // [ "M", "M", "", "", "" ]
155
+ * romanNumeralRegex.exec("XII") // [ "XII", "", "", "X", "II" ]
156
+ * romanNumeralRegex.exec("MCCXIV") // [ "MCCXIV", "M", "CC", "X", "IV" ]
157
+ * ```
158
+ */
159
+ const romanNumeralRegex = /^(?=[MDCLXVI])(M{0,3})(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$/i;
160
+ /**
161
+ * Default options for {@link numericQuantity}.
162
+ */
163
+ const defaultOptions = {
164
+ round: 3,
165
+ allowTrailingInvalid: false,
166
+ romanNumerals: false,
167
+ bigIntOnOverflow: false,
168
+ decimalSeparator: "."
142
169
  };
143
170
 
144
- // src/parseRomanNumerals.ts
145
- var parseRomanNumerals = (romanNumerals) => {
146
- const normalized = `${romanNumerals}`.replace(
147
- romanNumeralUnicodeRegex,
148
- (_m, rn) => romanNumeralUnicodeToAsciiMap[rn]
149
- ).toUpperCase();
150
- const regexResult = romanNumeralRegex.exec(normalized);
151
- if (!regexResult) {
152
- return NaN;
153
- }
154
- const [, thousands, hundreds, tens, ones] = regexResult;
155
- return (romanNumeralValues[thousands] ?? 0) + (romanNumeralValues[hundreds] ?? 0) + (romanNumeralValues[tens] ?? 0) + (romanNumeralValues[ones] ?? 0);
171
+ //#endregion
172
+ //#region src/parseRomanNumerals.ts
173
+ /**
174
+ * Converts a string of Roman numerals to a number, like `parseInt`
175
+ * for Roman numerals. Uses modern, strict rules (only 1 to 3999).
176
+ *
177
+ * The string can include ASCII representations of Roman numerals
178
+ * or Unicode Roman numeral code points (`U+2160` through `U+217F`).
179
+ */
180
+ const parseRomanNumerals = (romanNumerals) => {
181
+ const normalized = `${romanNumerals}`.replace(romanNumeralUnicodeRegex, (_m, rn) => romanNumeralUnicodeToAsciiMap[rn]).toUpperCase();
182
+ const regexResult = romanNumeralRegex.exec(normalized);
183
+ if (!regexResult) return NaN;
184
+ const [, thousands, hundreds, tens, ones] = regexResult;
185
+ return (romanNumeralValues[thousands] ?? 0) + (romanNumeralValues[hundreds] ?? 0) + (romanNumeralValues[tens] ?? 0) + (romanNumeralValues[ones] ?? 0);
156
186
  };
157
187
 
158
- // src/numericQuantity.ts
159
- var spaceThenSlashRegex = /^\s*\//;
188
+ //#endregion
189
+ //#region src/numericQuantity.ts
190
+ const spaceThenSlashRegex = /^\s*\//;
160
191
  function numericQuantity(quantity, options = defaultOptions) {
161
- if (typeof quantity === "number" || typeof quantity === "bigint") {
162
- return quantity;
163
- }
164
- let finalResult = NaN;
165
- const quantityAsString = `${quantity}`.replace(
166
- vulgarFractionsRegex,
167
- (_m, vf) => ` ${vulgarFractionToAsciiMap[vf]}`
168
- ).replace("\u2044", "/").trim();
169
- if (quantityAsString.length === 0) {
170
- return NaN;
171
- }
172
- const opts = {
173
- ...defaultOptions,
174
- ...options
175
- };
176
- const regexResult = (opts.allowTrailingInvalid ? numericRegexWithTrailingInvalid : numericRegex).exec(quantityAsString);
177
- if (!regexResult) {
178
- return opts.romanNumerals ? parseRomanNumerals(quantityAsString) : NaN;
179
- }
180
- const [, dash, ng1temp, ng2temp] = regexResult;
181
- const numberGroup1 = ng1temp.replace(/[,_]/g, "");
182
- const numberGroup2 = ng2temp?.replace(/[,_]/g, "");
183
- if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith(".")) {
184
- finalResult = 0;
185
- } else {
186
- if (opts.bigIntOnOverflow) {
187
- const asBigInt = dash ? BigInt(`-${numberGroup1}`) : BigInt(numberGroup1);
188
- if (asBigInt > BigInt(Number.MAX_SAFE_INTEGER) || asBigInt < BigInt(Number.MIN_SAFE_INTEGER)) {
189
- return asBigInt;
190
- }
191
- }
192
- finalResult = parseInt(numberGroup1);
193
- }
194
- if (!numberGroup2) {
195
- return dash ? finalResult * -1 : finalResult;
196
- }
197
- const roundingFactor = opts.round === false ? NaN : parseFloat(`1e${Math.floor(Math.max(0, opts.round))}`);
198
- if (numberGroup2.startsWith(".") || numberGroup2.startsWith("e") || numberGroup2.startsWith("E")) {
199
- const decimalValue = parseFloat(`${finalResult}${numberGroup2}`);
200
- finalResult = isNaN(roundingFactor) ? decimalValue : Math.round(decimalValue * roundingFactor) / roundingFactor;
201
- } else if (spaceThenSlashRegex.test(numberGroup2)) {
202
- const numerator = parseInt(numberGroup1);
203
- const denominator = parseInt(numberGroup2.replace("/", ""));
204
- finalResult = isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
205
- } else {
206
- const fractionArray = numberGroup2.split("/");
207
- const [numerator, denominator] = fractionArray.map((v) => parseInt(v));
208
- finalResult += isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
209
- }
210
- return dash ? finalResult * -1 : finalResult;
192
+ if (typeof quantity === "number" || typeof quantity === "bigint") return quantity;
193
+ let finalResult = NaN;
194
+ const quantityAsString = `${quantity}`.replace(vulgarFractionsRegex, (_m, vf) => ` ${vulgarFractionToAsciiMap[vf]}`).replace("⁄", "/").trim();
195
+ if (quantityAsString.length === 0) return NaN;
196
+ const opts = {
197
+ ...defaultOptions,
198
+ ...options
199
+ };
200
+ let normalizedString = quantityAsString;
201
+ if (opts.decimalSeparator === ",") {
202
+ const commaCount = (quantityAsString.match(/,/g) || []).length;
203
+ if (commaCount === 1) normalizedString = quantityAsString.replaceAll(".", "_").replace(",", ".");
204
+ else if (commaCount > 1) {
205
+ if (!opts.allowTrailingInvalid) return NaN;
206
+ const firstCommaIndex = quantityAsString.indexOf(",");
207
+ const secondCommaIndex = quantityAsString.indexOf(",", firstCommaIndex + 1);
208
+ const beforeSecondComma = quantityAsString.substring(0, secondCommaIndex).replaceAll(".", "_").replace(",", ".");
209
+ const afterSecondComma = quantityAsString.substring(secondCommaIndex + 1);
210
+ normalizedString = opts.allowTrailingInvalid ? beforeSecondComma + "&" + afterSecondComma : beforeSecondComma;
211
+ } else normalizedString = quantityAsString.replaceAll(".", "_");
212
+ }
213
+ const regexResult = (opts.allowTrailingInvalid ? numericRegexWithTrailingInvalid : numericRegex).exec(normalizedString);
214
+ if (!regexResult) return opts.romanNumerals ? parseRomanNumerals(quantityAsString) : NaN;
215
+ const [, dash, ng1temp, ng2temp] = regexResult;
216
+ const numberGroup1 = ng1temp.replaceAll(",", "").replaceAll("_", "");
217
+ const numberGroup2 = ng2temp === null || ng2temp === void 0 ? void 0 : ng2temp.replaceAll(",", "").replaceAll("_", "");
218
+ if (!numberGroup1 && numberGroup2 && numberGroup2.startsWith(".")) finalResult = 0;
219
+ else {
220
+ if (opts.bigIntOnOverflow) {
221
+ const asBigInt = dash ? BigInt(`-${numberGroup1}`) : BigInt(numberGroup1);
222
+ if (asBigInt > BigInt(Number.MAX_SAFE_INTEGER) || asBigInt < BigInt(Number.MIN_SAFE_INTEGER)) return asBigInt;
223
+ }
224
+ finalResult = parseInt(numberGroup1);
225
+ }
226
+ if (!numberGroup2) return dash ? finalResult * -1 : finalResult;
227
+ const roundingFactor = opts.round === false ? NaN : parseFloat(`1e${Math.floor(Math.max(0, opts.round))}`);
228
+ if (numberGroup2.startsWith(".") || numberGroup2.startsWith("e") || numberGroup2.startsWith("E")) {
229
+ const decimalValue = parseFloat(`${finalResult}${numberGroup2}`);
230
+ finalResult = isNaN(roundingFactor) ? decimalValue : Math.round(decimalValue * roundingFactor) / roundingFactor;
231
+ } else if (spaceThenSlashRegex.test(numberGroup2)) {
232
+ const numerator = parseInt(numberGroup1);
233
+ const denominator = parseInt(numberGroup2.replace("/", ""));
234
+ finalResult = isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
235
+ } else {
236
+ const fractionArray = numberGroup2.split("/");
237
+ const [numerator, denominator] = fractionArray.map((v) => parseInt(v));
238
+ finalResult += isNaN(roundingFactor) ? numerator / denominator : Math.round(numerator * roundingFactor / denominator) / roundingFactor;
239
+ }
240
+ return dash ? finalResult * -1 : finalResult;
211
241
  }
212
- export {
213
- defaultOptions,
214
- numericQuantity,
215
- numericRegex,
216
- numericRegexWithTrailingInvalid,
217
- parseRomanNumerals,
218
- romanNumeralRegex,
219
- romanNumeralUnicodeRegex,
220
- romanNumeralUnicodeToAsciiMap,
221
- romanNumeralValues,
222
- vulgarFractionToAsciiMap,
223
- vulgarFractionsRegex
224
- };
242
+
243
+ //#endregion
244
+ export { defaultOptions, numericQuantity, numericRegex, numericRegexWithTrailingInvalid, parseRomanNumerals, romanNumeralRegex, romanNumeralUnicodeRegex, romanNumeralUnicodeToAsciiMap, romanNumeralValues, vulgarFractionToAsciiMap, vulgarFractionsRegex };
225
245
  //# sourceMappingURL=numeric-quantity.mjs.map