UNPKG

typia

Version:

Superfast runtime validators with only one line

43 lines 1.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._randomFormatIpv4 = void 0; const _randomInteger_1 = require("./_randomInteger"); const _randomStringLength_1 = require("./_randomStringLength"); const __randomComposition_1 = require("./private/__randomComposition"); const _randomFormatIpv4 = (props) => { if ((props === null || props === void 0 ? void 0 : props.minLength) === undefined && (props === null || props === void 0 ? void 0 : props.maxLength) === undefined) return new Array(4).fill(0).map(random).join("."); // Three dots plus four octets of one to three digits: `0.0.0.0` is the // shortest address and `255.255.255.255` the longest, and every length // between them is reached by choosing how many digits each octet spends. const length = (0, _randomStringLength_1._randomLengthPick)((0, _randomStringLength_1._randomLengthWindow)(props, { minimum: 4 * DIGITS_MIN + DOTS, maximum: 4 * DIGITS_MAX + DOTS, spread: 8, })); return (0, __randomComposition_1.__randomComposition)({ total: length - DOTS, count: 4, minimum: DIGITS_MIN, maximum: DIGITS_MAX, }) .map(octet) .join("."); }; exports._randomFormatIpv4 = _randomFormatIpv4; const DOTS = 3; const DIGITS_MIN = 1; const DIGITS_MAX = 3; const random = () => (0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 255, }); // An octet is bounded by 255, so a three-digit one starts at 100 and stops // there rather than at 999. const octet = (digits) => String((0, _randomInteger_1._randomInteger)({ type: "integer", minimum: digits === 1 ? 0 : Math.pow(10, digits - 1), maximum: digits === 3 ? 255 : Math.pow(10, digits) - 1, })); //# sourceMappingURL=_randomFormatIpv4.js.map