-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (54 loc) · 1.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const Distance = require('./lib/search_algo');
const QuickSort = require('./lib/quickSort');
class Search {
constructor(users,range,destination){
if(!(users instanceof Array)) return new Error('Invalid data set')
this.users = users; // Array of users Object
this.range = range; //in KM
this.destination = destination; // { latitude : 12.341 , longitude: 89.788 }
this.validUser = [];
this.validate();
}
validate(){
let tmp = []
this.users.forEach( val => {
try{
val = JSON.parse(val)
}catch (e){
}
let r = this.validateData(val);
if(r)
tmp.push(r)
})
this.users = tmp;
tmp = undefined;
}
validateData(d){
let lat = d['latitude'] && typeof d['latitude'] === 'string' && d['latitude'].length > 0 ? Number(d['latitude']) : null;
let lon = d['longitude'] && typeof d['longitude'] === 'string' && d['longitude'].length > 0 ? Number(d['longitude']) : null;
let uId = d['user_id'] && typeof d['user_id'] === 'number' ? d['user_id'] : null;
let name = d['name'] && typeof d['name'] == 'string' ? d['name'] : null;
if(lat && lon && uId && name){
return {
latitude : lat,
longitude : lon,
user_id : uId,
name : name
}
} else{
console.log('Invalid data encounter , Hence Skipping')
return null;
}
}
filter(){
this.users.forEach((val) => {
let distance = new Distance(val.latitude,val.longitude,this.destination)
if(distance.findDistance() <= this.range){
this.validUser.push({user_id:val.user_id,name:val.name})
}
})
let finalResult = new QuickSort();
return finalResult.qSortMain(this.validUser,0,this.validUser.length - 1,this.property);
}
}
module.exports = Search;