UNPKG

@pranavstark/geo-distances

Version:

A geographical distance calculator. Calculates distance between two points using haversine formula.

41 lines (32 loc) 1.18 kB
'use strict' let lib = require('./lib'), colors = require('colors'), distances = [], message = ' cannot be empty'; let getDistances = (filepath, start, range, unit) => { let data = require(filepath), lat1 = start.latitude, lon1 = start.longitude; let log = ((index, type, message) => { if (type == 'error') { console.log(colors.red(`error: In data array ${index} object is empty`)) console.log(colors.red(`${type}: ${message}`)); process.exit() } }) let checkCoordinates = ((index, type, coordinate) => { if (coordinate.trim() === "" || coordinate === undefined) { log(index, 'error', type + message); } return coordinate; }) data.filter((obj, index) => { let lat2 = checkCoordinates( index, 'latitude', obj.latitude); let lon2 = checkCoordinates(index, 'longitude', obj.longitude); let distance = lib.distance(lat1, lon1, lat2, lon2, unit); if (distance <= range) { obj["distance"] = distance distances.push(obj); } }); return distances; } module.exports = getDistances;