typia
Version:
Superfast runtime validators with only one line
32 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._randomFormatUrl = void 0;
const _randomInteger_1 = require("./_randomInteger");
const _randomString_1 = require("./_randomString");
const _randomFormatUrl = (props) => {
if ((props === null || props === void 0 ? void 0 : props.minLength) === undefined && (props === null || props === void 0 ? void 0 : props.maxLength) === undefined)
return `https://${random(10)}.${random(3)}`;
// `https://a.bc` is the shortest host this builder emits (12 chars); any extra
// length rides in the path segment, which the URL grammar leaves unbounded.
const base = `https://${random(1)}.${random(2)}`;
const target = pickLength(props, base.length);
return target <= base.length
? base
: `${base}/${random(target - base.length - 1)}`;
};
exports._randomFormatUrl = _randomFormatUrl;
const pickLength = (props, base) => {
let low = props.minLength === undefined ? base : props.minLength;
if (low < base)
low = base;
const high = props.maxLength === undefined ? low + 14 : props.maxLength;
if (high < low)
throw new Error("unable to generate a random URL 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=_randomFormatUrl.js.map