{"version":3,"file":"_randomStringLength.mjs","names":[],"sources":["../../src/internal/_randomStringLength.ts"],"sourcesContent":["import { _randomInteger } from \"./_randomInteger\";\nimport { _randomString } from \"./_randomString\";\nimport { _stringLength } from \"./_stringLength\";\n\nexport const _RANDOM_LENGTH_ERROR =\n  \"unable to generate a random string satisfying both the format and the length constraints.\";\n\n/**\n * Length bounds forwarded to a string format or pattern generator.\n *\n * `typia.random<T>()` passes this object to the `_randomFormat*` and\n * `_randomPattern` helpers whenever the string leaf also carries a `MinLength`\n * or `MaxLength` tag, so a constrained format or pattern is generated at a\n * length its own validator accepts (issue #2189).\n *\n * @evidence contracts/common.md#principled-implementation Two optional numbers carry the minimum and maximum length of a string leaf to a format or pattern generator.\n * @evidence contracts/common.md#clear-and-simple-design Two fields.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts A data record.\n * @evidence contracts/common.md#meaningful-documentation The comment says when the generated code passes it.\n */\nexport interface _ILengthProps {\n  /** Inclusive minimum string length in code points, when constrained. */\n  minLength?: number;\n\n  /** Inclusive maximum string length in code points, when constrained. */\n  maxLength?: number;\n}\n\n/**\n * Builds the variable segment of a fixed-shape format string so that the total\n * length `fixed + segment.length` lands inside the requested window.\n *\n * `fixed` is the number of characters the format always contributes around the\n * segment, `fallback` is the segment length used when the leaf is unconstrained\n * on that side, and `minimum` is the smallest segment the format still\n * validates with. Throws when the window cannot be satisfied by this format's\n * shape (e.g. `Format<\"email\"> & MaxLength<3>`).\n *\n * @evidence contracts/common.md#principled-implementation A variable segment is drawn so that a fixed overhead plus the segment fits the requested window, with a floor for the shortest valid segment, and a window the shape cannot meet throws.\n * @evidence contracts/common.md#clear-and-simple-design One function over the string generator.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts Failure is explicit.\n * @evidence contracts/common.md#meaningful-documentation The doc defines fixed, fallback and minimum.\n */\nexport const _randomSegmentLength = (\n  props: _ILengthProps | undefined,\n  fixed: number,\n  fallback: number,\n  minimum: number,\n): string => {\n  let low: number =\n    props?.minLength === undefined ? minimum : props.minLength - fixed;\n  if (low < minimum) low = minimum;\n  // With only a lower bound, spread the segment across `fallback` extra\n  // characters so the generated length varies, mirroring `_randomString`.\n  const high: number =\n    props?.maxLength === undefined ? low + fallback : props.maxLength - fixed;\n  if (high < low || high < minimum) throw new Error(_RANDOM_LENGTH_ERROR);\n  return _randomString({ type: \"string\", minLength: low, maxLength: high });\n};\n\n/**\n * Intersects the requested length window with the lengths a format's grammar\n * can express, and draws one of them.\n *\n * `minimum` and `maximum` are the shortest and longest values the format admits\n * — omit `maximum` when the grammar is open above — and `spread` is how far\n * past the floor an unbounded request may reach, so an open side still varies\n * the way `_randomString` does. The caller draws from the returned window and\n * applies whatever step its grammar has (base64 lengths are multiples of four,\n * a fractional second needs at least one digit).\n *\n * Throws only when the window and the grammar do not overlap at all. That is\n * the distinction the retry driver below cannot make: it redraws one shape, so\n * it gives up on every length that shape never emits even when the format\n * itself accepts one (issue #2284).\n *\n * @evidence contracts/common.md#principled-implementation The requested bounds are intersected with the lengths the grammar admits, with a spread for an open side, and an empty intersection throws, which is the one case where no length can satisfy both.\n * @evidence contracts/common.md#clear-and-simple-design One function that returns a window and leaves the step to the caller.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The window is arithmetic and is not a retry.\n * @evidence contracts/common.md#meaningful-documentation The doc explains the window, the spread and the issue.\n */\nexport const _randomLengthWindow = (\n  props: _ILengthProps | undefined,\n  format: { minimum: number; maximum?: number; spread?: number },\n): { low: number; high: number } => {\n  const ceiling: number = format.maximum ?? Number.MAX_SAFE_INTEGER;\n  const low: number = Math.max(\n    props?.minLength ?? format.minimum,\n    format.minimum,\n  );\n  const high: number = Math.min(\n    props?.maxLength ?? low + (format.spread ?? 0),\n    ceiling,\n  );\n  if (low > high) throw new Error(_RANDOM_LENGTH_ERROR);\n  return { low, high };\n};\n\n/**\n * Draws a length inside a window produced by {@link _randomLengthWindow}.\n *\n * @evidence contracts/common.md#principled-implementation A length is drawn uniformly inside the window through the integer generator.\n * @evidence contracts/common.md#clear-and-simple-design One function.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts It uses the shared generator.\n * @evidence contracts/common.md#meaningful-documentation The one-line doc states the use.\n */\nexport const _randomLengthPick = (window: {\n  low: number;\n  high: number;\n}): number =>\n  _randomInteger({\n    type: \"integer\",\n    minimum: window.low,\n    maximum: window.high,\n  });\n\n/**\n * Wraps a fixed-length format generator (uuid, date) with the requested length\n * window.\n *\n * Use it only where the grammar admits exactly one length, so a window that\n * excludes it is genuinely unsatisfiable. A format that can express more than\n * one length must build its value at a length drawn from\n * {@link _randomLengthWindow} instead; redrawing one shape cannot reach a length\n * that shape never emits.\n *\n * @evidence contracts/common.md#principled-implementation A fixed-length format is generated and accepted only when its length in characters fits the window, up to 256 draws, and otherwise throws, which is correct for a grammar with exactly one length and wrong for formats with several, as the doc says.\n * @evidence contracts/common.md#clear-and-simple-design One function.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts Characters, not code units, are counted to stay consistent with the length tags.\n * @evidence contracts/common.md#meaningful-documentation The doc states when to use it and when not to.\n */\nexport const _randomFormatLength = (\n  props: _ILengthProps | undefined,\n  generate: () => string,\n): string => {\n  if (props?.minLength === undefined && props?.maxLength === undefined)\n    return generate();\n  for (let i: number = 0; i < 256; ++i) {\n    const value: string = generate();\n    // Characters, not UTF-16 code units: `minLength` and `maxLength` are the\n    // bounds the generated validator compares in characters. `uuid` and `date`,\n    // the only two shapes routed through here, are ASCII, so the two counts\n    // agree for them today -- this keeps one measure so a later non-ASCII\n    // format cannot reintroduce the divergence unnoticed.\n    const length: number = _stringLength(value);\n    if (\n      (props.minLength === undefined || length >= props.minLength) &&\n      (props.maxLength === undefined || length <= props.maxLength)\n    )\n      return value;\n  }\n  throw new Error(_RANDOM_LENGTH_ERROR);\n};\n"],"mappings":";;;;AAIA,MAAa,uBACX;;;;;;;;;;;;;;;;AAsCF,MAAa,wBACX,OACA,OACA,UACA,YACW;CACX,IAAI,MACF,OAAO,cAAc,KAAA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