-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtripState.js
More file actions
44 lines (41 loc) · 1.06 KB
/
tripState.js
File metadata and controls
44 lines (41 loc) · 1.06 KB
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
const trips = [];
module.exports = {
add: data => {
const index = index(data.id)
if (index >= 0) {
setData(index, data);
return;
}
trips.push({
id: data.id,
latitude: data.latitude,
longitude: data.longitude,
})
},
remove: data => {
if (trips.length <= 0) return null
const index = index(data.id)
if (index < 0) return;
return trips.splice(index, 1);
},
getData: id => {
if (trips.length <= 0) return null
const index = index(data.id)
if (index < 0) return;
return trips[index];
},
setData: data => {
setData(index(data.id), data)
}
}
function index(id) {
trips.forEach((value, index, array) => {
if (value.id === id) return index;
})
return -1;
}
function setData(index, data) {
if (trips.length <= 0 || index < 0) return;
trips[index].latitude = data.latitude;
trips[index].longitude = data.longitude;
}