turash/bugulma/frontend/lib/utils.ts
Damir Mukimov 6347f42e20
Consolidate repositories: Remove nested frontend .git and merge into main repository
- Remove nested git repository from bugulma/frontend/.git
- Add all frontend files to main repository tracking
- Convert from separate frontend/backend repos to unified monorepo
- Preserve all frontend code and development history as tracked files
- Eliminate nested repository complexity for simpler development workflow

This creates a proper monorepo structure with frontend and backend
coexisting in the same repository for easier development and deployment.
2025-11-25 06:02:57 +01:00

71 lines
2.3 KiB
TypeScript

/**
* Converts a File object to a base64 encoded string for Gemini API.
* @param file - The file to convert.
* @returns A promise that resolves to a Gemini-compatible `inlineData` object.
*/
export const fileToGenerativePart = async (file: File) => {
const base64EncodedDataPromise = new Promise<string>((resolve) => {
const reader = new FileReader();
reader.onloadend = () => resolve((reader.result as string).split(',')[1]);
reader.readAsDataURL(file);
});
return {
inlineData: {
data: await base64EncodedDataPromise,
mimeType: file.type,
},
};
};
/**
* Calculates the distance between two geographical points in meters using the Haversine formula.
* @param coords1 - The first set of coordinates { lat, lng }.
* @param coords2 - The second set of coordinates { lat, lng }.
* @returns The distance in meters.
*/
export const getDistance = (
coords1: { lat: number; lng: number },
coords2: { lat: number; lng: number }
): number => {
const R = 6371e3; // Earth's radius in metres
const φ1 = (coords1.lat * Math.PI) / 180;
const φ2 = (coords2.lat * Math.PI) / 180;
const Δφ = ((coords2.lat - coords1.lat) * Math.PI) / 180;
const Δλ = ((coords2.lng - coords1.lng) * 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;
};
/**
* Converts a File object to a data URL string.
* @param file The file to convert.
* @returns A promise that resolves with the data URL.
*/
export const fileToDataUrl = (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result as string);
reader.onerror = reject;
reader.readAsDataURL(file);
});
};
/**
* Parses a data URL into its base64 content and MIME type.
* @param dataUrl The data URL string.
* @returns An object with base64 and mimeType, or null if parsing fails.
*/
export const dataUrlToParts = (dataUrl: string): { base64: string; mimeType: string } | null => {
const match = dataUrl.match(/^data:(.+);base64,(.+)$/);
if (!match) return null;
return {
mimeType: match[1],
base64: match[2],
};
};