expressjs -> nextjs
This commit is contained in:
70
nextjs/src/lib/format.js
Normal file
70
nextjs/src/lib/format.js
Normal file
@@ -0,0 +1,70 @@
|
||||
export function formatBytes(bytes) {
|
||||
const size = Number(bytes) || 0;
|
||||
if (size < 1024) {
|
||||
return `${size} B`;
|
||||
}
|
||||
|
||||
const units = ['KB', 'MB', 'GB', 'TB'];
|
||||
let value = size / 1024;
|
||||
let unitIndex = 0;
|
||||
|
||||
while (value >= 1024 && unitIndex < units.length - 1) {
|
||||
value /= 1024;
|
||||
unitIndex += 1;
|
||||
}
|
||||
|
||||
return `${value.toFixed(value < 10 ? 1 : 0)} ${units[unitIndex]}`;
|
||||
}
|
||||
|
||||
export function formatTimestamp(timestamp) {
|
||||
const value = Number(timestamp);
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
return '-';
|
||||
}
|
||||
return new Date(value).toLocaleString('de-DE');
|
||||
}
|
||||
|
||||
export function formatCountdown(timestamp) {
|
||||
const expiresAt = Number(timestamp) || 0;
|
||||
const deltaMinutes = Math.floor(Math.max(0, expiresAt - Date.now()) / 60_000);
|
||||
const hours = Math.floor(deltaMinutes / 60);
|
||||
const days = Math.floor(hours / 24);
|
||||
|
||||
if (days > 0) {
|
||||
return `${days}d ${hours % 24}h`;
|
||||
}
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${deltaMinutes % 60}m`;
|
||||
}
|
||||
return `${deltaMinutes}m`;
|
||||
}
|
||||
|
||||
export function parseLogDetail(detailText) {
|
||||
if (!detailText) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(detailText);
|
||||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
||||
return [{ key: 'detail', value: String(detailText) }];
|
||||
}
|
||||
return Object.entries(parsed).map(([key, value]) => ({
|
||||
key,
|
||||
value: String(value),
|
||||
}));
|
||||
} catch {
|
||||
return [{ key: 'detail', value: String(detailText) }];
|
||||
}
|
||||
}
|
||||
|
||||
export function readSearchParam(searchParams, key) {
|
||||
const rawValue = searchParams?.[key];
|
||||
if (Array.isArray(rawValue)) {
|
||||
return String(rawValue[0] || '');
|
||||
}
|
||||
if (typeof rawValue === 'string') {
|
||||
return rawValue;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
Reference in New Issue
Block a user