@@ -5,38 +5,37 @@ const fs = require('fs');
5
5
* @param {String } path - The path to the CSV data file.
6
6
* @returns {Promise<void> }
7
7
*/
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 ) ;
19
18
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 ] = [ ] ;
29
25
}
26
+ const stuEntries = stuPropNames . map ( ( propName , idx ) => [ propName , studentPropValues [ idx ] ] ) ;
27
+ stuObj [ field ] . push ( Object . fromEntries ( stuEntries ) ) ;
28
+ }
30
29
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 } ` ) ;
38
35
}
39
- } ) ;
36
+ resolve ( true ) ;
37
+ }
40
38
} ) ;
39
+ } ) ;
41
40
42
41
module . exports = countStudents ;
0 commit comments