UNPKG

typia

Version:

Superfast runtime validators with only one line

31 lines 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._randomFormatByte = void 0; const _randomString_1 = require("./_randomString"); const _randomStringLength_1 = require("./_randomStringLength"); const _randomFormatByte = (props) => { if ((props === null || props === void 0 ? void 0 : props.minLength) === undefined && (props === null || props === void 0 ? void 0 : props.maxLength) === undefined) return FIXED; // Base64 encodes three bytes as four characters, so a valid length is always // a multiple of four; the window is raised to the first multiple it contains // and the draw steps by four from there. Every character `_randomString` // emits is in the base64 alphabet, so the result needs no padding. const window = (0, _randomStringLength_1._randomLengthWindow)(props, { minimum: 0, spread: 16 }); const low = Math.ceil(window.low / 4) * 4; if (low > window.high) throw new Error(_randomStringLength_1._RANDOM_LENGTH_ERROR); const length = low + 4 * (0, _randomStringLength_1._randomLengthPick)({ low: 0, high: Math.floor((window.high - low) / 4), }); return (0, _randomString_1._randomString)({ type: "string", minLength: length, maxLength: length, }); }; exports._randomFormatByte = _randomFormatByte; const FIXED = "vt7ekz4lIoNTTS9sDQYdWKharxIFAR54+z/umIxSgUM="; //# sourceMappingURL=_randomFormatByte.js.map