typia
Version:
Superfast runtime validators with only one line
56 lines (55 loc) • 1.74 kB
JavaScript
import { _randomInteger } from "./_randomInteger.mjs";
import { _randomLengthPick, _randomLengthWindow } from "./_randomStringLength.mjs";
import { __randomComposition } from "./private/__randomComposition.mjs";
import { _randomFormatIpv4 } from "./_randomFormatIpv4.mjs";
//#region src/internal/_randomFormatIpv6.ts
const _randomFormatIpv6 = (props) => {
if (props?.minLength === void 0 && props?.maxLength === void 0) return new Array(GROUPS).fill(0).map(random).join(":");
const length = _randomLengthPick(_randomLengthWindow(props, {
minimum: SHORTEST,
maximum: LONGEST,
spread: 12
}));
if (length === SHORTEST) return "::";
if (length < GROUPS * DIGITS_MIN + COLONS) {
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);
const tail = _randomFormatIpv4({
minLength: IPV4_MAX,
maxLength: IPV4_MAX
});
return `${compose(length - IPV4_MAX - EMBEDDED, EMBEDDED)}:${tail}`;
};
const GROUPS = 8;
const COLONS = GROUPS - 1;
const DIGITS_MIN = 1;
const DIGITS_MAX = 4;
const EMBEDDED = 6;
const IPV4_MAX = 15;
const SHORTEST = 2;
const LONGEST = 45;
const compose = (digits, count) => __randomComposition({
total: digits,
count,
minimum: DIGITS_MIN,
maximum: DIGITS_MAX
}).map(group).join(":");
const random = () => _randomInteger({
type: "integer",
minimum: 0,
maximum: 65535
}).toString(16);
const group = (digits) => {
let text = "";
for (let i = 0; i < digits; ++i) text += _randomInteger({
type: "integer",
minimum: 0,
maximum: 15
}).toString(16);
return text;
};
//#endregion
export { _randomFormatIpv6 };
//# sourceMappingURL=_randomFormatIpv6.mjs.map