UNPKG

@opensig/opendesign

Version:

72 lines (71 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const dayjs = require("dayjs"); const time = require("../_utils/time.js"); const types = require("./types.js"); function addFnDisabled(disabled, options, disabledFn) { if (!disabledFn) return; for (const v of disabledFn()) { if (options.some((opt) => opt.value === v)) disabled.add(v); } } function buildDisabledSet({ options, minBoundary, maxBoundary, disabledFn }) { const disabled = /* @__PURE__ */ new Set(); for (const opt of options) { if ((minBoundary == null ? void 0 : minBoundary.match) && opt.value < minBoundary.value) disabled.add(opt.value); if ((maxBoundary == null ? void 0 : maxBoundary.match) && opt.value > maxBoundary.value) disabled.add(opt.value); } addFnDisabled(disabled, options, disabledFn); return disabled; } function findClosest(options, disabled, target) { const enabled = options.filter((opt) => !disabled.has(opt.value)); if (enabled.length === 0) return void 0; const atOrAfter = enabled.find((opt) => opt.value >= target); return atOrAfter ? atOrAfter.value : enabled[enabled.length - 1].value; } function resolveHour({ options, bounds, disabledFn, target }) { const disabled = buildDisabledSet({ options, minBoundary: bounds.minT ? { match: true, value: bounds.minT.hour() } : void 0, maxBoundary: bounds.maxT ? { match: true, value: bounds.maxT.hour() } : void 0, disabledFn }); return findClosest(options, disabled, target); } function resolveMinute({ options, bounds, h, disabledFn, target }) { var _a, _b, _c, _d; const disabled = buildDisabledSet({ options, minBoundary: { match: h === ((_a = bounds.minT) == null ? void 0 : _a.hour()), value: ((_b = bounds.minT) == null ? void 0 : _b.minute()) ?? 0 }, maxBoundary: { match: h === ((_c = bounds.maxT) == null ? void 0 : _c.hour()), value: ((_d = bounds.maxT) == null ? void 0 : _d.minute()) ?? 59 }, disabledFn: disabledFn ? () => disabledFn(h) : void 0 }); return findClosest(options, disabled, target); } function resolveSecond({ options, bounds, h, m, disabledFn, target }) { var _a, _b, _c, _d, _e, _f; const disabled = buildDisabledSet({ options, minBoundary: { match: h === ((_a = bounds.minT) == null ? void 0 : _a.hour()) && m === ((_b = bounds.minT) == null ? void 0 : _b.minute()), value: ((_c = bounds.minT) == null ? void 0 : _c.second()) ?? 0 }, maxBoundary: { match: h === ((_d = bounds.maxT) == null ? void 0 : _d.hour()) && m === ((_e = bounds.maxT) == null ? void 0 : _e.minute()), value: ((_f = bounds.maxT) == null ? void 0 : _f.second()) ?? 59 }, disabledFn: disabledFn ? () => disabledFn(h, m) : void 0 }); return findClosest(options, disabled, target); } function findNearestTime(params) { const { hourOptions, minuteOptions, secondOptions, format, target } = params; const bounds = { minT: params.minTime ? dayjs(types.TIME_PREFIX + params.minTime) : null, maxT: params.maxTime ? dayjs(types.TIME_PREFIX + params.maxTime) : null }; const h = resolveHour({ options: hourOptions, bounds, disabledFn: params.disabledHours, target: target.hour }); if (h === void 0) return void 0; const mTarget = h === target.hour ? target.minute : 0; const m = resolveMinute({ options: minuteOptions, bounds, h, disabledFn: params.disabledMinutes, target: mTarget }); if (m === void 0) return void 0; const sTarget = h === target.hour && m === target.minute ? target.second : 0; const s = resolveSecond({ options: secondOptions, bounds, h, m, disabledFn: params.disabledSeconds, target: sTarget }) ?? 0; return dayjs(`1970-01-01 ${time.pad(h)}:${time.pad(m)}:${time.pad(s)}`).format(format); } exports.findNearestTime = findNearestTime;