@matatbread/typia
Version:
Superfast runtime validators with only one line
40 lines • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._randomInteger = void 0;
const _randomInteger = (props) => {
var _a, _b, _c, _d;
let minimum = (_a = props.minimum) !== null && _a !== void 0 ? _a : ((_b = props.multipleOf) !== null && _b !== void 0 ? _b : 1) *
(props.maximum === undefined ? 0 : props.maximum - 100);
if (props.minimum !== undefined && props.exclusiveMinimum === true)
minimum++;
let maximum = (_c = props.maximum) !== null && _c !== void 0 ? _c : ((_d = props.multipleOf) !== null && _d !== void 0 ? _d : 1) *
(props.minimum === undefined ? 100 : props.minimum + 100);
if (props.maximum !== undefined && props.exclusiveMaximum === true)
maximum--;
if (minimum > maximum)
throw new Error("Minimum value is greater than maximum value.");
return props.multipleOf === undefined
? scalar({
minimum,
maximum,
})
: multiple({
minimum,
maximum,
multipleOf: props.multipleOf,
});
};
exports._randomInteger = _randomInteger;
const scalar = (p) => Math.floor(Math.random() * (p.maximum - p.minimum + 1)) + p.minimum;
const multiple = (p) => {
const minimum = Math.ceil(p.minimum / p.multipleOf) * p.multipleOf;
const maximum = Math.floor(p.maximum / p.multipleOf) * p.multipleOf;
if (minimum > maximum)
throw new Error("The range of the integer is smaller than the multipleOf value.");
const value = scalar({
minimum,
maximum,
});
return value - (value % p.multipleOf);
};
//# sourceMappingURL=_randomInteger.js.map