UNPKG

@chayns-components/date

Version:

A set of beautiful React components for developing your own applications with chayns.

145 lines (144 loc) 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getTimeTillNow = exports.getTimeString = exports.getFormattedTime = exports.getDateInfo = void 0; var _chaynsApi = require("chayns-api"); var _date = require("./date"); const getDateInfo = ({ date, shouldShowYear, shouldShowTime, shouldShowDayOfWeek, shouldShowRelativeDayOfWeek, shouldUseShortText }) => { const { active: language } = (0, _chaynsApi.getLanguage)(); let dayPart = ''; if (shouldShowRelativeDayOfWeek) { const rtf = new Intl.RelativeTimeFormat(language, { numeric: 'auto' }); if ((0, _date.isToday)(date)) { dayPart = capitalizeFirstLetter(rtf.format(0, 'day')); } if ((0, _date.isTomorrow)(date)) { dayPart = capitalizeFirstLetter(rtf.format(1, 'day')); } if ((0, _date.isYesterday)(date)) { dayPart = capitalizeFirstLetter(rtf.format(-1, 'day')); } } if (!dayPart && shouldShowDayOfWeek) { dayPart = date.toLocaleDateString(language, { weekday: shouldUseShortText ? 'short' : 'long' }); } const dateParts = { day: '2-digit', month: shouldUseShortText ? 'short' : 'long' }; if (shouldShowYear && !(0, _date.isCurrentYear)(date)) { dateParts.year = 'numeric'; } const timeParts = {}; if (shouldShowTime) { timeParts.hour = '2-digit'; timeParts.minute = '2-digit'; } let formattedTime = ''; if (Object.keys(timeParts).length > 0) { formattedTime = `, ${date.toLocaleTimeString(language, { ...timeParts })}`; } const hourWord = getTimeString({ language }); formattedTime += shouldShowTime ? ` ${hourWord}` : ''; const formattedDate = `${date.toLocaleDateString(language, dateParts)}${formattedTime}`; return `${dayPart}${dayPart ? ', ' : ''}${formattedDate}`; }; exports.getDateInfo = getDateInfo; const capitalizeFirstLetter = text => text.charAt(0).toUpperCase() + text.slice(1); const getTimeTillNow = ({ date, currentDate, language = _chaynsApi.Language.English }) => { const diffInSeconds = Math.floor((currentDate.getTime() - date.getTime()) / 1000); const isPast = diffInSeconds > 0; const units = [{ label: 'year', seconds: 31536000 }, { label: 'month', seconds: 2592000 }, { label: 'day', seconds: 86400 }, { label: 'hour', seconds: 3600 }, { label: 'minute', seconds: 60 }, { label: 'second', seconds: 1 }]; const absDiff = Math.abs(diffInSeconds); const { label, seconds } = units.find(u => absDiff >= u.seconds) || { label: 'second', seconds: 1 }; const count = Math.floor(absDiff / seconds); const formatter = new Intl.RelativeTimeFormat(language, { numeric: 'auto' }); return formatter.format(isPast ? -count : count, label); }; exports.getTimeTillNow = getTimeTillNow; const getFormattedTime = ({ date, shouldShowSeconds = false }) => { const { active: language } = (0, _chaynsApi.getLanguage)(); const timeOptions = { hour: '2-digit', minute: '2-digit', second: shouldShowSeconds ? '2-digit' : undefined }; const formattedTime = date.toLocaleTimeString(language, timeOptions).replace(/^0/, ''); const hourWord = getTimeString({ language }); return `${formattedTime} ${hourWord}`.trim(); }; exports.getFormattedTime = getFormattedTime; const getTimeString = ({ language }) => { const map = { nl: 'uur', fr: 'heures', de: 'Uhr', es: 'horas', it: 'ore', pt: 'horas', pl: 'godzina', tr: 'saat', uk: 'година', en: '' }; return map[language ?? ''] ?? ''; }; exports.getTimeString = getTimeString; //# sourceMappingURL=dateInfo.js.map