-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
43 lines (35 loc) · 1.13 KB
/
functions.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
const fs = require('fs');
module.exports = {
// Read data from json file and show it to the console
show: file => {
fs.readFile(`./${file}`, (error, data) => {
if (error) {
console.log('Error occured in reading the file')
} else {
console.log(`data: ${data}`)
}
});
},
// Find user from a value of a particular attribute
findByAttribute: (file, attr, value, callback) => {
fs.readFile(`./${file}`, (error, data) => {
if (error) {
console.log('Error occured in reading the file')
} else {
for (user of JSON.parse(data).data)
if (user[attr] == value)
callback(user)
}
});
},
// Find user from the index in the array
findByIndex: (file, index, callback) => {
fs.readFile(`./${file}`, (error, data) => {
if (error) {
console.log('Error occured in reading the file')
} else {
callback(JSON.parse(data).data[index])
}
});
}
}