{"version":3,"file":"_randomBigint.mjs","names":[],"sources":["../../src/internal/_randomBigint.ts"],"sourcesContent":["import { OpenApi } from \"@typia/interface\";\n\nimport { _decimalIntegerStep } from \"./_decimal\";\n\n/**\n * Generates a random `bigint` satisfying an integer schema.\n *\n * The schema carries its bounds and step as numbers, but the value is a\n * `bigint`, so the draw runs on integers. Drawing through doubles rounded every\n * value past 2^53, and it checked `multipleOf` against a double's shortest\n * decimal text rather than the integer `BigInt` makes of it, so `random`\n * returned values the validator rejects, or gave up on a range that holds one\n * (#2457). An integral bound or step is read as the integer the double holds,\n * which is the integer the validator compares with.\n *\n * @evidence contracts/common.md#principled-implementation Bounds and step are read as the integers their doubles hold, an exclusive bound is moved to the next inclusive integer, and the draw is made on bigints so no precision is lost past 2^53; a multiple of the step is chosen by dividing the range by the step, drawing a quotient and multiplying back. The draw scales one 53-bit sample, so a range wider than 2^53 values is sampled at most that many distinct points.\n * @evidence contracts/common.md#clear-and-simple-design One function with small private helpers for the bound, the step, the rounding divisions and the draw.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts Errors for empty or infinite ranges are explicit, and the step is the integer the validator compares with.\n * @evidence contracts/common.md#meaningful-documentation The doc explains why the draw is not done through doubles and cites the issue.\n */\nexport const _randomBigint = (schema: OpenApi.IJsonSchema.IInteger): bigint => {\n  const lower: bigint | null = boundary(\n    schema.minimum,\n    schema.exclusiveMinimum,\n    \"lower\",\n  );\n  const upper: bigint | null = boundary(\n    schema.maximum,\n    schema.exclusiveMaximum,\n    \"upper\",\n  );\n  const minimum: bigint =\n    lower ?? (upper === null ? BigInt(0) : upper - BigInt(100));\n  const maximum: bigint =\n    upper ?? (lower === null ? BigInt(100) : lower + BigInt(100));\n  if (minimum > maximum)\n    throw new Error(\"Minimum value is greater than maximum value.\");\n  if (schema.multipleOf === undefined) return between(minimum, maximum);\n\n  const step: bigint | null = integerStep(schema.multipleOf);\n  if (step === null || step <= BigInt(0))\n    throw new Error(\"The multipleOf value must be a positive finite number.\");\n  const first: bigint = ceilDivide(minimum, step);\n  const last: bigint = floorDivide(maximum, step);\n  if (first > last)\n    throw new Error(\"The range does not contain a multipleOf value.\");\n  return between(first, last) * step;\n};\n\n/**\n * Reads one side's inclusive integer bound.\n *\n * `-Infinity` below or `Infinity` above excludes no integer, so it is no bound\n * at all; `Infinity` below or `-Infinity` above excludes every integer.\n */\nconst boundary = (\n  inclusive: number | undefined,\n  exclusive: number | undefined,\n  side: \"lower\" | \"upper\",\n): bigint | null => {\n  const candidates: bigint[] = [];\n  for (const [value, open] of [\n    [inclusive, false],\n    [exclusive, true],\n  ] as const) {\n    if (value === undefined) continue;\n    if (Number.isFinite(value) === false) {\n      if (value === (side === \"lower\" ? -Infinity : Infinity)) continue;\n      throw new Error(\"Numeric range has no finite value.\");\n    }\n    candidates.push(\n      side === \"lower\"\n        ? open\n          ? BigInt(Math.floor(value)) + BigInt(1)\n          : BigInt(Math.ceil(value))\n        : open\n          ? BigInt(Math.ceil(value)) - BigInt(1)\n          : BigInt(Math.floor(value)),\n    );\n  }\n  if (candidates.length === 0) return null;\n  return candidates.reduce((x, y) =>\n    side === \"lower\" ? (x > y ? x : y) : x < y ? x : y,\n  );\n};\n\n/**\n * The smallest positive integer a `multipleOf` divides.\n *\n * An integral step is the integer its double holds; a fractional one, such as\n * `1.5`, steps by its smallest integer multiple (`3`).\n */\nconst integerStep = (multipleOf: number): bigint | null => {\n  if (Number.isFinite(multipleOf) === false) return null;\n  if (Number.isInteger(multipleOf)) return BigInt(multipleOf);\n  return _decimalIntegerStep(multipleOf)?.coefficient ?? null;\n};\n\nconst ceilDivide = (x: bigint, y: bigint): bigint => {\n  const quotient: bigint = x / y;\n  return x % y > BigInt(0) ? quotient + BigInt(1) : quotient;\n};\n\nconst floorDivide = (x: bigint, y: bigint): bigint => {\n  const quotient: bigint = x / y;\n  return x % y < BigInt(0) ? quotient - BigInt(1) : quotient;\n};\n\n/** Draws an integer in `[minimum, maximum]`, scaling one 53-bit sample. */\nconst between = (minimum: bigint, maximum: bigint): bigint => {\n  const scale: bigint = BigInt(1) << BigInt(53);\n  const sample: bigint = BigInt(\n    Math.min(\n      Number(scale - BigInt(1)),\n      Math.floor(Math.max(0, Math.random()) * Number(scale)),\n    ),\n  );\n  return minimum + ((maximum - minimum + BigInt(1)) * sample) / scale;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoBA,MAAa,iBAAiB,WAAiD;CAC7E,MAAM,QAAuB,SAC3B,OAAO,SACP,OAAO,kBACP,OACF;CACA,MAAM,QAAuB,SAC3B,OAAO,SACP,OAAO,kBACP,OACF;CACA,MAAM,UACJ,UAAU,UAAU,OAAO,OAAO,CAAC,IAAI,QAAQ,OAAO,GAAG;CAC3D,MAAM,UACJ,UAAU,UAAU,OAAO,OAAO,GAAG,IAAI,QAAQ,OAAO,GAAG;CAC7D,IAAI,UAAU,SACZ,MAAM,IAAI,MAAM,8CAA8C;CAChE,IAAI,OAAO,eAAe,KAAA,GAAW,OAAO,QAAQ,SAAS,OAAO;CAEpE,MAAM,OAAsB,YAAY,OAAO,UAAU;CACzD,IAAI,SAAS,QAAQ,QAAQ,OAAO,CAAC,GACnC,MAAM,IAAI,MAAM,wDAAwD;CAC1E,MAAM,QAAgB,WAAW,SAAS,IAAI;CAC9C,MAAM,OAAe,YAAY,SAAS,IAAI;CAC9C,IAAI,QAAQ,MACV,MAAM,IAAI,MAAM,gDAAgD;CAClE,OAAO,QAAQ,OAAO,IAAI,IAAI;AAChC;;;;;;;AAQA,MAAM,YACJ,WACA,WACA,SACkB;CAClB,MAAM,aAAuB,CAAC;CAC9B,KAAK,MAAM,CAAC,OAAO,SAAS,CAC1B,CAAC,WAAW,KAAK,GACjB,CAAC,WAAW,IAAI,CAClB,GAAY;EACV,IAAI,UAAU,KAAA,GAAW;EACzB,IAAI,OAAO,SAAS,KAAK,MAAM,OAAO;GACpC,IAAI,WAAW,SAAS,UAAU,YAAY,WAAW;GACzD,MAAM,IAAI,MAAM,oCAAoC;EACtD;EACA,WA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