UNPKG

@zag-js/date-utils

Version:

Date utilities for zag.js

147 lines (145 loc) 5.13 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/constrain.ts var constrain_exports = {}; __export(constrain_exports, { alignCenter: () => alignCenter, alignEnd: () => alignEnd, alignStart: () => alignStart, constrainSegments: () => constrainSegments, constrainStart: () => constrainStart, constrainValue: () => constrainValue }); module.exports = __toCommonJS(constrain_exports); var import_date = require("@internationalized/date"); function alignCenter(date, duration, locale, min, max) { const halfDuration = {}; for (let prop in duration) { const key = prop; const value = duration[key]; if (value == null) continue; halfDuration[key] = Math.floor(value / 2); if (halfDuration[key] > 0 && value % 2 === 0) { halfDuration[key]--; } } const aligned = alignStart(date, duration, locale).subtract(halfDuration); return constrainStart(date, aligned, duration, locale, min, max); } function alignStart(date, duration, locale, min, max) { let aligned = date; if (duration.years) { aligned = (0, import_date.startOfYear)(date); } else if (duration.months) { aligned = (0, import_date.startOfMonth)(date); } else if (duration.weeks) { aligned = (0, import_date.startOfWeek)(date, locale); } return constrainStart(date, aligned, duration, locale, min, max); } function alignEnd(date, duration, locale, min, max) { let d = { ...duration }; if (d.days) { d.days--; } else if (d.weeks) { d.weeks--; } else if (d.months) { d.months--; } else if (d.years) { d.years--; } let aligned = alignStart(date, duration, locale).subtract(d); return constrainStart(date, aligned, duration, locale, min, max); } function constrainStart(date, aligned, duration, locale, min, max) { if (min && date.compare(min) >= 0) { aligned = (0, import_date.maxDate)(aligned, alignStart((0, import_date.toCalendarDate)(min), duration, locale)); } if (max && date.compare(max) <= 0) { aligned = (0, import_date.minDate)(aligned, alignEnd((0, import_date.toCalendarDate)(max), duration, locale)); } return aligned; } function constrainValue(date, minValue, maxValue) { const dateOnly = (0, import_date.toCalendarDate)(date); const minOnly = minValue ? (0, import_date.toCalendarDate)(minValue) : void 0; const maxOnly = maxValue ? (0, import_date.toCalendarDate)(maxValue) : void 0; let constrainedDateOnly = dateOnly; if (minOnly) { constrainedDateOnly = (0, import_date.maxDate)(constrainedDateOnly, minOnly); } if (maxOnly) { constrainedDateOnly = (0, import_date.minDate)(constrainedDateOnly, maxOnly); } if (constrainedDateOnly.compare(dateOnly) === 0) { return date; } if ("hour" in date) { return date.set({ year: constrainedDateOnly.year, month: constrainedDateOnly.month, day: constrainedDateOnly.day }); } return constrainedDateOnly; } function constrainSegments(date, minValue, maxValue) { const dateOnly = (0, import_date.toCalendarDate)(date); const minOnly = minValue ? (0, import_date.toCalendarDate)(minValue) : void 0; const maxOnly = maxValue ? (0, import_date.toCalendarDate)(maxValue) : void 0; let result = dateOnly; if (minOnly && result.compare(minOnly) < 0) { if (result.year < minOnly.year) { result = result.set({ year: minOnly.year }); } if (result.compare(minOnly) < 0 && result.month < minOnly.month) { result = result.set({ month: minOnly.month }); } if (result.compare(minOnly) < 0 && result.day < minOnly.day) { result = result.set({ day: minOnly.day }); } } if (maxOnly && result.compare(maxOnly) > 0) { if (result.year > maxOnly.year) { result = result.set({ year: maxOnly.year }); } if (result.compare(maxOnly) > 0 && result.month > maxOnly.month) { result = result.set({ month: maxOnly.month }); } if (result.compare(maxOnly) > 0 && result.day > maxOnly.day) { result = result.set({ day: maxOnly.day }); } } if (result.compare(dateOnly) === 0) { return date; } if ("hour" in date) { return date.set({ year: result.year, month: result.month, day: result.day }); } return result; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { alignCenter, alignEnd, alignStart, constrainSegments, constrainStart, constrainValue });