nd-cust-geo
Version:
We have some customer records in a text file (customers.txt) -- one customer per line, JSON lines formatted. We want to invite any customer within 100km of Dublin for some food and drinks on us. Write a program that will read the full list of customers an
113 lines (100 loc) • 3.61 kB
JavaScript
var fs = require('fs');
var readline = require('readline');
module.exports = {
custParams : {
'lat': 'ORIGIN-LATITUDE',
'lon': 'ORIGIN-LONGITUDE',
'fileDest': 'CUSTOMERS-FILE-PATH',
'range':'FILTER-RANGE-KM',
'sortBy':'SORT-KEY'
},
setCustParams: function(lat, lon, fileDest,range,sort) {
this.custParams.lat = lat;
this.custParams.lon = lon;
this.custParams.fileDest = fileDest;
this.custParams.range = range;
this.custParams.sortBy=sort;
},
getcustomers: async function(sortByKey){
try{
if(this.custParams.lat=="" || this.custParams.lat==undefined){
throw new SyntaxError("Origin latitude is not found.");
}
if(this.custParams.lon=="" || this.custParams.lon==undefined){
throw new SyntaxError("Origin longitude is not found.");
}
if(this.custParams.fileDest=="" || this.custParams.fileDest==undefined){
throw new SyntaxError("File path is not found.");
}
if(this.custParams.range=="" || this.custParams.range==undefined){
throw new SyntaxError("Range is not found.");
}
if(this.custParams.sortBy=="" || this.custParams.sortBy==undefined){
throw new SyntaxError("sortBy key not found.");
}
}catch(e){
console.log( "Error: " + e.message );
return false;
}
return this.sortByKey(await this.getcustomersTxt(this),this.custParams.sortBy);
},
getcustomersTxt: function(params){
var customersArray =[];
return new Promise(function(res,rej){
var stats = fs.stat(params.custParams.fileDest, function(err,stat){
try{
if (!err) {
let readInterface = readline.createInterface({input: fs.createReadStream(params.custParams.fileDest)});
readInterface.on('line', function(line) {
let jsonObj = JSON.parse(line);
// get user distance
let dist = params.distance(params.custParams.lat, params.custParams.lon, jsonObj.latitude, jsonObj.longitude, 'K');
if(dist<params.custParams.range){
customersArray.push(jsonObj);
}
}).on('close', function() {
res(customersArray);
}).on('error', function (e) {
throw new SyntaxError(e);
});
}else{
throw new SyntaxError("File not found as you provided path.");
}
}catch(e){
if(e.name == "SyntaxError") {
console.log( "Error: " + e.message );
} else {
throw e;
}
}
});
});
},
distance:function(lat1, lon1, lat2, lon2, unit){
if ((lat1 == lat2) && (lon1 == lon2)) {
return 0;
}
else {
var radlat1 = Math.PI * lat1/180; //convert Degrees to Radians
var radlat2 = Math.PI * lat2/180; //convert Degrees to Radians
var theta = lon1-lon2;
var radtheta = Math.PI * theta/180;
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
if (dist > 1) {
dist = 1;
}
dist = Math.acos(dist);
dist = dist * 180/Math.PI;
dist = dist * 60 * 1.1515;
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist.toFixed(2);
}
},
sortByKey:function(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
};