@kelvininc/ui-components
Version:
Kelvin UI Components
80 lines (76 loc) • 4.42 kB
JavaScript
import './p-BRgmvbuh.js';
import { m as ERelativeTimeComparisonConfig, n as EUnitReference } from './p-DBphUUgi.js';
import './p-BcMhjKoS.js';
import './p-BQDwJ0uF.js';
import { n as newTimezoneDate, i as calculateDate, j as newTimezoneDateFromFormat, k as isDateTimeBefore } from './p-Ba4zaTTd.js';
import { D as DEFAULT_RELATIVE_TIME_OPTIONS_GROUPS } from './p-zjJfMcH8.js';
/** Date range builders */
const buildOptionRange = (option, timeZone) => {
switch (option.comparisonConfig) {
case ERelativeTimeComparisonConfig.StartDateEndDate:
return buildStartDateEndDateConfigRange(option, timeZone);
case ERelativeTimeComparisonConfig.StartDate || ERelativeTimeComparisonConfig.EndDate:
return buildSingleDateConfigRange(option, timeZone);
case ERelativeTimeComparisonConfig.AbsoluteAmountOfUnits:
return buildAbsoluteAmountOfUnitsConfigRange(option, timeZone);
default:
return buildRelativeAmountOfUnitsConfigRange(option, timeZone);
}
};
// Build date from config with start and end date
const buildStartDateEndDateConfigRange = (option, timeZone) => {
const startDateTime = newTimezoneDateFromFormat(timeZone, option.startDate.dateFormat, option.startDate.date);
const endDateTime = newTimezoneDateFromFormat(timeZone, option.endDate.dateFormat, option.endDate.date);
return buildDayjsRange(startDateTime, endDateTime);
};
// Build date from config with start or end date
const buildSingleDateConfigRange = (option, timeZone) => {
const nowDateTime = newTimezoneDate(timeZone);
const calculatedDate = newTimezoneDateFromFormat(timeZone, option.startDate.dateFormat, option.startDate.date);
return buildDayjsRange(nowDateTime, calculatedDate);
};
// Build date with both start and end date relative to now timestamp
const buildAbsoluteAmountOfUnitsConfigRange = (option, timeZone) => {
const nowDateTime = newTimezoneDate(timeZone);
const { unit: startDateUnit, amount: startDateAmount, unitReference: startUnitReference } = option.startDate;
const { unit: endDateUnit, amount: endDateAmount, unitReference: endUnitReference } = option.endDate;
const startDateTime = calculateDateWithUnitReference(calculateDate(nowDateTime, startDateAmount, startDateUnit), startDateUnit, startUnitReference);
const endDateTime = calculateDateWithUnitReference(calculateDate(nowDateTime, endDateAmount, endDateUnit), endDateUnit, endUnitReference);
return buildDayjsRange(startDateTime, endDateTime);
};
// Build date with start date relative to now timestamp
const buildRelativeAmountOfUnitsConfigRange = (option, timeZone) => {
const nowDateTime = newTimezoneDate(timeZone);
const { amount, unit, unitReference } = option.startDate;
const calculatedDate = calculateDateWithUnitReference(calculateDate(nowDateTime, amount, unit), unit, unitReference);
return buildDayjsRange(nowDateTime, calculatedDate);
};
const calculateDateWithUnitReference = (date, unit, unitReference) => {
if (!unitReference) {
return date;
}
return unitReference === EUnitReference.StartOfUnit ? date.startOf(unit) : date.endOf(unit);
};
/**
* Builds the range of the start and end date and returns the range array, ordering the dates if needed
* @param dateA date (start or end)
* @param dateB date (start or end)
* @returns range containing start date and end date ordered
*/
const buildDayjsRange = (dateA, dateB) => {
return isDateTimeBefore(dateA, dateB) ? [dateA, dateB] : [dateB, dateA];
};
const buildTimestampRange = ([startDate, endDate]) => [startDate.valueOf(), endDate.valueOf()];
const getRelativeTimeOption = (key, options = DEFAULT_RELATIVE_TIME_OPTIONS_GROUPS) => options.flat().find(option => option.value === key);
var relativeTime_helper = /*#__PURE__*/Object.freeze({
__proto__: null,
buildAbsoluteAmountOfUnitsConfigRange: buildAbsoluteAmountOfUnitsConfigRange,
buildDayjsRange: buildDayjsRange,
buildOptionRange: buildOptionRange,
buildRelativeAmountOfUnitsConfigRange: buildRelativeAmountOfUnitsConfigRange,
buildSingleDateConfigRange: buildSingleDateConfigRange,
buildStartDateEndDateConfigRange: buildStartDateEndDateConfigRange,
buildTimestampRange: buildTimestampRange,
getRelativeTimeOption: getRelativeTimeOption
});
export { buildTimestampRange as a, buildOptionRange as b, relativeTime_helper as r };