typia
Version:
Superfast runtime validators with only one line
34 lines (33 loc) • 1.1 kB
JavaScript
import { _randomInteger } from "./_randomInteger.mjs";
import { _randomLengthPick, _randomLengthWindow } from "./_randomStringLength.mjs";
import { __randomComposition } from "./private/__randomComposition.mjs";
//#region src/internal/_randomFormatIpv4.ts
const _randomFormatIpv4 = (props) => {
if (props?.minLength === void 0 && props?.maxLength === void 0) return new Array(4).fill(0).map(random).join(".");
return __randomComposition({
total: _randomLengthPick(_randomLengthWindow(props, {
minimum: 4 * DIGITS_MIN + DOTS,
maximum: 4 * DIGITS_MAX + DOTS,
spread: 8
})) - DOTS,
count: 4,
minimum: DIGITS_MIN,
maximum: DIGITS_MAX
}).map(octet).join(".");
};
const DOTS = 3;
const DIGITS_MIN = 1;
const DIGITS_MAX = 3;
const random = () => _randomInteger({
type: "integer",
minimum: 0,
maximum: 255
});
const octet = (digits) => String(_randomInteger({
type: "integer",
minimum: digits === 1 ? 0 : Math.pow(10, digits - 1),
maximum: digits === 3 ? 255 : Math.pow(10, digits) - 1
}));
//#endregion
export { _randomFormatIpv4 };
//# sourceMappingURL=_randomFormatIpv4.mjs.map