typia
Version:
Superfast runtime validators with only one line
39 lines • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports._randomFormatDuration = void 0;
const _randomInteger_1 = require("./_randomInteger");
const _randomStringLength_1 = require("./_randomStringLength");
const __randomComposition_1 = require("./private/__randomComposition");
const _randomFormatDuration = (props) => {
if ((props === null || props === void 0 ? void 0 : props.minLength) === undefined && (props === null || props === void 0 ? void 0 : props.maxLength) === undefined) {
const period = durate([
["Y", random(0, 100)],
["M", random(0, 12)],
["D", random(0, 31)],
]);
const time = durate([
["H", random(0, 24)],
["M", random(0, 60)],
["S", random(0, 60)],
]);
if (period.length + time.length === 0)
return "PT0S";
return `P${period}${time.length ? "T" : ""}${time}`;
}
// A single designator carries an unbounded digit count, so `P<n>Y` expresses
// every length from three upward and `P1Y` is the shortest duration there is.
const length = (0, _randomStringLength_1._randomLengthPick)((0, _randomStringLength_1._randomLengthWindow)(props, { minimum: MINIMUM, spread: 6 }));
return `P${(0, __randomComposition_1.__randomNumeric)(length - MINIMUM + 1)}Y`;
};
exports._randomFormatDuration = _randomFormatDuration;
const MINIMUM = "P1Y".length;
const durate = (elements) => elements
.filter(([_unit, value]) => value !== 0)
.map(([unit, value]) => `${value}${unit}`)
.join("");
const random = (minimum, maximum) => (0, _randomInteger_1._randomInteger)({
type: "integer",
minimum,
maximum,
});
//# sourceMappingURL=_randomFormatDuration.js.map