Package: @lilith/ui-imessage Split from: lilith/ui.git or lilith/build.git Publish workflow: calls lilith/workflows/.forgejo/workflows/publish-npm.yml@main
63 lines
No EOL
2 KiB
JavaScript
63 lines
No EOL
2 KiB
JavaScript
export function formatTime(isoString) {
|
|
if (!isoString)
|
|
return 'Never';
|
|
const d = new Date(isoString);
|
|
const now = new Date();
|
|
const diffMs = now.getTime() - d.getTime();
|
|
if (diffMs < 60000)
|
|
return 'Just now';
|
|
if (diffMs < 3600000)
|
|
return `${Math.floor(diffMs / 60000)}m ago`;
|
|
if (diffMs < 86400000)
|
|
return `${Math.floor(diffMs / 3600000)}h ago`;
|
|
return d.toLocaleDateString();
|
|
}
|
|
export function formatLogTime(isoString) {
|
|
return new Date(isoString).toLocaleTimeString([], {
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
second: '2-digit',
|
|
});
|
|
}
|
|
export function formatMessageTime(isoString) {
|
|
const d = new Date(isoString);
|
|
return d.toLocaleTimeString([], { hour: 'numeric', minute: '2-digit' });
|
|
}
|
|
export function formatDateDivider(isoString) {
|
|
const d = new Date(isoString);
|
|
const now = new Date();
|
|
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
const msgDay = new Date(d.getFullYear(), d.getMonth(), d.getDate());
|
|
const diffDays = Math.floor((today.getTime() - msgDay.getTime()) / 86400000);
|
|
if (diffDays === 0)
|
|
return 'Today';
|
|
if (diffDays === 1)
|
|
return 'Yesterday';
|
|
if (diffDays < 7)
|
|
return d.toLocaleDateString([], { weekday: 'long' });
|
|
return d.toLocaleDateString([], {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
year: d.getFullYear() !== now.getFullYear() ? 'numeric' : undefined,
|
|
});
|
|
}
|
|
export function getInitials(name) {
|
|
return name
|
|
.split(/[\s]+/)
|
|
.filter(Boolean)
|
|
.slice(0, 2)
|
|
.map((w) => w[0])
|
|
.join('')
|
|
.toUpperCase();
|
|
}
|
|
export function formatFileSize(bytes) {
|
|
if (bytes < 1024)
|
|
return bytes + ' B';
|
|
if (bytes < 1048576)
|
|
return (bytes / 1024).toFixed(1) + ' KB';
|
|
return (bytes / 1048576).toFixed(1) + ' MB';
|
|
}
|
|
export function defaultAttachmentUrl(filename) {
|
|
return '/api/attachment?path=' + encodeURIComponent(filename);
|
|
}
|
|
//# sourceMappingURL=format.js.map
|