|
1 |
| -const fs = require('fs'); |
| 1 | +const fs = require('fs/promises'); |
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * 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>} |
7 | 7 | */
|
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 | + }); |
22 | 23 |
|
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}`); |
35 | 26 |
|
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}`); |
45 | 30 | }
|
46 |
| - }); |
47 |
| -}); |
| 31 | + |
| 32 | + return true; |
| 33 | + } catch (error) { |
| 34 | + console.error('Cannot load the database'); |
| 35 | + throw error; |
| 36 | + } |
| 37 | +}; |
48 | 38 |
|
49 | 39 | module.exports = countStudents;
|
0 commit comments