Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
103 lines (90 loc) · 3.19 KB
/
Copy pathindex.ts
File metadata and controls
103 lines (90 loc) · 3.19 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
export function formatTime(date: Date): string {
const now = new Date();
const diff = Math.floor((now.getTime() - date.getTime()) / 1000);
if (diff < 60) return 'Just now';
if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
return `${Math.floor(diff / 86400)}d ago`;
}
export function formatPrice(price: number): string {
if (price >= 1000) {
return `$${price.toLocaleString(undefined, {
minimumFractionDigits: 0,
maximumFractionDigits: 0,
})}`;
}
return `$${price.toLocaleString(undefined, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}`;
}
export function formatChange(change: number): string {
const sign = change >= 0 ? '+' : '';
return `${sign}${change.toFixed(2)}%`;
}
export function getChangeClass(change: number): string {
return change >= 0 ? 'up' : 'down';
}
export function getHeatmapClass(change: number): string {
const abs = Math.abs(change);
const direction = change >= 0 ? 'up' : 'down';
if (abs >= 2) return `${direction}-3`;
if (abs >= 1) return `${direction}-2`;
return `${direction}-1`;
}
export function debounce<T extends (...args: unknown[]) => void>(
fn: T,
delay: number
): (...args: Parameters<T>) => void {
let timeoutId: ReturnType<typeof setTimeout>;
return (...args: Parameters<T>) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn(...args), delay);
};
}
export function loadFromStorage<T>(key: string, defaultValue: T): T {
try {
const stored = localStorage.getItem(key);
if (stored) {
const parsed = JSON.parse(stored) as T;
// Merge with defaults for object types to handle new properties
if (typeof defaultValue === 'object' && defaultValue !== null && !Array.isArray(defaultValue)) {
return { ...defaultValue, ...parsed };
}
return parsed;
}
} catch (e) {
console.warn(`Failed to load ${key} from storage:`, e);
}
return defaultValue;
}
export function saveToStorage<T>(key: string, value: T): void {
try {
localStorage.setItem(key, JSON.stringify(value));
} catch (e) {
console.warn(`Failed to save ${key} to storage:`, e);
}
}
export function generateId(): string {
return `id-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
}
export function isMobileDevice(): boolean {
const isMobileWidth = window.innerWidth < 768;
const isTouchDevice = window.matchMedia('(pointer: coarse)').matches;
return isMobileWidth || isTouchDevice;
}
export function chunkArray<T>(items: T[], size: number): T[][] {
const chunkSize = Math.max(1, size);
const chunks: T[][] = [];
for (let i = 0; i < items.length; i += chunkSize) {
chunks.push(items.slice(i, i + chunkSize));
}
return chunks;
}
export { proxyUrl, fetchWithProxy } from './proxy';
export { exportToJSON, exportToCSV, ExportPanel } from './export';
export { buildMapUrl, parseMapUrlState } from './urlState';
export type { ParsedMapUrlState } from './urlState';
export { CircuitBreaker, createCircuitBreaker, getCircuitBreakerStatus, getCircuitBreakerCooldownInfo } from './circuit-breaker';
export type { CircuitBreakerOptions } from './circuit-breaker';
export * from './analysis-constants';