salat-first
Version:
Islamic prayer times calculation with special support for Moroccan methods and Maliki madhab
76 lines (75 loc) • 3.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.COMMON_LOCATIONS = void 0;
exports.createCoordinates = createCoordinates;
exports.findNearestLocation = findNearestLocation;
exports.calculateDistance = calculateDistance;
const coordinates_1 = require("../core/coordinates");
const validation_1 = require("./validation");
/**
* Common locations with their coordinates
*/
exports.COMMON_LOCATIONS = {
MAKKAH: new coordinates_1.Coordinates(21.4225241, 39.8261818),
MADINAH: new coordinates_1.Coordinates(24.5246542, 39.5691841),
CASABLANCA: new coordinates_1.Coordinates(33.5731, -7.5898),
RABAT: new coordinates_1.Coordinates(34.0209, -6.8416),
FEZ: new coordinates_1.Coordinates(34.0181, -5.0078),
MARRAKECH: new coordinates_1.Coordinates(31.6295, -7.9811),
RIYADH: new coordinates_1.Coordinates(24.7136, 46.6753),
ISTANBUL: new coordinates_1.Coordinates(41.0082, 28.9784),
CAIRO: new coordinates_1.Coordinates(30.0444, 31.2357),
DUBAI: new coordinates_1.Coordinates(25.2048, 55.2708),
LONDON: new coordinates_1.Coordinates(51.5074, -0.1278),
NEW_YORK: new coordinates_1.Coordinates(40.7128, -74.006),
PARIS: new coordinates_1.Coordinates(48.8566, 2.3522),
TOKYO: new coordinates_1.Coordinates(35.6762, 139.6503),
SYDNEY: new coordinates_1.Coordinates(-33.8688, 151.2093),
};
/**
* Creates a new coordinates object with validation
* @param latitude The latitude in decimal degrees
* @param longitude The longitude in decimal degrees
* @returns The validated coordinates
*/
function createCoordinates(latitude, longitude) {
(0, validation_1.validateLatitude)(latitude);
(0, validation_1.validateLongitude)(longitude);
return new coordinates_1.Coordinates(latitude, longitude);
}
/**
* Finds the nearest common location to given coordinates
* @param coordinates The coordinates to find the nearest location for
* @returns The name of the nearest common location and the distance in kilometers
*/
function findNearestLocation(coordinates) {
let nearestName = "";
let nearestDistance = Infinity;
for (const [name, location] of Object.entries(exports.COMMON_LOCATIONS)) {
const distance = calculateDistance(coordinates, location);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestName = name;
}
}
return { name: nearestName, distance: nearestDistance };
}
/**
* Calculates the distance between two coordinates using the Haversine formula
* @param coords1 The first coordinates
* @param coords2 The second coordinates
* @returns The distance in kilometers
*/
function calculateDistance(coords1, coords2) {
const R = 6371; // Earth's radius in kilometers
const dLat = ((coords2.latitude - coords1.latitude) * Math.PI) / 180;
const dLon = ((coords2.longitude - coords1.longitude) * Math.PI) / 180;
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos((coords1.latitude * Math.PI) / 180) *
Math.cos((coords2.latitude * Math.PI) / 180) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
const distance = R * c;
return distance;
}