-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
80 lines (69 loc) · 2.42 KB
/
Copy pathutils.ts
File metadata and controls
80 lines (69 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { clsx, type ClassValue } from 'clsx';
import { CandlestickData, Time } from 'lightweight-charts';
import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
export function formatPrice(
value: number | null | undefined,
digits?: number,
currency?: string,
showSymbol?: boolean,
) {
if (value === null || value === undefined || isNaN(value)) {
return showSymbol !== false ? '$0.00' : '0.00';
}
if (showSymbol === undefined || showSymbol === true) {
return value.toLocaleString(undefined, {
style: 'currency',
currency: currency?.toUpperCase() || 'USD',
minimumFractionDigits: digits ?? 2,
maximumFractionDigits: digits ?? 2,
});
}
return value.toLocaleString(undefined, {
minimumFractionDigits: digits ?? 2,
maximumFractionDigits: digits ?? 2,
});
}
export function formatPercentage(change: number | null | undefined): string {
if (change === null || change === undefined || isNaN(change)) {
return '0.0%';
}
const formattedChange = change.toFixed(1);
return `${formattedChange}%`;
}
export function trendingClasses(value: number) {
const isTrendingUp = value > 0;
return {
textClass: isTrendingUp ? 'text-green-400' : 'text-red-400',
bgClass: isTrendingUp ? 'bg-green-500/10' : 'bg-red-500/10',
iconClass: isTrendingUp ? 'icon-up' : 'icon-down',
};
}
export const convertOHLCData = (rawData: OHLCData[]): CandlestickData[] => {
return rawData.map(([timestampMs, open, high, low, close]) => ({
time: Math.floor(timestampMs / 1000) as Time,
open: Number(open),
high: Number(high),
low: Number(low),
close: Number(close),
}));
};
export function timeAgo(date: string | number | Date): string {
const now = new Date();
const past = new Date(date);
const diff = now.getTime() - past.getTime(); // difference in ms
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const weeks = Math.floor(days / 7);
if (seconds < 60) return 'just now';
if (minutes < 60) return `${minutes} min`;
if (hours < 24) return `${hours} hour${hours > 1 ? 's' : ''}`;
if (days < 7) return `${days} day${days > 1 ? 's' : ''}`;
if (weeks < 4) return `${weeks} week${weeks > 1 ? 's' : ''}`;
// Format date as YYYY-MM-DD
return past.toISOString().split('T')[0];
}