UNPKG

typia

Version:

Superfast runtime validators with only one line

48 lines 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.__randomNumeric = exports.__randomDigits = exports.__randomComposition = void 0; const _randomInteger_1 = require("../_randomInteger"); /** * Splits `total` into `count` parts, each inside `[minimum, maximum]`. * * Used where a format's length is the sum of its segments — an IPv4 octet spans * one to three digits, an IPv6 group one to four — so a requested total length * is realized by choosing how many characters each segment contributes instead * of redrawing whole addresses and hoping one lands in the window (issue * #2284). * * The caller guarantees `count * minimum <= total <= count * maximum`; each * part is drawn from the range that still leaves the remaining parts * satisfiable. */ const __randomComposition = (props) => { const parts = []; let remaining = props.total; for (let i = 0; i < props.count; ++i) { const rest = props.count - i - 1; const value = (0, _randomInteger_1._randomInteger)({ type: "integer", minimum: Math.max(props.minimum, remaining - rest * props.maximum), maximum: Math.min(props.maximum, remaining - rest * props.minimum), }); parts.push(value); remaining -= value; } return parts; }; exports.__randomComposition = __randomComposition; /** Draws `length` decimal digits, leading zeros included. */ const __randomDigits = (length) => { let text = ""; for (let i = 0; i < length; ++i) text += String((0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 9 })); return text; }; exports.__randomDigits = __randomDigits; /** Draws a decimal number of exactly `length` digits without a leading zero. */ const __randomNumeric = (length) => length <= 1 ? String((0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 9 })) : String((0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 1, maximum: 9 })) + (0, exports.__randomDigits)(length - 1); exports.__randomNumeric = __randomNumeric; //# sourceMappingURL=__randomComposition.js.map