62 lines
2.3 KiB
JavaScript
62 lines
2.3 KiB
JavaScript
const time = new Intl.DateTimeFormat('de-DE', { hour: '2-digit', minute: '2-digit' });
|
||
const weekday = new Intl.DateTimeFormat('de-DE', { weekday: 'short' });
|
||
const short = new Intl.DateTimeFormat('de-DE', { day: '2-digit', month: '2-digit', year: '2-digit' });
|
||
const long = new Intl.DateTimeFormat('de-DE', { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' });
|
||
|
||
const startOfDay = (d) => new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||
|
||
/** Zeit in der Gesprächsliste: heute Uhrzeit, diese Woche Wochentag, sonst Datum */
|
||
export function listTime(iso) {
|
||
if (!iso) return '';
|
||
|
||
const date = new Date(iso);
|
||
const days = (startOfDay(new Date()) - startOfDay(date)) / 86400000;
|
||
|
||
if (days === 0) return time.format(date);
|
||
if (days === 1) return 'Gestern';
|
||
if (days < 7) return weekday.format(date);
|
||
|
||
return short.format(date);
|
||
}
|
||
|
||
export const messageTime = (iso) => time.format(new Date(iso));
|
||
|
||
/** Trenner zwischen Tagen im Verlauf */
|
||
export function dayLabel(iso) {
|
||
const date = new Date(iso);
|
||
const days = (startOfDay(new Date()) - startOfDay(date)) / 86400000;
|
||
|
||
if (days === 0) return 'Heute';
|
||
if (days === 1) return 'Gestern';
|
||
|
||
return long.format(date);
|
||
}
|
||
|
||
export const sameDay = (a, b) => startOfDay(new Date(a)) === startOfDay(new Date(b));
|
||
|
||
export function fileSize(bytes) {
|
||
if (bytes < 1024) return `${bytes} B`;
|
||
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
|
||
|
||
return `${(bytes / 1024 / 1024).toFixed(1).replace('.', ',')} MB`;
|
||
}
|
||
|
||
/** Text in Stücke zerlegen, Links erkennen – ohne v-html */
|
||
export function linkify(text) {
|
||
const parts = [];
|
||
const pattern = /(https?:\/\/[^\s<>"']+[^\s<>"'.,;:!?)\]])/g;
|
||
let last = 0;
|
||
|
||
for (const match of text.matchAll(pattern)) {
|
||
if (match.index > last) parts.push({ text: text.slice(last, match.index) });
|
||
parts.push({ text: match[0], href: match[0] });
|
||
last = match.index + match[0].length;
|
||
}
|
||
|
||
if (last < text.length) parts.push({ text: text.slice(last) });
|
||
|
||
return parts;
|
||
}
|
||
|
||
/** Nur Emojis (bis zu 3)? Dann groß darstellen */
|
||
export const onlyEmoji = (text) => !!text && /^(\p{Extended_Pictographic}|\p{Emoji_Component}||️|\s){1,12}$/u.test(text) && [...text.replace(/\s/g, '')].length <= 8;
|