-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-model.js
45 lines (39 loc) · 1.12 KB
/
data-model.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
// A library that interprets files, like our .json
const fs = require("fs");
// Returns all restaurants
function getRestaurants() {
return new Promise((resolve, reject) => {
fs.readFile("restaurant_data.json", "utf8", (err, data) => {
if (err) {
reject(err);
} else {
const json = JSON.parse(data);
resolve(json);
}
});
});
}
// Returns all zip codes (so we can compare a user's input)
async function getZips() {
// Load all the restaurants
const data = await getRestaurants();
return data.Restaurants.map((restaurant) => `${restaurant.Zip_Code}`);
}
// Return all restaurants within a given zip code
async function getZipRestaurants(zip) {
// Load all the restaurants
const data = await getRestaurants();
// Filter through all restaurants, finding ones in the zip
const zipRestaurants = data.Restaurants.filter(
(restaurant) => restaurant.Zip_Code == zip
);
// Return a formatted list
return zipRestaurants.map(
(restaurant) => `${restaurant.Name}: ${restaurant.Phone}\n\n`
);
}
module.exports = {
getRestaurants,
getZips,
getZipRestaurants
};