typia
Version:
Superfast runtime validators with only one line
1 lines • 2.39 kB
Source Map (JSON)
{"version":3,"file":"_randomFormatIdnHostname.mjs","names":[],"sources":["../../src/internal/_randomFormatIdnHostname.ts"],"sourcesContent":["import { _randomHostnameLabels } from \"./_randomFormatHostname\";\nimport { _randomInteger } from \"./_randomInteger\";\nimport { _randomString } from \"./_randomString\";\nimport { _ILengthProps } from \"./_randomStringLength\";\n\nexport const _randomFormatIdnHostname = (props?: _ILengthProps): string => {\n if (props?.minLength === undefined && props?.maxLength === undefined)\n return `${random(10)}.${random(3)}`;\n // `_isFormatIdnHostname` requires at least one label, a dot, and a >=2-char\n // TLD (`(…\\.)+[a-z¡-]{2,}`), none of which the non-idn hostname generator\n // guarantees on its length path (it can emit a single dotless label). Reserve\n // a fixed `.<2-char TLD>` and realize the rest as dot-joined leading labels.\n const length: number = pickLength(props);\n return `${_randomHostnameLabels(length - 3)}.${random(2)}`;\n};\n\nconst MAX_TOTAL = 253;\nconst MIN_TOTAL = 4; // the shortest idn hostname, `x.yy`: label(1) + `.` + tld(2)\n\nconst pickLength = (props: _ILengthProps): number => {\n let low: number = props.minLength === undefined ? MIN_TOTAL : props.minLength;\n if (low < MIN_TOTAL) low = MIN_TOTAL;\n const high: number =\n props.maxLength === undefined\n ? Math.min(MAX_TOTAL, low + 14)\n : Math.min(MAX_TOTAL, props.maxLength);\n if (high < low)\n throw new Error(\n \"unable to generate a random idn hostname 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({ type: \"string\", minLength: length, maxLength: length });\n"],"mappings":";;;;AAKA,MAAa,4BAA4B,UAAkC;CACzE,IAAI,OAAO,cAAc,KAAA,KAAa,OAAO,cAAc,KAAA,GACzD,OAAO,GAAG,OAAO,EAAE,EAAE,GAAG,OAAO,CAAC;CAMlC,OAAO,GAAG,sBADa,WAAW,KACG,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC;AACzD;AAEA,MAAM,YAAY;AAClB,MAAM,YAAY;AAElB,MAAM,cAAc,UAAiC;CACnD,IAAI,MAAc,MAAM,cAAc,KAAA,IAAY,YAAY,MAAM;CACpE,IAAI,MAAM,WAAW,MAAM;CAC3B,MAAM,OACJ,MAAM,cAAc,KAAA,IAChB,KAAK,IAAI,WAAW,MAAM,EAAE,IAC5B,KAAK,IAAI,WAAW,MAAM,SAAS;CACzC,IAAI,OAAO,KACT,MAAM,IAAI,MACR,iGACF;CACF,OAAO,eAAe;EAAE,MAAM;EAAW,SAAS;EAAK,SAAS;CAAK,CAAC;AACxE;AAEA,MAAM,UAAU,WACd,cAAc;CAAE,MAAM;CAAU,WAAW;CAAQ,WAAW;AAAO,CAAC"}