{"version":3,"file":"withPrecision-DPE-pG3p.cjs","names":[],"sources":["../src/internal/withPrecision.ts"],"sourcesContent":["// ECMAScript doesn't support more decimal places than 15 anyways,\n// so supporting higher values doesn't make a lot of sense,\n// Considering Number.MAX_SAFE_INTEGER < 10**16, we can use -15\n// for the negative precision limit, too.\nconst MAX_PRECISION = 15;\n\nexport const withPrecision =\n  (roundingFn: (value: number) => number) =>\n  (value: number, precision: number): number => {\n    if (precision === 0) {\n      return roundingFn(value);\n    }\n\n    if (Math.abs(precision) > MAX_PRECISION) {\n      throw new RangeError(\"precision must be between -15 and 15\");\n    }\n\n    if (!Number.isSafeInteger(precision)) {\n      throw new TypeError(\n        `precision must be an integer: ${precision.toString()}`,\n      );\n    }\n\n    if (Number.isNaN(value) || !Number.isFinite(value)) {\n      return roundingFn(value);\n    }\n\n    const shiftedValue = shiftDecimalPoint(value, precision);\n    const rounded = roundingFn(shiftedValue);\n    return shiftDecimalPoint(rounded, -precision);\n  };\n\n/**\n * Shift a number's decimal point via scientific notation.\n *\n * This takes advantage of the fact that `Number` methods support scientific\n * (e-notation) string natively and avoids working with double-precision\n * floating-point numbers directly, working around their limitations\n * with representing decimal numbers.\n */\nfunction shiftDecimalPoint(value: number, shift: number): number {\n  const asString = value.toString();\n  const [n, exponent] = asString.split(\"e\", 2);\n\n  const shiftedExponent =\n    (exponent === undefined ? 0 : Number(exponent)) + shift;\n\n  const shiftedValueAsString = `${n!}e${shiftedExponent.toString()}`;\n\n  return Number(shiftedValueAsString);\n}\n"],"mappings":"AAIA,MAEa,EACV,IACA,EAAe,IAA8B,CAC5C,GAAI,IAAc,EAChB,OAAO,EAAW,CAAK,EAGzB,GAAI,KAAK,IAAI,CAAS,EAAI,GACxB,MAAU,WAAW,sCAAsC,EAG7D,GAAI,CAAC,OAAO,cAAc,CAAS,EACjC,MAAU,UACR,iCAAiC,EAAU,SAAS,GACtD,EASF,OANI,OAAO,MAAM,CAAK,GAAK,CAAC,OAAO,SAAS,CAAK,EACxC,EAAW,CAAK,EAKlB,EADS,EADK,EAAkB,EAAO,CACR,CACP,EAAG,CAAC,CAAS,CAC9C,EAUF,SAAS,EAAkB,EAAe,EAAuB,CAE/D,GAAM,CAAC,EAAG,GADO,EAAM,SACM,CAAC,CAAC,MAAM,IAAK,CAAC,EAKrC,EAAuB,GAAG,EAAG,KAFhC,IAAa,IAAA,GAAY,EAAI,OAAO,CAAQ,GAAK,EAAA,CAEE,SAAS,IAE/D,OAAO,OAAO,CAAoB,CACpC"}