UNPKG

biz2credit-code-challenge-gaurav-pandey-3

Version:

Code Challenge for Gaurav Pandey

52 lines (44 loc) 1.68 kB
/* * This API will provide distance based information */ const Biz2CreditAPI = (range, sLat, sLong) => { // As per design guidelines, all hardcoded values should be fetched from config files const RADIUS_OF_EARTH = 6371; // since there is only one hardcoded so written here /* @desc: This api will convert degree value to radian @param: degree (geographic degree) @return: radian */ const convertDeg2Rad = degree => { if (!degree) return 0; return degree * (Math.PI / 180); }; /* @desc: This api will verify cordinates exists within range @param: range number @param: sourceCordinates object { lat, long } @param: d object { lat, long } @return: will return true/false based on lat & long exists in given range */ const isCordinatesWithInRange = (dLat, dLong) => { // validation input if (!range || !sLat || !sLong || !dLat || !dLong) return false; // converting degree into radian let rLat = convertDeg2Rad(dLat - sLat); // radian lattitude let rLong = convertDeg2Rad(dLong - sLong); // radian longitude /* As per Haversine formula */ let area = Math.sin(rLat / 2) * Math.sin(rLat / 2) + Math.cos(convertDeg2Rad(sLat)) * Math.cos(convertDeg2Rad(dLat)) * Math.sin(rLong / 2) * Math.sin(rLong / 2); // distance from source to dest const distance = RADIUS_OF_EARTH * (2 * Math.atan2(Math.sqrt(area), Math.sqrt(1 - area))); // truthy if distance in within range return distance <= range; }; /* Exposing APIs */ return { isCordinatesWithInRange }; }; /* Exporting module */ module.exports = Biz2CreditAPI;