UNPKG

@wordpress/components

Version:
117 lines (115 loc) 4.62 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); // packages/components/src/date-time/utils.ts var utils_exports = {}; __export(utils_exports, { buildPadInputStateReducer: () => buildPadInputStateReducer, from12hTo24h: () => from12hTo24h, from24hTo12h: () => from24hTo12h, getDaysInMonth: () => getDaysInMonth, inputToDate: () => inputToDate, setInConfiguredTimezone: () => setInConfiguredTimezone, startOfDayInConfiguredTimezone: () => startOfDayInConfiguredTimezone, validateInputElementTarget: () => validateInputElementTarget }); module.exports = __toCommonJS(utils_exports); var import_utc = require("@date-fns/utc"); var import_date = require("@wordpress/date"); var import_actions = require("../input-control/reducer/actions.cjs"); function inputToDate(input) { if (typeof input === "string") { const hasTimezone = /Z|[+-]\d{2}(:?\d{2})?$/.test(input); if (hasTimezone) { return new import_utc.UTCDateMini(new Date(input)); } return new import_utc.UTCDateMini((0, import_date.getDate)(input).getTime()); } const time = input instanceof Date ? input.getTime() : input; return new import_utc.UTCDateMini(time); } function startOfDayInConfiguredTimezone(date) { const year = Number((0, import_date.date)("Y", date)); const month = Number((0, import_date.date)("n", date)) - 1; const day = Number((0, import_date.date)("j", date)); return new Date(year, month, day, 0, 0, 0, 0); } function from12hTo24h(hours, isPm) { return isPm ? (hours % 12 + 12) % 24 : hours % 12; } function from24hTo12h(hours) { return hours % 12 || 12; } function buildPadInputStateReducer(pad) { return (state, action) => { const nextState = { ...state }; if (action.type === import_actions.COMMIT || action.type === import_actions.PRESS_UP || action.type === import_actions.PRESS_DOWN) { if (nextState.value !== void 0) { nextState.value = nextState.value.toString().padStart(pad, "0"); } } return nextState; }; } var getDaysInMonth = (year, month) => ( // Take advantage of JavaScript's built-in date wrapping logic, where day 0 // of the next month is interpreted as the last day of the preceding month. new Date(year, month + 1, 0).getDate() ); function setInConfiguredTimezone(date, updates) { const values = { year: Number((0, import_date.date)("Y", date)), month: Number((0, import_date.date)("n", date)) - 1, date: Number((0, import_date.date)("j", date)), hours: Number((0, import_date.date)("H", date)), minutes: Number((0, import_date.date)("i", date)), seconds: Number((0, import_date.date)("s", date)), ...updates }; const daysInMonth = getDaysInMonth(values.year, values.month); values.date = Math.min(values.date, daysInMonth); const year = String(values.year).padStart(4, "0"); const month = String(values.month + 1).padStart(2, "0"); const day = String(values.date).padStart(2, "0"); const hours = String(values.hours).padStart(2, "0"); const minutes = String(values.minutes).padStart(2, "0"); const seconds = String(values.seconds).padStart(2, "0"); const timezoneless = `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`; return new import_utc.UTCDateMini((0, import_date.getDate)(timezoneless).getTime()); } function validateInputElementTarget(event) { const HTMLInputElementInstance = event.target?.ownerDocument.defaultView?.HTMLInputElement ?? HTMLInputElement; if (!(event.target instanceof HTMLInputElementInstance)) { return false; } return event.target.validity.valid; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { buildPadInputStateReducer, from12hTo24h, from24hTo12h, getDaysInMonth, inputToDate, setInConfiguredTimezone, startOfDayInConfiguredTimezone, validateInputElementTarget }); //# sourceMappingURL=utils.cjs.map