@@ -3,42 +3,40 @@ import fs from 'fs';
3
3
/**
4
4
* Reads the data of students in a CSV data file.
5
5
* @param {String } dataPath The path to the CSV data file.
6
- * @author Bezaleel Olakunori <https://github.com/B3zaleel>
7
6
* @returns {Promise<{
8
7
* String: {firstname: String, lastname: String, age: number}[]
9
8
* }> }
10
9
*/
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 ) ;
10
+ const readDatabase = ( dataPath ) => new Promise ( ( resolve , reject ) => {
11
+ if ( ! dataPath ) {
12
+ reject ( new Error ( 'Cannot load the database' ) ) ;
13
+ }
14
+ if ( dataPath ) {
15
+ fs . readFile ( dataPath , ( err , data ) => {
16
+ if ( err ) {
17
+ reject ( new Error ( 'Cannot load the database' ) ) ;
18
+ }
19
+ if ( data ) {
20
+ const fileLines = data . toString ( 'utf-8' ) . trim ( ) . split ( '\n' ) ;
21
+ const studentGroups = { } ;
22
+ const dbFieldNames = fileLines [ 0 ] . split ( ',' ) ;
23
+ const stuPropNames = dbFieldNames . slice ( 0 , dbFieldNames . length - 1 ) ;
26
24
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 ) ) ;
25
+ for ( const line of fileLines . slice ( 1 ) ) {
26
+ const studentRecord = line . split ( ',' ) ;
27
+ const stuPropValues = studentRecord . slice ( 0 , studentRecord . length - 1 ) ;
28
+ const field = studentRecord [ studentRecord . length - 1 ] ;
29
+ if ( ! Object . keys ( studentGroups ) . includes ( field ) ) {
30
+ studentGroups [ field ] = [ ] ;
36
31
}
37
- resolve ( studentGroups ) ;
32
+ const stuEntries = stuPropNames . map ( ( propName , idx ) => [ propName , stuPropValues [ idx ] ] ) ;
33
+ studentGroups [ field ] . push ( Object . fromEntries ( stuEntries ) ) ;
38
34
}
39
- } ) ;
40
- }
41
- } ) ;
35
+ resolve ( studentGroups ) ;
36
+ }
37
+ } ) ;
38
+ }
39
+ } ) ;
42
40
43
41
export default readDatabase ;
44
42
module . exports = readDatabase ;
0 commit comments