UNPKG

@opensig/opendesign

Version:

41 lines (40 loc) 1.54 kB
import { computed, toValue } from "vue"; import dayjs from "dayjs"; import { TIME_PREFIX } from "./types.mjs"; const isTimeBefore = (a, b) => dayjs(TIME_PREFIX + a).isBefore(dayjs(TIME_PREFIX + b)); const isTimeAfter = (a, b) => dayjs(TIME_PREFIX + a).isAfter(dayjs(TIME_PREFIX + b)); function useTimeRangeConstraints(params) { const { startTime, endTime, format, hourStep, minuteStep, secondStep, enabled } = params; const smallestUnit = computed(() => { var _a; const fmt = ((_a = toValue(format)) == null ? void 0 : _a.toLowerCase()) ?? "hh:mm:ss"; if (fmt.includes("ss")) return "second"; if (fmt.includes("mm")) return "minute"; return "hour"; }); const smallestStep = computed( () => ({ second: toValue(secondStep) ?? 1, minute: toValue(minuteStep) ?? 1, hour: toValue(hourStep) ?? 1 })[smallestUnit.value] ); const maxStartTime = computed(() => { if (enabled !== void 0 && !toValue(enabled)) return void 0; const end = toValue(endTime); if (!end) return void 0; return dayjs(TIME_PREFIX + end).subtract(smallestStep.value, smallestUnit.value).format("HH:mm:ss"); }); const minEndTime = computed(() => { if (enabled !== void 0 && !toValue(enabled)) return void 0; const start = toValue(startTime); if (!start) return void 0; return dayjs(TIME_PREFIX + start).add(smallestStep.value, smallestUnit.value).format("HH:mm:ss"); }); return { maxStartTime, minEndTime }; } export { isTimeAfter, isTimeBefore, useTimeRangeConstraints };