Skip to content

Commit 00a1480

Browse files
committed
Fix eslint error
1 parent 7ec31fb commit 00a1480

File tree

5 files changed

+39
-43
lines changed

5 files changed

+39
-43
lines changed

0x05-Node_JS_basic/3-read_file_async.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ const countStudents = (path) =>
1313
}
1414
if (data) {
1515
const fileLines = data.toString('utf-8').trim().split('\n');
16-
const studentObj = {};
16+
const stuObj = {};
1717
const fieldNames = fileLines[0].split(',');
18-
const studentPropNames = fieldNames.slice(0, fieldNames.length - 1);
18+
const stuPropNames = fieldNames.slice(0, fieldNames.length - 1);
1919

2020
for (const line of fileLines.slice(1)) {
2121
const studentRow = line.split(',');
2222
const studentPropValues = studentRow.slice(0, studentRow.length - 1);
2323
const field = studentRow[studentRow.length - 1];
24-
if (!Object.keys(studentObj).includes(field)) {
25-
studentObj[field] = [];
24+
if (!Object.keys(stuObj).includes(field)) {
25+
stuObj[field] = [];
2626
}
27-
const studentEntries = studentPropNames.map((propName, idx) => [propName, studentPropValues[idx]]);
28-
studentObj[field].push(Object.fromEntries(studentEntries));
27+
const stuEntries = stuPropNames.map((propName, idx) => [propName, studentPropValues[idx]]);
28+
stuObj[field].push(Object.fromEntries(stuEntries));
2929
}
3030

31-
const totalStudents = Object.values(studentObj).reduce((pre, cur) => (pre || []).length + cur.length);
32-
console.log(`Number of students: ${totalStudents}`);
33-
for (const [field, group] of Object.entries(studentObj)) {
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)) {
3434
const studentNames = group.map((student) => student.firstname).join(', ');
3535
console.log(`Number of students in ${field}: ${group.length}. List: ${studentNames}`);
3636
}

0x05-Node_JS_basic/5-http.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const http = require('http');
2-
const fs = require('fs');
32
const countStudents = require('./3-read_file_async');
43

54
const PORT = 1245;

0x05-Node_JS_basic/6-http_express.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const express = require('express');
2+
23
const app = express();
34

45
const PORT = 1245;

0x05-Node_JS_basic/7-http_express.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const express = require('express');
22
const countStudents = require('./3-read_file_async');
3+
34
const app = express();
45

56
const PORT = 1245;

0x05-Node_JS_basic/full_server/utils.js

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,37 @@ import fs from 'fs';
88
* String: {firstname: String, lastname: String, age: number}[]
99
* }>}
1010
*/
11-
const readDatabase = (dataPath) => new Promise((resolve, reject) => {
12-
if (!dataPath) {
13-
reject(new Error('Cannot load the database'));
14-
}
15-
if (dataPath) {
16-
fs.readFile(dataPath, (err, data) => {
17-
if (err) {
18-
reject(new Error('Cannot load the database'));
19-
}
20-
if (data) {
21-
const fileLines = data
22-
.toString('utf-8')
23-
.trim()
24-
.split('\n');
25-
const studentGroups = {};
26-
const dbFieldNames = fileLines[0].split(',');
27-
const studentPropNames = dbFieldNames
28-
.slice(0, dbFieldNames.length - 1);
11+
const readDatabase = (dataPath) =>
12+
new Promise((resolve, reject) => {
13+
if (!dataPath) {
14+
reject(new Error('Cannot load the database'));
15+
}
16+
if (dataPath) {
17+
fs.readFile(dataPath, (err, data) => {
18+
if (err) {
19+
reject(new Error('Cannot load the database'));
20+
}
21+
if (data) {
22+
const fileLines = data.toString('utf-8').trim().split('\n');
23+
const studentGroups = {};
24+
const dbFieldNames = fileLines[0].split(',');
25+
const stuPropNames = dbFieldNames.slice(0, dbFieldNames.length - 1);
2926

30-
for (const line of fileLines.slice(1)) {
31-
const studentRecord = line.split(',');
32-
const studentPropValues = studentRecord
33-
.slice(0, studentRecord.length - 1);
34-
const field = studentRecord[studentRecord.length - 1];
35-
if (!Object.keys(studentGroups).includes(field)) {
36-
studentGroups[field] = [];
27+
for (const line of fileLines.slice(1)) {
28+
const studentRecord = line.split(',');
29+
const studentPropValues = studentRecord.slice(0, studentRecord.length - 1);
30+
const field = studentRecord[studentRecord.length - 1];
31+
if (!Object.keys(studentGroups).includes(field)) {
32+
studentGroups[field] = [];
33+
}
34+
const studentEntries = stuPropNames.map((propName, idx) => [propName, studentPropValues[idx]]);
35+
studentGroups[field].push(Object.fromEntries(studentEntries));
3736
}
38-
const studentEntries = studentPropNames
39-
.map((propName, idx) => [propName, studentPropValues[idx]]);
40-
studentGroups[field].push(Object.fromEntries(studentEntries));
37+
resolve(studentGroups);
4138
}
42-
resolve(studentGroups);
43-
}
44-
});
45-
}
46-
});
39+
});
40+
}
41+
});
4742

4843
export default readDatabase;
4944
module.exports = readDatabase;

0 commit comments

Comments
 (0)