-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveLocations.js
151 lines (133 loc) · 3.83 KB
/
saveLocations.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require('fs');
const _ = require('lodash'); // in case it's needed later
const geo = require('./geo');
/*
I: none
O: a location object holding a place name, latitude, and longitude
C: none
E: none
What this fn does: It reads location data from a file on the localhost and returns an object.
Relationship btwn inputs and outputs: There are no inputs.
*/
const fetchLocations = () => {
try {
return JSON.parse(fs.readFileSync('places.json'));
}
catch(error){
return [];
}
};
/*
I: a string (the name of the saved location you want to retrieve)
O: an object
C: none
E: the input is not saved to the local file
What this fn does: It retrieves one set of location data from localhost
Relationship btwn inputs and outputs: The output is parsed json data retrieved from localhost where the
name or nickname property matches the input.
*/
const fetchOneLocation = (placeName) => {
let places = fetchLocations();
let test = (item) => {
return item.name === placeName || item.nickname === placeName;
}
let place = places.filter(test);
if(place.length === 1) {
return place[0];
} else {
return null;
}
};
/*
I: geoData object, a string
O: data saved to a file
C: none
E: none
What this fn does: It checks the local file for duplicate entries, finding none, it saves the new entry.
Relationship btwn inputs and outputs: The file written is the location data with the nickname added to it.
Requires user to use "-a" switch and gets the geodata from that.
Fn is called from geoData fetch in app.js
*/
const addPlace = (location, nickname) => {
let places = fetchLocations();
let duplicates = [];
var duplicateTest = (item) => {
return (item.nickname === nickname || (item.lat === location.lat && item.lng === location.lng))
};
duplicates = places.filter(duplicateTest)
if (duplicates.length === 0) {
location.nickname = nickname;
location.default = false;
places.push(location);
fs.writeFileSync('./places.json', JSON.stringify(places, undefined, 2));
} else {
console.log("Duplicate location already saved.", duplicates[0]);
}
};
/*
I: a string
O: none
C: input must be a saved location
E: input location does not exist
What this fn does: It searches the saved locations for the input string, and finding it removes it.
Relationship btwn inputs and outputs: no outputs
*/
const deleteLocation = (placeName) => {
let places = fetchLocations();
let test =(item) => {
return (item.nickname !== placeName);
};
let filteredPlaces = places.filter(test);
fs.writeFileSync('./places.json', JSON.stringify(filteredPlaces, undefined, 2));
};
/*
I: a string (the nickname of the saved location you wish to set as default)
O: none
C:
E: input must be a nickName saved in places.json
What this fn does: It sets one saved location to default: true, and the rest to false
Relationship btwn inputs and outputs: no outputs
*/
const setDefaultLocation = (nickname) => {
let places = fetchLocations();
for (let i = 0; i < places.length; i++) {
let item = places[i];
if (item.nickname === nickname ) {
item.default = true;
} else {
item.default = false;
}
}
fs.writeFileSync('./places.json', JSON.stringify(places, undefined, 2));
};
const getDefaultLocation = () => {
let places = fetchLocations();
let test = (item) => {
return item.default === true;
};
return places.filter(test)[0];
};
const listLocations = () => {
let places = fetchLocations();
return places.sort(sortLocations)
};
const sortLocations = (a, b) => {
let textA = a.nickname.toUpperCase();
let textB = b.nickname.toUpperCase();
if (textA < textB) {
return -1;
} else if (textA > textB) {
return 1;
} else {
return 0;
}
};
module.exports = {
addPlace,
deleteLocation,
fetchLocations,
fetchOneLocation,
getDefaultLocation,
listLocations,
setDefaultLocation
};