forked from MLH/mlh-localhost-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
49 lines (42 loc) · 1.39 KB
/
storage.js
File metadata and controls
49 lines (42 loc) · 1.39 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
45
46
47
48
49
const fs = require("fs");
const LOCATIONS_PATH = "locations.txt";
const COORDINATES_PATH = "coordinates.json";
const getLocations = () => {
return fs.promises
.readFile(LOCATIONS_PATH)
.then((fileContent) => String(fileContent).split("\n"));
}
const getNewLocations = () => {
return Promise.all([
fs.promises.readFile(LOCATIONS_PATH),
fs.promises.readFile(COORDINATES_PATH)
]).then(([locations, coordinates]) => {
const locationLength = String(locations).replace(/\n+$/,"").split("\n").length;
const coordinateLength = String(coordinates).replace(/\n+$/,"").split("\n").length;
console.log({locationLength, coordinateLength});
return String(locations).split("\n").slice(coordinateLength);
});
}
const getCoordinates = () => {
return fs.promises
.readFile(COORDINATES_PATH)
.then((fileContent) => JSON.parse(fileContent));
}
const saveNewCoordinates = (newCoordinates) => {
return getCoordinates().then((currentCoordinates) => {
console.log({newCoordinates});
const coordinates = currentCoordinates.concat(newCoordinates);
coordinatesContent = JSON.stringify(coordinates).replace(/\}/g, "}\n");
return fs.promises.writeFile(COORDINATES_PATH, coordinatesContent);
})
};
const getExampleLocations = function() {
return [];
};
module.exports = {
getLocations,
getNewLocations,
getCoordinates,
saveNewCoordinates,
getExampleLocations
};