@trap_stevo/ventry
Version:
The universal engine for creating, tracking, and evolving interactive content — from posts, comments, and likes to offers, auctions, events, and beyond. Define, extend, and analyze content objects in real time. Turn anything into user-driven content.
42 lines (41 loc) • 1.17 kB
JavaScript
;
function haversineKm(lat1, lon1, lat2, lon2) {
const R = 6371;
const toRad = x => x * Math.PI / 180;
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat / 2) ** 2 + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) ** 2;
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c;
}
;
function geoIncludes(ctx, rules) {
if (!Array.isArray(rules) || rules.length === 0) {
return false;
}
for (const r of rules) {
if (Array.isArray(r.countries) && r.countries.length > 0) {
if (!ctx || !r.countries.includes(ctx.country)) {
continue;
}
}
if (Array.isArray(r.regions) && r.regions.length > 0) {
if (!ctx || !ctx.region || !r.regions.includes(ctx.region)) {
continue;
}
}
if (typeof r.radiusKm === "number" && r.center && typeof ctx?.lat === "number" && typeof ctx?.lon === "number") {
const d = haversineKm(ctx.lat, ctx.lon, r.center.lat, r.center.lon);
if (d > r.radiusKm) {
continue;
}
}
return true;
}
return false;
}
;
module.exports = {
geoIncludes,
haversineKm
};