@azizbecha/strkit
Version:
strkit is a utility library offering a collection of essential string functions including validation, case conversion, truncation, and more. Ideal for both JavaScript and TypeScript developers to simplify string operations in their applications.
36 lines • 1.66 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = getDistanceBetweenPoints;
/**
* Calculates the distance between two geographic coordinates using the Haversine formula.
* @param point1 - First geographic coordinate with `lat` and `lon` properties.
* @param point2 - Second geographic coordinate with `lat` and `lon` properties.
* @returns The distance between the two coordinates in kilometers.
*
* @example
* getDistanceBetweenPoints({ lat: 51.5074, lon: 0.1278 }, { lat: 48.8566, lon: 2.3522 }); // 343.5
*/
function getDistanceBetweenPoints(point1, point2) {
const toRadians = (degrees) => degrees * (Math.PI / 180);
const R = 6371; // Radius of the Earth in kilometers
const dLat = toRadians(point2.lat - point1.lat);
const dLon = toRadians(point2.lon - point1.lon);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRadians(point1.lat)) *
Math.cos(toRadians(point2.lat)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // Distance in kilometers
}
});
//# sourceMappingURL=getDistanceBetweenPoints.js.map