@xec-sh/cli
Version:
Xec: The Universal Shell for TypeScript
79 lines • 2.42 kB
JavaScript
export function formatBytes(bytes) {
if (bytes === 0)
return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
}
export function formatRelativeTime(date) {
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHour = Math.floor(diffMin / 60);
const diffDay = Math.floor(diffHour / 24);
const diffWeek = Math.floor(diffDay / 7);
const diffMonth = Math.floor(diffDay / 30);
const diffYear = Math.floor(diffDay / 365);
if (diffYear > 0) {
return `${diffYear} year${diffYear > 1 ? 's' : ''} ago`;
}
else if (diffMonth > 0) {
return `${diffMonth} month${diffMonth > 1 ? 's' : ''} ago`;
}
else if (diffWeek > 0) {
return `${diffWeek} week${diffWeek > 1 ? 's' : ''} ago`;
}
else if (diffDay > 0) {
return `${diffDay} day${diffDay > 1 ? 's' : ''} ago`;
}
else if (diffHour > 0) {
return `${diffHour} hour${diffHour > 1 ? 's' : ''} ago`;
}
else if (diffMin > 0) {
return `${diffMin} minute${diffMin > 1 ? 's' : ''} ago`;
}
else if (diffSec > 0) {
return `${diffSec} second${diffSec > 1 ? 's' : ''} ago`;
}
else {
return 'just now';
}
}
export function formatDuration(ms) {
if (ms < 1000) {
return `${ms}ms`;
}
const sec = Math.floor(ms / 1000);
const min = Math.floor(sec / 60);
const hour = Math.floor(min / 60);
if (hour > 0) {
const remainingMin = min % 60;
return `${hour}h${remainingMin > 0 ? ` ${remainingMin}m` : ''}`;
}
else if (min > 0) {
const remainingSec = sec % 60;
return `${min}m${remainingSec > 0 ? ` ${remainingSec}s` : ''}`;
}
else {
return `${sec}s`;
}
}
export function truncate(str, maxLength) {
if (str.length <= maxLength)
return str;
return str.substring(0, maxLength - 3) + '...';
}
export function formatError(error) {
if (error instanceof Error) {
return error.message;
}
else if (typeof error === 'string') {
return error;
}
else {
return String(error);
}
}
//# sourceMappingURL=formatters.js.map