UNPKG

stream-chat-react-native-core

Version:

The official React Native and Expo components for Stream Chat, a service for building chat applications

83 lines 2.81 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.predefinedFormatters = void 0; var _dayjs = require("dayjs"); var _getDateString = require("./getDateString"); var _isDayOrMoment = require("../../contexts/translationContext/isDayOrMoment"); var predefinedFormatters = exports.predefinedFormatters = { durationFormatter: streamI18n => (value, _, { format, withSuffix }) => { if (format && (0, _dayjs.isDayjs)(streamI18n.DateTimeParser)) { return streamI18n.DateTimeParser.duration(value).format(format); } return streamI18n.DateTimeParser.duration(value).humanize(!!withSuffix); }, timestampFormatter: streamI18n => (value, _, { calendarFormats, ...options }) => { var parsedCalendarFormats; try { if (!options.calendar) { parsedCalendarFormats = {}; } else if (typeof calendarFormats === 'string') { parsedCalendarFormats = JSON.parse(calendarFormats); } else if (typeof calendarFormats === 'object') { parsedCalendarFormats = calendarFormats; } } catch (e) { console.error('[TIMESTAMP FORMATTER]', e); } var result = (0, _getDateString.getDateString)({ ...options, calendarFormats: parsedCalendarFormats, date: value, tDateTimeParser: streamI18n.tDateTimeParser }); if (!result || typeof result === 'number') { return JSON.stringify(value); } return result; }, fromNowFormatter: streamI18n => (value, _, { withSuffix } = {}) => { var parsedTime = streamI18n.tDateTimeParser(value); if (!(0, _isDayOrMoment.isDayOrMoment)(parsedTime) || typeof parsedTime.fromNow !== 'function') { return JSON.stringify(value); } return parsedTime.fromNow(withSuffix === false); }, relativeCompactDateFormatter: streamI18n => value => { if (value === undefined || value === null) { return JSON.stringify(value); } var parsedTime = streamI18n.tDateTimeParser(value); var parsedNow = streamI18n.tDateTimeParser(); if (!(0, _isDayOrMoment.isDayOrMoment)(parsedTime) || !(0, _isDayOrMoment.isDayOrMoment)(parsedNow)) { return JSON.stringify(value); } var oneDayInMs = 24 * 60 * 60 * 1000; var startOfNowDayMs = parsedNow.startOf('day').valueOf(); var startOfTimeDayMs = parsedTime.startOf('day').valueOf(); var daysAgo = Math.floor((startOfNowDayMs - startOfTimeDayMs) / oneDayInMs); if (daysAgo <= 0) { return 'Today'; } if (daysAgo === 1) { return 'Yesterday'; } if (daysAgo <= 6) { return `${daysAgo}d ago`; } var weeksAgo = Math.floor(daysAgo / 7); if (weeksAgo <= 3) { return `${weeksAgo}w ago`; } return parsedTime.format('DD/MM/YY'); } }; //# sourceMappingURL=predefinedFormatters.js.map