UNPKG

typia

Version:

Superfast runtime validators with only one line

69 lines 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._randomFormatIpv6 = void 0; const _randomFormatIpv4_1 = require("./_randomFormatIpv4"); const _randomInteger_1 = require("./_randomInteger"); const _randomStringLength_1 = require("./_randomStringLength"); const __randomComposition_1 = require("./private/__randomComposition"); const _randomFormatIpv6 = (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(GROUPS).fill(0).map(random).join(":"); const length = (0, _randomStringLength_1._randomLengthPick)((0, _randomStringLength_1._randomLengthWindow)(props, { minimum: SHORTEST, maximum: LONGEST, spread: 12, })); // `::` alone is the all-zero address, and the three forms below cover every // length between it and the padded IPv4-suffixed address: a compressed prefix // for the short ones, eight groups of one to four hexadecimal digits for the // middle, and six groups plus a dotted-quad tail for the long ones. if (length === SHORTEST) return "::"; if (length < GROUPS * DIGITS_MIN + COLONS) { // `::` plus `count` groups spends one colon between each pair. const count = Math.ceil((length - 1) / (DIGITS_MAX + 1)); return `::${compose(length - 1 - count, count)}`; } if (length <= GROUPS * DIGITS_MAX + COLONS) return compose(length - COLONS, GROUPS); // The dotted-quad tail is drawn at its longest so the six leading groups keep // a composable number of digits. const tail = (0, _randomFormatIpv4_1._randomFormatIpv4)({ minLength: IPV4_MAX, maxLength: IPV4_MAX, }); return `${compose(length - IPV4_MAX - EMBEDDED, EMBEDDED)}:${tail}`; }; exports._randomFormatIpv6 = _randomFormatIpv6; const GROUPS = 8; const COLONS = GROUPS - 1; const DIGITS_MIN = 1; const DIGITS_MAX = 4; const EMBEDDED = 6; // groups preceding a dotted-quad tail const IPV4_MAX = "255.255.255.255".length; const SHORTEST = "::".length; const LONGEST = EMBEDDED * DIGITS_MAX + EMBEDDED + IPV4_MAX; const compose = (digits, count) => (0, __randomComposition_1.__randomComposition)({ total: digits, count, minimum: DIGITS_MIN, maximum: DIGITS_MAX, }) .map(group) .join(":"); const random = () => (0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 65535, }).toString(16); const group = (digits) => { let text = ""; for (let i = 0; i < digits; ++i) text += (0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 15, }).toString(16); return text; }; //# sourceMappingURL=_randomFormatIpv6.js.map