typia
Version:
Superfast runtime validators with only one line
30 lines (29 loc) • 1.17 kB
JavaScript
import { _randomInteger } from "./_randomInteger.mjs";
import { _randomString } from "./_randomString.mjs";
import { _randomHostnameLabels } from "./_randomFormatHostname.mjs";
//#region src/internal/_randomFormatIdnHostname.ts
const _randomFormatIdnHostname = (props) => {
if (props?.minLength === void 0 && props?.maxLength === void 0) return `${random(10)}.${random(3)}`;
return `${_randomHostnameLabels(pickLength(props) - 3)}.${random(2)}`;
};
const MAX_TOTAL = 253;
const MIN_TOTAL = 4;
const pickLength = (props) => {
let low = props.minLength === void 0 ? MIN_TOTAL : props.minLength;
if (low < MIN_TOTAL) low = MIN_TOTAL;
const high = props.maxLength === void 0 ? 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 _randomInteger({
type: "integer",
minimum: low,
maximum: high
});
};
const random = (length) => _randomString({
type: "string",
minLength: length,
maxLength: length
});
//#endregion
export { _randomFormatIdnHostname };
//# sourceMappingURL=_randomFormatIdnHostname.mjs.map