UNPKG

typia

Version:

Superfast runtime validators with only one line

1 lines 1.88 kB
{"version":3,"file":"_randomPattern.mjs","names":[],"sources":["../../src/internal/_randomPattern.ts"],"sourcesContent":["import RandExp from \"randexp\";\n\nimport { _ILengthProps } from \"./_randomStringLength\";\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 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 (\n regex.test(str) &&\n (min === undefined || str.length >= min) &&\n (max === undefined || str.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":";;AAIA,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;CAE7D,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,IACE,MAAM,KAAK,GAAG,MACb,QAAQ,KAAA,KAAa,IAAI,UAAU,SACnC,QAAQ,KAAA,KAAa,IAAI,UAAU,MAEpC,OAAO;CACX;CACA,MAAM,IAAI,MACR,UACI,qFACA,0DACN;AACF"}