UNPKG

typia

Version:

Superfast runtime validators with only one line

41 lines 2.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._randomFormatTime = void 0; const _randomInteger_1 = require("./_randomInteger"); const _randomStringLength_1 = require("./_randomStringLength"); const __randomComposition_1 = require("./private/__randomComposition"); const _randomFormatTime = (props) => { const clock = () => new Date((0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: DAY })).toISOString(); if ((props === null || props === void 0 ? void 0 : props.minLength) === undefined && (props === null || props === void 0 ? void 0 : props.maxLength) === undefined) return clock().substring(11); // `HH:MM:SS` carries a mandatory offset — one character as `Z`, six as // `+HH:MM` — and an optional fraction of one to nine digits between them. // Together they express 9 and every length from 11 through 24; 10 is the one // gap, because a fraction needs both a dot and a digit. const window = (0, _randomStringLength_1._randomLengthWindow)(props, { minimum: CLOCK + 1, maximum: CLOCK + 1 + FRACTION_MAX + OFFSET, spread: 6, }); let length = (0, _randomStringLength_1._randomLengthPick)(window); if (length === CLOCK + 2) length = window.high >= CLOCK + 3 ? CLOCK + 3 : CLOCK + 1; if (length < window.low || length > window.high) throw new Error(_randomStringLength_1._RANDOM_LENGTH_ERROR); const base = clock().substring(11, 11 + CLOCK); if (length === CLOCK + 1) return `${base}Z`; // Nine fraction digits cap the `Z` form at 19 characters, so anything longer // spends the extra five on a numeric offset instead. return length <= CLOCK + 2 + FRACTION_MAX ? `${base}.${(0, __randomComposition_1.__randomDigits)(length - CLOCK - 2)}Z` : `${base}.${(0, __randomComposition_1.__randomDigits)(length - CLOCK - 1 - OFFSET)}${offset()}`; }; exports._randomFormatTime = _randomFormatTime; const DAY = 86400000; const CLOCK = "00:00:00".length; const OFFSET = "+00:00".length; const FRACTION_MAX = 9; const offset = () => `${(0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 1 }) === 0 ? "+" : "-"}${pad((0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 23 }))}:${pad((0, _randomInteger_1._randomInteger)({ type: "integer", minimum: 0, maximum: 59 }))}`; const pad = (value) => String(value).padStart(2, "0"); //# sourceMappingURL=_randomFormatTime.js.map