{"version":3,"file":"_randomPattern.mjs","names":[],"sources":["../../src/internal/_randomPattern.ts"],"sourcesContent":["import RandExp from \"randexp\";\n\nimport { _ILengthProps } from \"./_randomStringLength\";\nimport { _stringLength } from \"./_stringLength\";\n\n/**\n * Generate a string that matches a regular expression and the length bounds.\n *\n * Candidates are drawn until one matches and has a length in characters within\n * the bounds; the draw gives up with an error after a fixed number of\n * attempts.\n *\n * @evidence contracts/common.md#principled-implementation A generator for the pattern draws candidates and keeps one that matches the expression and whose length in code points is within the bounds, with a maximum repeat steered toward the window, because the library offers no floor; it gives up after 1,024 draws for a bounded request and ten for an unbounded one and throws. Length is counted in characters, which is what the length tags compare.\n * @evidence contracts/common.md#clear-and-simple-design One function with the verification loop.\n * @evidence contracts/common.md#prohibited-implementation-shortcuts The retry is bounded and the failure is reported; the code-point count is the tag's unit, as the comment explains.\n * @evidence contracts/common.md#meaningful-documentation The doc states the retry limits and the length unit, and a long inline comment explains the astral character case.\n */\nexport const _randomPattern = (\n  regex: RegExp,\n  props?: _ILengthProps,\n): string => {\n  const r: RandExp = new RandExp(regex);\n  const min: number | undefined = props?.minLength;\n  const max: number | undefined = props?.maxLength;\n\n  // RandExp expands an unbounded quantifier to `randInt(token.min, token.min +\n  // r.max)`, so steering `r.max` biases the generated length toward the window.\n  // There is no matching floor control, so the length is still verified below.\n  if (max !== undefined) r.max = max;\n  else if (min !== undefined && min * 2 > r.max) r.max = min * 2;\n\n  // The verification counts characters, not UTF-16 code units, because that is\n  // what `MinLength` and `MaxLength` compare against. `Pattern` compiles to a\n  // flagless `RegExp`, so a quantifier binds one code unit wherever its atom is\n  // a bare astral character: under `^😀+$` the `+` repeats the trailing low\n  // surrogate alone, and RandExp draws `\\ud83d` + k * `\\ude00` -- k characters\n  // in k + 1 code units. Measuring in code units accepted a two-character draw\n  // for `MinLength<3>` and handed back a value the generated validator rejects.\n  // Group the atom (`^(?:😀)+$`) and the arithmetic changes to 2k units for k\n  // characters; the two counts still differ, which is the point.\n  //\n  // The count is taken only after the pattern matches, and only when a bound\n  // exists, so an unbounded draw still walks nothing.\n  const bounded: boolean = min !== undefined || max !== undefined;\n  const limit: number = bounded ? 1024 : 10;\n  for (let i: number = 0; i < limit; ++i) {\n    const str: string = r.gen();\n    if (regex.test(str) === false) continue;\n    if (bounded === false) return str;\n    const length: number = _stringLength(str);\n    if (\n      (min === undefined || length >= min) &&\n      (max === undefined || length <= max)\n    )\n      return str;\n  }\n  throw new Error(\n    bounded\n      ? \"unable to generate a random string matching the pattern within the length range.\"\n      : \"unable to generate a random string matching the pattern.\",\n  );\n};\n"],"mappings":";;;;;;;;;;;;;;;AAiBA,MAAa,kBACX,OACA,UACW;CACX,MAAM,IAAa,IAAI,QAAQ,KAAK;CACpC,MAAM,MAA0B,OAAO;CACvC,MAAM,MAA0B,OAAO;CAKvC,IAAI,QAAQ,KAAA,GAAW,EAAE,MAAM;MAC1B,IAAI,QAAQ,KAAA,KAAa,MAAM,IAAI,EAAE,KAAK,EAAE,MAAM,MAAM;CAc7D,MAAM,UAAmB,QAAQ,KAAA,KAAa,QAAQ,KAAA;CACtD,MAAM,QAAgB,UAAU,OAAO;CACvC,KAAK,IAAI,IAAY,GAAG,IAAI,OAAO,EAAE,GAAG;EACtC,MAAM,MAAc,EAAE,IAAI;EAC1B,IAAI,MAAM,KAAK,GAAG,MAAM,OAAO;EAC/B,IAAI,YAAY,OAAO,OAAO;EAC9B,MAAM,SAAiB,cAAc,GAAG;EACxC,KACG,QAAQ,KAAA,KAAa,UAAU,SAC/B,QAAQ,KAAA,KAAa,UAAU,MAEhC,OAAO;CACX;CACA,MAAM,IAAI,MACR,UACI,qFACA,0DACN;AACF"}