@helpwave/hightide
Version:
helpwave's component and theming library
193 lines (190 loc) • 7.04 kB
JavaScript
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/util/date.ts
var date_exports = {};
__export(date_exports, {
addDuration: () => addDuration,
changeDuration: () => changeDuration,
equalDate: () => equalDate,
formatDate: () => formatDate,
formatDateTime: () => formatDateTime,
getBetweenDuration: () => getBetweenDuration,
getDaysInMonth: () => getDaysInMonth,
getWeeksForCalenderMonth: () => getWeeksForCalenderMonth,
isInTimeSpan: () => isInTimeSpan,
monthsList: () => monthsList,
subtractDuration: () => subtractDuration,
weekDayList: () => weekDayList
});
module.exports = __toCommonJS(date_exports);
// src/util/array.ts
var equalSizeGroups = (array, groupSize) => {
if (groupSize <= 0) {
console.warn(`group size should be greater than 0: groupSize = ${groupSize}`);
return [[...array]];
}
const groups = [];
for (let i = 0; i < array.length; i += groupSize) {
groups.push(array.slice(i, Math.min(i + groupSize, array.length)));
}
return groups;
};
// src/util/date.ts
var monthsList = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var weekDayList = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
var formatDate = (date) => {
const year = date.getFullYear().toString().padStart(4, "0");
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
return `${year}-${month}-${day}`;
};
var formatDateTime = (date) => {
const dateString = formatDate(date);
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
return `${dateString}T${hours}:${minutes}`;
};
var getDaysInMonth = (year, month) => {
const lastDayOfMonth = new Date(year, month + 1, 0);
return lastDayOfMonth.getDate();
};
var changeDuration = (date, duration, isAdding) => {
const {
years = 0,
months = 0,
days = 0,
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0
} = duration;
if (years < 0) {
console.error(`Range error years must be greater than 0: received ${years}`);
return new Date(date);
}
if (months < 0 || months > 11) {
console.error(`Range error month must be 0 <= month <= 11: received ${months}`);
return new Date(date);
}
if (days < 0) {
console.error(`Range error days must be greater than 0: received ${days}`);
return new Date(date);
}
if (hours < 0 || hours > 23) {
console.error(`Range error hours must be 0 <= hours <= 23: received ${hours}`);
return new Date(date);
}
if (minutes < 0 || minutes > 59) {
console.error(`Range error minutes must be 0 <= minutes <= 59: received ${minutes}`);
return new Date(date);
}
if (seconds < 0 || seconds > 59) {
console.error(`Range error seconds must be 0 <= seconds <= 59: received ${seconds}`);
return new Date(date);
}
if (milliseconds < 0) {
console.error(`Range error seconds must be greater than 0: received ${milliseconds}`);
return new Date(date);
}
const multiplier = isAdding ? 1 : -1;
const newDate = new Date(date);
newDate.setFullYear(newDate.getFullYear() + multiplier * years);
newDate.setMonth(newDate.getMonth() + multiplier * months);
newDate.setDate(newDate.getDate() + multiplier * days);
newDate.setHours(newDate.getHours() + multiplier * hours);
newDate.setMinutes(newDate.getMinutes() + multiplier * minutes);
newDate.setSeconds(newDate.getSeconds() + multiplier * seconds);
newDate.setMilliseconds(newDate.getMilliseconds() + multiplier * milliseconds);
return newDate;
};
var addDuration = (date, duration) => {
return changeDuration(date, duration, true);
};
var subtractDuration = (date, duration) => {
return changeDuration(date, duration, false);
};
var getBetweenDuration = (startDate, endDate) => {
const durationInMilliseconds = endDate.getTime() - startDate.getTime();
const millisecondsInSecond = 1e3;
const millisecondsInMinute = 60 * millisecondsInSecond;
const millisecondsInHour = 60 * millisecondsInMinute;
const millisecondsInDay = 24 * millisecondsInHour;
const millisecondsInMonth = 30 * millisecondsInDay;
const years = Math.floor(durationInMilliseconds / (365.25 * millisecondsInDay));
const months = Math.floor(durationInMilliseconds / millisecondsInMonth);
const days = Math.floor(durationInMilliseconds / millisecondsInDay);
const hours = Math.floor(durationInMilliseconds % millisecondsInDay / millisecondsInHour);
const seconds = Math.floor(durationInMilliseconds % millisecondsInHour / millisecondsInSecond);
const milliseconds = durationInMilliseconds % millisecondsInSecond;
return {
years,
months,
days,
hours,
seconds,
milliseconds
};
};
var isInTimeSpan = (value, startDate, endDate) => {
if (startDate && endDate) {
console.assert(startDate <= endDate);
return startDate <= value && value <= endDate;
} else if (startDate) {
return startDate <= value;
} else if (endDate) {
return endDate >= value;
} else {
return true;
}
};
var equalDate = (date1, date2) => {
return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate();
};
var getWeeksForCalenderMonth = (date, weekStart, weeks = 6) => {
const month = date.getMonth();
const year = date.getFullYear();
const dayList = [];
let currentDate = new Date(year, month, 1);
const weekStartIndex = weekDayList.indexOf(weekStart);
while (currentDate.getDay() !== weekStartIndex) {
currentDate = subtractDuration(currentDate, { days: 1 });
}
while (dayList.length < 7 * weeks) {
const date2 = new Date(currentDate);
date2.setHours(date2.getHours(), date2.getMinutes());
dayList.push(date2);
currentDate = addDuration(currentDate, { days: 1 });
}
return equalSizeGroups(dayList, 7);
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
addDuration,
changeDuration,
equalDate,
formatDate,
formatDateTime,
getBetweenDuration,
getDaysInMonth,
getWeeksForCalenderMonth,
isInTimeSpan,
monthsList,
subtractDuration,
weekDayList
});
//# sourceMappingURL=date.js.map