Initial Commit
This commit is contained in:
71
src/utils/index.ts
Normal file
71
src/utils/index.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
// Utility functions for the Beach Litter Mapping Platform
|
||||
|
||||
/**
|
||||
* Validates if coordinates are within Ireland's geographical bounds
|
||||
*/
|
||||
export function isValidIrishCoordinates(lat: number, lng: number): boolean {
|
||||
// Ireland approximate bounds
|
||||
const MIN_LAT = 51.4;
|
||||
const MAX_LAT = 55.4;
|
||||
const MIN_LNG = -10.5;
|
||||
const MAX_LNG = -5.5;
|
||||
|
||||
return lat >= MIN_LAT && lat <= MAX_LAT && lng >= MIN_LNG && lng <= MAX_LNG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a date to ISO 8601 format
|
||||
*/
|
||||
export function formatDateISO(date: Date): string {
|
||||
return date.toISOString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates distance between two coordinates in meters using Haversine formula
|
||||
*/
|
||||
export function calculateDistance(
|
||||
lat1: number,
|
||||
lng1: number,
|
||||
lat2: number,
|
||||
lng2: number
|
||||
): number {
|
||||
const R = 6371e3; // Earth's radius in meters
|
||||
const φ1 = (lat1 * Math.PI) / 180;
|
||||
const φ2 = (lat2 * Math.PI) / 180;
|
||||
const Δφ = ((lat2 - lat1) * Math.PI) / 180;
|
||||
const Δλ = ((lng2 - lng1) * Math.PI) / 180;
|
||||
|
||||
const a =
|
||||
Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
|
||||
Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
|
||||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||||
|
||||
return R * c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates file type for photo uploads
|
||||
*/
|
||||
export function isValidImageFile(file: File): boolean {
|
||||
const validTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
|
||||
return validTypes.includes(file.type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates file size (max 5MB)
|
||||
*/
|
||||
export function isValidFileSize(file: File, maxSizeMB: number = 5): boolean {
|
||||
const maxSizeBytes = maxSizeMB * 1024 * 1024;
|
||||
return file.size <= maxSizeBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats bytes to human-readable size
|
||||
*/
|
||||
export function formatFileSize(bytes: number): string {
|
||||
if (bytes === 0) return '0 Bytes';
|
||||
const k = 1024;
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
|
||||
}
|
||||
Reference in New Issue
Block a user