typia
Version:
Superfast runtime validators with only one line
1 lines • 2.12 kB
Source Map (JSON)
{"version":3,"file":"_randomFormatUrl.mjs","names":[],"sources":["../../src/internal/_randomFormatUrl.ts"],"sourcesContent":["import { _randomInteger } from \"./_randomInteger\";\nimport { _randomString } from \"./_randomString\";\nimport { _ILengthProps } from \"./_randomStringLength\";\n\nexport const _randomFormatUrl = (props?: _ILengthProps): string => {\n if (props?.minLength === undefined && props?.maxLength === undefined)\n return `https://${random(10)}.${random(3)}`;\n // `https://a.bc` is the shortest host this builder emits (12 chars); any extra\n // length rides in the path segment, which the URL grammar leaves unbounded.\n const base: string = `https://${random(1)}.${random(2)}`;\n const target: number = pickLength(props, base.length);\n return target <= base.length\n ? base\n : `${base}/${random(target - base.length - 1)}`;\n};\n\nconst pickLength = (props: _ILengthProps, base: number): number => {\n let low: number = props.minLength === undefined ? base : props.minLength;\n if (low < base) low = base;\n const high: number =\n props.maxLength === undefined ? low + 14 : props.maxLength;\n if (high < low)\n throw new Error(\n \"unable to generate a random URL satisfying both the format and the length constraints.\",\n );\n return _randomInteger({ type: \"integer\", minimum: low, maximum: high });\n};\n\nconst random = (length: number) =>\n _randomString({\n type: \"string\",\n minLength: length,\n maxLength: length,\n });\n"],"mappings":";;;AAIA,MAAa,oBAAoB,UAAkC;CACjE,IAAI,OAAO,cAAc,KAAA,KAAa,OAAO,cAAc,KAAA,GACzD,OAAO,WAAW,OAAO,EAAE,EAAE,GAAG,OAAO,CAAC;CAG1C,MAAM,OAAe,WAAW,OAAO,CAAC,EAAE,GAAG,OAAO,CAAC;CACrD,MAAM,SAAiB,WAAW,OAAO,KAAK,MAAM;CACpD,OAAO,UAAU,KAAK,SAClB,OACA,GAAG,KAAK,GAAG,OAAO,SAAS,KAAK,SAAS,CAAC;AAChD;AAEA,MAAM,cAAc,OAAsB,SAAyB;CACjE,IAAI,MAAc,MAAM,cAAc,KAAA,IAAY,OAAO,MAAM;CAC/D,IAAI,MAAM,MAAM,MAAM;CACtB,MAAM,OACJ,MAAM,cAAc,KAAA,IAAY,MAAM,KAAK,MAAM;CACnD,IAAI,OAAO,KACT,MAAM,IAAI,MACR,wFACF;CACF,OAAO,eAAe;EAAE,MAAM;EAAW,SAAS;EAAK,SAAS;CAAK,CAAC;AACxE;AAEA,MAAM,UAAU,WACd,cAAc;CACZ,MAAM;CACN,WAAW;CACX,WAAW;AACb,CAAC"}