UNPKG

typia

Version:

Superfast runtime validators with only one line

32 lines 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._randomFormatIdnHostname = void 0; const _randomFormatHostname_1 = require("./_randomFormatHostname"); const _randomInteger_1 = require("./_randomInteger"); const _randomString_1 = require("./_randomString"); const _randomFormatIdnHostname = (props) => { if ((props === null || props === void 0 ? void 0 : props.minLength) === undefined && (props === null || props === void 0 ? void 0 : props.maxLength) === undefined) return `${random(10)}.${random(3)}`; // `_isFormatIdnHostname` requires at least one label, a dot, and a >=2-char // TLD (`(…\.)+[a-z¡-￿]{2,}`), none of which the non-idn hostname generator // guarantees on its length path (it can emit a single dotless label). Reserve // a fixed `.<2-char TLD>` and realize the rest as dot-joined leading labels. const length = pickLength(props); return `${(0, _randomFormatHostname_1._randomHostnameLabels)(length - 3)}.${random(2)}`; }; exports._randomFormatIdnHostname = _randomFormatIdnHostname; const MAX_TOTAL = 253; const MIN_TOTAL = 4; // the shortest idn hostname, `x.yy`: label(1) + `.` + tld(2) const pickLength = (props) => { let low = props.minLength === undefined ? MIN_TOTAL : props.minLength; if (low < MIN_TOTAL) low = MIN_TOTAL; const high = props.maxLength === undefined ? Math.min(MAX_TOTAL, low + 14) : Math.min(MAX_TOTAL, props.maxLength); if (high < low) throw new Error("unable to generate a random idn hostname satisfying both the format and the length constraints."); return (0, _randomInteger_1._randomInteger)({ type: "integer", minimum: low, maximum: high }); }; const random = (length) => (0, _randomString_1._randomString)({ type: "string", minLength: length, maxLength: length }); //# sourceMappingURL=_randomFormatIdnHostname.js.map