UNPKG

typia

Version:

Superfast runtime validators with only one line

31 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._randomFormatDatetime = void 0; const _randomStringLength_1 = require("./_randomStringLength"); const __randomComposition_1 = require("./private/__randomComposition"); const __randomEpoch_1 = require("./private/__randomEpoch"); const _randomFormatDatetime = (props) => { const instant = () => new Date((0, __randomEpoch_1.__randomEpoch)(props)).toISOString(); if ((props === null || props === void 0 ? void 0 : props.minLength) === undefined && (props === null || props === void 0 ? void 0 : props.maxLength) === undefined) return instant(); // `YYYY-MM-DDTHH:MM:SS` plus `Z` is the shortest instant, and a fractional // second grows it one digit at a time. Because that fraction needs both a dot // and at least one digit, 21 is the single length between the two forms that // no instant can have. const window = (0, _randomStringLength_1._randomLengthWindow)(props, { minimum: SECONDS + 1, spread: 8, }); let length = (0, _randomStringLength_1._randomLengthPick)(window); if (length === SECONDS + 2) length = window.high >= SECONDS + 3 ? SECONDS + 3 : SECONDS + 1; if (length < window.low || length > window.high) throw new Error(_randomStringLength_1._RANDOM_LENGTH_ERROR); const base = instant().substring(0, SECONDS); return length === SECONDS + 1 ? `${base}Z` : `${base}.${(0, __randomComposition_1.__randomDigits)(length - SECONDS - 2)}Z`; }; exports._randomFormatDatetime = _randomFormatDatetime; const SECONDS = "0000-00-00T00:00:00".length; //# sourceMappingURL=_randomFormatDatetime.js.map