@cerberus-design/react
Version:
The Cerberus Design React component library.
130 lines (129 loc) • 3.75 kB
JavaScript
;
"use client";
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/hooks/useDate.ts
var useDate_exports = {};
__export(useDate_exports, {
DateFormats: () => DateFormats,
formatISOToMilitary: () => formatISOToMilitary,
formatMilitaryDate: () => formatMilitaryDate,
formatMilitaryToISO: () => formatMilitaryToISO,
useDate: () => useDate
});
module.exports = __toCommonJS(useDate_exports);
var import_react = require("react");
function useDate(options) {
const initialValue = (options == null ? void 0 : options.initialValue) ?? "";
const format = (options == null ? void 0 : options.format) ?? DateFormats.USMilitary;
const onChange = options == null ? void 0 : options.onChange;
const [value, setValue] = (0, import_react.useState)(initialValue);
const handleChange = (0, import_react.useCallback)(
(e) => {
const newValue = formatMilitaryDate(e.currentTarget.value);
if (onChange) onChange(e);
setValue(newValue);
},
[onChange]
);
return (0, import_react.useMemo)(
() => ({
format,
value,
ISO: formatMilitaryToISO(value),
onChange: handleChange
}),
[format, value, handleChange]
);
}
function formatMilitaryToISO(input) {
const [day, month, year] = input.split(" ");
const monthIndex = MONTHS.findIndex((m) => m.startsWith(month));
const monthNum = monthIndex + 1;
return `${year ?? "0000"}-${monthNum.toString().padStart(2, "0")}-${day.padStart(
2,
"0"
)}`;
}
function formatMilitaryDate(input) {
let formatted = input.toUpperCase().replace(/[^0-9A-Z]/g, "");
let day = "";
let month = "";
let year = "";
if (formatted.length >= 2) {
day = formatted.replace(/[^0-9]/g, "").slice(0, 2);
const dayNum = parseInt(day, 10);
if (dayNum > 31) day = "31";
else if (dayNum === 0) day = "01";
formatted = formatted.slice(2);
}
if (formatted.length >= 3) {
month = formatted.slice(0, 3);
const monthIndex = MONTHS.findIndex((m) => m.startsWith(month));
if (monthIndex !== -1) {
month = MONTHS[monthIndex];
} else {
month = month.replace(/[^A-Z]/g, "");
}
formatted = formatted.slice(3);
}
if (formatted.length > 0) {
year = formatted.slice(0, 4);
}
return [day, month, year].filter(Boolean).join(" ");
}
function formatISOToMilitary(date) {
const [year, month, day] = date.split("-");
const monthIndex = parseInt(month, 10) - 1;
const monthStr = MONTHS[monthIndex];
return `${day} ${monthStr} ${year}`;
}
var DateFormats = {
get ISO() {
return "YYYY-MM-DD";
},
get USMilitary() {
return "DD MMM YYYY";
},
get Months() {
return MONTHS;
}
};
var MONTHS = [
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC"
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
DateFormats,
formatISOToMilitary,
formatMilitaryDate,
formatMilitaryToISO,
useDate
});
//# sourceMappingURL=useDate.cjs.map