Skip to content

Commit 7b83228

Browse files
committed
Add 3-read_file_async.js file
1 parent f10cdbc commit 7b83228

File tree

3 files changed

+32
-44
lines changed

3 files changed

+32
-44
lines changed

0x05-Node_JS_basic/1-stdin.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
process.stdin.setEncoding('utf8');
2-
3-
console.log('Welcome to Holberton School, what is your name?');
1+
process.stdout.write('Welcome to Holberton School, what is your name?\n');
42

53
process.stdin.on('readable', () => {
64
const name = process.stdin.read();

0x05-Node_JS_basic/2-read_file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function countStudents(path) {
1515
}
1616
const students = data.map((line) => line.split(','));
1717

18-
console.log(`Number of students: ${students.length - 1}`);
18+
console.log(`Number of students: ${students.slice(1).length - 1}`);
1919

2020
const fields = students[0];
2121
const fieldIndex = fields.indexOf('field');
Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,39 @@
1-
const fs = require('fs');
1+
const fs = require('fs/promises');
22

33
/**
44
* Counts the students in a CSV data file.
5-
* @param {String} dataPath The path to the CSV data file.
6-
* @author Bezaleel Olakunori <https://github.com/B3zaleel>
5+
* @param {String} path - The path to the CSV data file.
6+
* @returns {Promise<void>}
77
*/
8-
const countStudents = (dataPath) => new Promise((resolve, reject) => {
9-
fs.readFile(dataPath, 'utf-8', (err, data) => {
10-
if (err) {
11-
reject(new Error('Cannot load the database'));
12-
}
13-
if (data) {
14-
const fileLines = data
15-
.toString('utf-8')
16-
.trim()
17-
.split('\n');
18-
const studentGroups = {};
19-
const dbFieldNames = fileLines[0].split(',');
20-
const studentPropNames = dbFieldNames
21-
.slice(0, dbFieldNames.length - 1);
8+
const countStudents = async (path) => {
9+
try {
10+
const data = await fs.readFile(path, 'utf-8');
11+
const fileLines = data.trim().split('\n');
12+
const studentGroups = {};
13+
const dbFieldNames = fileLines[0].split(',');
14+
const studentPropNames = dbFieldNames.slice(0, -1);
15+
16+
fileLines.slice(1).forEach((line) => {
17+
const [field, ...studentPropValues] = line.split(',');
18+
studentGroups[field] = [
19+
...(studentGroups[field] || []),
20+
Object.fromEntries(studentPropNames.map((propName, idx) => [propName, studentPropValues[idx]])),
21+
];
22+
});
2223

23-
for (const line of fileLines.slice(1)) {
24-
const studentRecord = line.split(',');
25-
const studentPropValues = studentRecord
26-
.slice(0, studentRecord.length - 1);
27-
const field = studentRecord[studentRecord.length - 1];
28-
if (!Object.keys(studentGroups).includes(field)) {
29-
studentGroups[field] = [];
30-
}
31-
const studentEntries = studentPropNames
32-
.map((propName, idx) => [propName, studentPropValues[idx]]);
33-
studentGroups[field].push(Object.fromEntries(studentEntries));
34-
}
24+
const totalStudents = Object.values(studentGroups).flat().length;
25+
console.log(`Number of students: ${totalStudents}`);
3526

36-
const totalStudents = Object
37-
.values(studentGroups)
38-
.reduce((pre, cur) => (pre || []).length + cur.length);
39-
console.log(`Number of students: ${totalStudents}`);
40-
for (const [field, group] of Object.entries(studentGroups)) {
41-
const studentNames = group.map((student) => student.firstname).join(', ');
42-
console.log(`Number of students in ${field}: ${group.length}. List: ${studentNames}`);
43-
}
44-
resolve(true);
27+
for (const [field, group] of Object.entries(studentGroups)) {
28+
const studentNames = group.map((student) => student.firstname).join(', ');
29+
console.log(`Number of students in ${field}: ${group.length}. List: ${studentNames}`);
4530
}
46-
});
47-
});
31+
32+
return true;
33+
} catch (error) {
34+
console.error('Cannot load the database');
35+
throw error;
36+
}
37+
};
4838

4939
module.exports = countStudents;

0 commit comments

Comments
 (0)