{"version":3,"file":"__randomComposition.mjs","names":[],"sources":["../../../src/internal/private/__randomComposition.ts"],"sourcesContent":["import { _randomInteger } from \"../_randomInteger\";\n\n/**\n * Splits `total` into `count` parts, each inside `[minimum, maximum]`.\n *\n * Used where a format's length is the sum of its segments — an IPv4 octet spans\n * one to three digits, an IPv6 group one to four — so a requested total length\n * is realized by choosing how many characters each segment contributes instead\n * of redrawing whole addresses and hoping one lands in the window (issue\n * #2284).\n *\n * The caller guarantees `count * minimum <= total <= count * maximum`; each\n * part is drawn from the range that still leaves the remaining parts\n * satisfiable. Counts, lengths and segment bounds are nonnegative integers.\n *\n * @evidence contracts/common.md#principled-implementation A total is split into parts within a range by drawing each part from the interval that still leaves the remaining parts satisfiable, so the caller's guarantee that the total is within `count * minimum` and `count * maximum` yields a valid composition without retrying.\n * @evidence contracts/common.md#clear-and-simple-design One function.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The method is constructive and not a redraw loop.\n * @evidence contracts/common.md#meaningful-documentation The doc states the guarantee and the use for formats.\n */\nexport const __randomComposition = (props: {\n  total: number;\n  count: number;\n  minimum: number;\n  maximum: number;\n}): number[] => {\n  const parts: number[] = [];\n  let remaining: number = props.total;\n  for (let i: number = 0; i < props.count; ++i) {\n    const rest: number = props.count - i - 1;\n    const value: number = _randomInteger({\n      type: \"integer\",\n      minimum: Math.max(props.minimum, remaining - rest * props.maximum),\n      maximum: Math.min(props.maximum, remaining - rest * props.minimum),\n    });\n    parts.push(value);\n    remaining -= value;\n  }\n  return parts;\n};\n\n/**\n * Draws `length` characters of the base64 alphabet.\n *\n * The caller passes a multiple of four, which `format: \"byte\"` accepts without\n * padding; every character below is in the alphabet its validator spells out,\n * so the result needs no encoding step.\n *\n * @evidence contracts/common.md#principled-implementation Each character is drawn from the 64-symbol alphabet, which is the alphabet the byte validator accepts, so a multiple-of-four length is valid without an encoder.\n * @evidence contracts/common.md#clear-and-simple-design One loop.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The alphabet is the format's.\n * @evidence contracts/common.md#meaningful-documentation The doc states the multiple-of-four precondition.\n */\nexport const __randomBase64 = (length: number): string => {\n  let text: string = \"\";\n  for (let i: number = 0; i < length; ++i)\n    text +=\n      BASE64[\n        _randomInteger({\n          type: \"integer\",\n          minimum: 0,\n          maximum: BASE64.length - 1,\n        })\n      ];\n  return text;\n};\n\nconst BASE64 =\n  \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n\n/**\n * Draws `length` decimal digits, leading zeros included.\n *\n * @evidence contracts/common.md#principled-implementation Each character is a digit from zero to nine, leading zeros included.\n * @evidence contracts/common.md#clear-and-simple-design One loop.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts It uses the shared integer generator.\n * @evidence contracts/common.md#meaningful-documentation A one-line doc states the digits.\n */\nexport const __randomDigits = (length: number): string => {\n  let text: string = \"\";\n  for (let i: number = 0; i < length; ++i)\n    text += String(_randomInteger({ type: \"integer\", minimum: 0, maximum: 9 }));\n  return text;\n};\n\n/**\n * Draws a positive integer count of decimal digits; a single digit may be zero.\n *\n * @evidence contracts/common.md#principled-implementation A number of the requested digit count without a leading zero, with one digit allowed to be zero, built from a nonzero first digit and random digits.\n * @evidence contracts/common.md#clear-and-simple-design One expression over the digit helper.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts It uses the shared integer generator.\n * @evidence contracts/common.md#meaningful-documentation A one-line doc states the digit count.\n */\nexport const __randomNumeric = (length: number): string =>\n  length <= 1\n    ? String(_randomInteger({ type: \"integer\", minimum: 0, maximum: 9 }))\n    : String(_randomInteger({ type: \"integer\", minimum: 1, maximum: 9 })) +\n      __randomDigits(length - 1);\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,MAAa,uBAAuB,UAKpB;CACd,MAAM,QAAkB,CAAC;CACzB,IAAI,YAAoB,MAAM;CAC9B,KAAK,IAAI,IAAY,GAAG,IAAI,MAAM,OAAO,EAAE,GAAG;EAC5C,MAAM,OAAe,MAAM,QAAQ,IAAI;EACvC,MAAM,QAAgB,eAAe;GACnC,MAAM;GACN,SAAS,KAAK,IAAI,MAAM,SAAS,YAAY,OAAO,MAAM,OAAO;GACjE,SAAS,KAAK,IAAI,MAAM,SAAS,YAAY,OAAO,MAAM,OAAO;EACnE,CAAC;EACD,MAAM,KAAK,KAAK;EAChB,aAAa;CACf;CACA,OAAO;AACT;;;;;;;;;;;;;AAcA,MAAa,kBAAkB,WAA2B;CACxD,IAAI,OAAe;CACnB,KAAK,IAAI,IAAY,GAAG,IAAI,QAAQ,EAAE,GACpC,QACE,OACE,eAAe;EACb,MAAM;EACN,SAAS;EACT,SAAS,OAAO,SAAS;CAC3B,CAAC;CAEP,OAAO;AACT;AAEA,MAAM,SACJ;;;;;;;;;AAUF,MAAa,kBAAkB,WAA2B;CACxD,IAAI,OAAe;CACnB,KAAK,IAAI,IAAY,GAAG,IAAI,QAAQ,EAAE,GACpC,QAAQ,OAAO,eAAe;EAAE,MAAM;EAAW,SAAS;EAAG,SAAS;CAAE,CAAC,CAAC;CAC5E,OAAO;AACT;;;;;;;;;AAUA,MAAa,mBAAmB,WAC9B,UAAU,IACN,OAAO,eAAe;CAAE,MAAM;CAAW,SAAS;CAAG,SAAS;AAAE,CAAC,CAAC,IAClE,OAAO,eAAe;CAAE,MAAM;CAAW,SAAS;CAAG,SAAS;AAAE,CAAC,CAAC,IAClE,eAAe,SAAS,CAAC"}