Skip to content

Commit 0aba6e1

Browse files
committed
Fix eslint error
1 parent 00a1480 commit 0aba6e1

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

0x05-Node_JS_basic/3-read_file_async.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,37 @@ const fs = require('fs');
55
* @param {String} path - The path to the CSV data file.
66
* @returns {Promise<void>}
77
*/
8-
const countStudents = (path) =>
9-
new Promise((resolve, reject) => {
10-
fs.readFile(path, 'utf-8', (err, data) => {
11-
if (err) {
12-
reject(new Error('Cannot load the database'));
13-
}
14-
if (data) {
15-
const fileLines = data.toString('utf-8').trim().split('\n');
16-
const stuObj = {};
17-
const fieldNames = fileLines[0].split(',');
18-
const stuPropNames = fieldNames.slice(0, fieldNames.length - 1);
8+
const countStudents = (path) => new Promise((resolve, reject) => {
9+
fs.readFile(path, 'utf-8', (err, data) => {
10+
if (err) {
11+
reject(new Error('Cannot load the database'));
12+
}
13+
if (data) {
14+
const fileLines = data.toString('utf-8').trim().split('\n');
15+
const stuObj = {};
16+
const fieldNames = fileLines[0].split(',');
17+
const stuPropNames = fieldNames.slice(0, fieldNames.length - 1);
1918

20-
for (const line of fileLines.slice(1)) {
21-
const studentRow = line.split(',');
22-
const studentPropValues = studentRow.slice(0, studentRow.length - 1);
23-
const field = studentRow[studentRow.length - 1];
24-
if (!Object.keys(stuObj).includes(field)) {
25-
stuObj[field] = [];
26-
}
27-
const stuEntries = stuPropNames.map((propName, idx) => [propName, studentPropValues[idx]]);
28-
stuObj[field].push(Object.fromEntries(stuEntries));
19+
for (const line of fileLines.slice(1)) {
20+
const studentRow = line.split(',');
21+
const studentPropValues = studentRow.slice(0, studentRow.length - 1);
22+
const field = studentRow[studentRow.length - 1];
23+
if (!Object.keys(stuObj).includes(field)) {
24+
stuObj[field] = [];
2925
}
26+
const stuEntries = stuPropNames.map((propName, idx) => [propName, studentPropValues[idx]]);
27+
stuObj[field].push(Object.fromEntries(stuEntries));
28+
}
3029

31-
const total = Object.values(stuObj).reduce((pre, cur) => (pre || []).length + cur.length);
32-
console.log(`Number of students: ${total}`);
33-
for (const [field, group] of Object.entries(stuObj)) {
34-
const studentNames = group.map((student) => student.firstname).join(', ');
35-
console.log(`Number of students in ${field}: ${group.length}. List: ${studentNames}`);
36-
}
37-
resolve(true);
30+
const total = Object.values(stuObj).reduce((pre, cur) => (pre || []).length + cur.length);
31+
console.log(`Number of students: ${total}`);
32+
for (const [field, group] of Object.entries(stuObj)) {
33+
const studentNames = group.map((student) => student.firstname).join(', ');
34+
console.log(`Number of students in ${field}: ${group.length}. List: ${studentNames}`);
3835
}
39-
});
36+
resolve(true);
37+
}
4038
});
39+
});
4140

4241
module.exports = countStudents;

0 commit comments

Comments
 (0)