@chayns-components/date
Version:
A set of beautiful React components for developing your own applications with chayns.
150 lines (149 loc) • 3.94 kB
JavaScript
;
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,
language,
shouldShowYear,
shouldShowTime,
shouldShowOnlyTime,
shouldShowDayOfWeek,
shouldShowRelativeDayOfWeek,
shouldUseShortText
}) => {
const {
active: activeLanguage
} = (0, _chaynsApi.getLanguage)();
const timeParts = {};
if (shouldShowTime || shouldShowOnlyTime) {
timeParts.hour = '2-digit';
timeParts.minute = '2-digit';
}
let formattedTime = '';
if (Object.keys(timeParts).length > 0) {
formattedTime = `${shouldShowOnlyTime ? '' : ', '}${date.toLocaleTimeString(language ?? activeLanguage, {
...timeParts
})}`;
}
const hourWord = getTimeString({
language: language ?? activeLanguage
});
formattedTime += shouldShowTime || shouldShowOnlyTime ? ` ${hourWord}` : '';
if (shouldShowOnlyTime) {
return formattedTime;
}
let dayPart = '';
if (shouldShowRelativeDayOfWeek) {
const rtf = new Intl.RelativeTimeFormat(language ?? activeLanguage, {
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 ?? activeLanguage, {
weekday: shouldUseShortText ? 'short' : 'long'
});
}
const dateParts = {
day: '2-digit',
month: shouldUseShortText ? 'short' : 'long'
};
if (shouldShowYear && !(0, _date.isCurrentYear)(date)) {
dateParts.year = 'numeric';
}
const formattedDate = `${date.toLocaleDateString(language ?? activeLanguage, 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: '',
fr: '',
de: 'Uhr',
es: '',
it: '',
pt: '',
pl: '',
tr: '',
uk: '',
en: ''
};
return map[language ?? ''] ?? '';
};
exports.getTimeString = getTimeString;
//# sourceMappingURL=dateInfo.js.map