{"version":3,"file":"numeric-utils.mjs","names":[],"sources":["../../../../src/_generated/style-rules/numeric-utils.ts"],"sourcesContent":["/*\n * AUTO-GENERATED FILE — DO NOT EDIT.\n * Source of truth: packages/style-rules/src/\n * Regenerate with `pnpm generate:shared` (runs automatically at build/dev/install).\n */\n\n/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\n\nimport type { BigintLiteral, NumericLiteral } from './literal-types';\n\nexport type NumericValue = number | bigint;\n\ntype Rational = {\n  denominator: bigint;\n  numerator: bigint;\n};\n\nconst BIGINT_ZERO = BigInt(0);\nconst BIGINT_ONE = BigInt(1);\nconst BIGINT_TWO = BigInt(2);\nconst BIGINT_RATIO_SCALE = BigInt('1000000000000000000');\n\nfunction bigintPowerOfTen(exponent: number): bigint {\n  return BigInt(`1${'0'.repeat(exponent)}`);\n}\n\n/** Resolves a native or JSON-safe numeric literal without reducing bigints. */\nexport function resolveNumericLiteral(value: NumericLiteral): NumericValue {\n  return typeof value === 'object' ? BigInt(value.bigint) : value;\n}\n\n/** Returns whether a value is the JSON-safe bigint literal shape. */\nexport function isBigintLiteral(value: unknown): value is BigintLiteral {\n  return (\n    typeof value === 'object' &&\n    value !== null &&\n    'bigint' in value &&\n    typeof value.bigint === 'string' &&\n    /^[+-]?\\d+$/.test(value.bigint)\n  );\n}\n\n/**\n * Compares numeric values without coercing bigint operands to number.\n * Returns null for NaN, otherwise -1, 0, or 1.\n */\nexport function compareNumericValues(\n  left: NumericValue,\n  right: NumericValue,\n): -1 | 0 | 1 | null {\n  if (\n    (typeof left === 'number' && Number.isNaN(left)) ||\n    (typeof right === 'number' && Number.isNaN(right))\n  ) {\n    return null;\n  }\n  if (left < right) {\n    return -1;\n  }\n  if (left > right) {\n    return 1;\n  }\n  return 0;\n}\n\n/** Numeric equality that treats equivalent number and bigint values as equal. */\nexport function numericValuesEqual(\n  left: NumericValue,\n  right: NumericValue,\n): boolean {\n  return compareNumericValues(left, right) === 0;\n}\n\n/**\n * Clamps a numeric value between two endpoints. Inverted endpoints are\n * supported, and the original number/bigint representation is retained.\n */\nexport function clampNumericValue(\n  value: NumericValue,\n  firstBound: NumericValue,\n  secondBound: NumericValue,\n): NumericValue | null {\n  const boundsOrder = compareNumericValues(firstBound, secondBound);\n  if (boundsOrder === null) {\n    return null;\n  }\n  const min = boundsOrder <= 0 ? firstBound : secondBound;\n  const max = boundsOrder <= 0 ? secondBound : firstBound;\n  const minOrder = compareNumericValues(value, min);\n  const maxOrder = compareNumericValues(value, max);\n  if (minOrder === null || maxOrder === null) {\n    return null;\n  }\n  if (minOrder < 0) {\n    return min;\n  }\n  if (maxOrder > 0) {\n    return max;\n  }\n  return value;\n}\n\nfunction numberToRational(value: number): Rational | null {\n  if (!Number.isFinite(value)) {\n    return null;\n  }\n\n  const match = /^([+-]?)(\\d+)(?:\\.(\\d+))?(?:e([+-]?\\d+))?$/i.exec(\n    value.toString(),\n  );\n  if (!match) {\n    return null;\n  }\n\n  const [, sign, integer, fraction = '', exponentText = '0'] = match;\n  const exponent = Number(exponentText) - fraction.length;\n  const digits = BigInt(`${sign}${integer}${fraction}`);\n  if (exponent >= 0) {\n    return {\n      denominator: BIGINT_ONE,\n      numerator: digits * bigintPowerOfTen(exponent),\n    };\n  }\n  return {\n    denominator: bigintPowerOfTen(-exponent),\n    numerator: digits,\n  };\n}\n\nfunction toRational(value: NumericValue): Rational | null {\n  return typeof value === 'bigint'\n    ? { denominator: BIGINT_ONE, numerator: value }\n    : numberToRational(value);\n}\n\nfunction divideBigints(numerator: bigint, denominator: bigint): number | null {\n  if (denominator === BIGINT_ZERO) {\n    return null;\n  }\n\n  const isNegative = numerator < BIGINT_ZERO !== denominator < BIGINT_ZERO;\n  const absoluteNumerator = numerator < BIGINT_ZERO ? -numerator : numerator;\n  const absoluteDenominator =\n    denominator < BIGINT_ZERO ? -denominator : denominator;\n  const integer = absoluteNumerator / absoluteDenominator;\n  const remainder = absoluteNumerator % absoluteDenominator;\n  const fraction =\n    (remainder * BIGINT_RATIO_SCALE + absoluteDenominator / BIGINT_TWO) /\n    absoluteDenominator;\n  const result =\n    Number(integer) + Number(fraction) / Number(BIGINT_RATIO_SCALE);\n\n  return isNegative ? -result : result;\n}\n\n/**\n * Computes `(value - startValue) / (endValue - startValue)` while retaining\n * bigint precision. Only the final dimensionless interpolation weight is\n * converted to number.\n */\nexport function getNumericWeight(\n  startValue: NumericValue,\n  endValue: NumericValue,\n  value: NumericValue,\n): number | null {\n  if (\n    typeof startValue === 'number' &&\n    typeof endValue === 'number' &&\n    typeof value === 'number'\n  ) {\n    const weight = (value - startValue) / (endValue - startValue);\n    return Number.isNaN(weight) ? null : weight;\n  }\n\n  const start = toRational(startValue);\n  const end = toRational(endValue);\n  const current = toRational(value);\n  if (start === null || end === null || current === null) {\n    return null;\n  }\n\n  const valueDelta =\n    current.numerator * start.denominator -\n    start.numerator * current.denominator;\n  const rangeDelta =\n    end.numerator * start.denominator - start.numerator * end.denominator;\n\n  return divideBigints(\n    valueDelta * end.denominator,\n    current.denominator * rangeDelta,\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoCA,MAAM,cAAc,OAAO,CAAC;AAC5B,MAAM,aAAa,OAAO,CAAC;AAC3B,MAAM,aAAa,OAAO,CAAC;AAC3B,MAAM,qBAAqB,OAAO,qBAAqB;AAEvD,SAAS,iBAAiB,UAA0B;CAClD,OAAO,OAAO,IAAI,IAAI,OAAO,QAAQ,G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