1
- const fs = require ( 'fs' ) ;
2
- const csv = require ( 'csv-parser' ) ;
1
+ const fs = require ( "fs" ) ;
2
+ const csv = require ( "csv-parser" ) ;
3
+ const glob = require ( "glob" ) ;
3
4
4
- const getCount = async ( ) => {
5
- const filePaths = [ '1.csv' , '2.csv' ] ;
6
- const filterColumns = [ 'technical-workshop-topic' , 'non-technical-workshop-topic' ] ;
5
+ const TECHNICAL_WORKSHOP_TOPIC = "technical-workshop-topic" ;
6
+ const NON_TECHNICAL_WORKSHOP_TOPIC = "non-technical-workshop-topic" ;
7
7
8
- const columnCounts = { } ;
9
- filterColumns . forEach ( ( column ) => {
10
- columnCounts [ column ] = { } ;
11
- } ) ;
8
+ const accountRenamings = ( obj = { } ) => {
9
+ const renameEvent = ( parent = "" , oldName = "" , newName = "" ) => {
10
+ obj [ parent ] [ newName ] += obj [ parent ] [ oldName ] ;
11
+ delete obj [ parent ] [ oldName ] ;
12
+ return obj ;
13
+ } ;
14
+ renameEvent (
15
+ TECHNICAL_WORKSHOP_TOPIC ,
16
+ "No Code App Building" ,
17
+ "No-Code 101: Building No-Code WebApps with CodeDesign"
18
+ ) ;
19
+ renameEvent (
20
+ TECHNICAL_WORKSHOP_TOPIC ,
21
+ "Cloud Services Workshop" ,
22
+ "Linux And Cloud Fundamentals"
23
+ ) ;
24
+ renameEvent (
25
+ NON_TECHNICAL_WORKSHOP_TOPIC ,
26
+ "Growth strategies for Bootstrapped startups" ,
27
+ "Cost-Effective Ways to Grow Your Self-Funded Startup"
28
+ ) ;
29
+
30
+ console . log ( obj ) ;
31
+
32
+ console . log ( "After removing undefined entries:\n" ) ;
33
+
34
+ delete obj [ TECHNICAL_WORKSHOP_TOPIC ] [ undefined ] ;
35
+ delete obj [ NON_TECHNICAL_WORKSHOP_TOPIC ] [ undefined ] ;
36
+
37
+ return obj ;
38
+ } ;
39
+
40
+ const getCount = ( ) => {
41
+ // const filePaths = ["csv/bothStudent.csv", "csv/workshopOnlyStudent.csv"];
42
+ const filePaths = glob . sync ( "csv/**/*.csv" ) ;
43
+ console . log ( filePaths ) ;
44
+
45
+ const filterColumns = [
46
+ "technical-workshop-topic" ,
47
+ "non-technical-workshop-topic" ,
48
+ ] ;
12
49
13
- return new Promise ( ( resolve , reject ) => {
14
- let count = 0 ;
15
- filePaths . forEach ( ( filePath ) => {
16
- fs . createReadStream ( filePath )
17
- . pipe ( csv ( ) )
18
- . on ( 'data' , ( row ) => {
19
- filterColumns . forEach ( ( column ) => {
20
- const value = row [ column ] ;
21
- if ( ! columnCounts [ column ] [ value ] ) {
22
- columnCounts [ column ] [ value ] = 0 ;
23
- }
24
- columnCounts [ column ] [ value ] ++ ;
25
- } ) ;
26
- } )
27
- . on ( 'end' , ( ) => {
28
- count ++ ;
29
- if ( count === filePaths . length ) {
30
- resolve ( columnCounts ) ;
31
- }
32
- } )
33
- . on ( 'error' , ( err ) => {
34
- reject ( err ) ;
35
- } ) ;
50
+ const columnCounts = { } ;
51
+ filterColumns . forEach ( ( column ) => {
52
+ columnCounts [ column ] = { } ;
53
+ } ) ;
54
+
55
+ return new Promise ( ( resolve , reject ) => {
56
+ let count = 0 ;
57
+ filePaths . forEach ( ( filePath ) => {
58
+ fs . createReadStream ( filePath )
59
+ . pipe ( csv ( ) )
60
+ . on ( "data" , ( row ) => {
61
+ filterColumns . forEach ( ( column ) => {
62
+ const value = row [ column ] ;
63
+ if ( ! columnCounts [ column ] [ value ] ) {
64
+ columnCounts [ column ] [ value ] = 0 ;
65
+ }
66
+ columnCounts [ column ] [ value ] ++ ;
67
+ } ) ;
68
+ } )
69
+ . on ( "end" , ( ) => {
70
+ count ++ ;
71
+ if ( count === filePaths . length ) {
72
+ resolve ( accountRenamings ( columnCounts ) ) ;
73
+ }
74
+ } )
75
+ . on ( "error" , ( err ) => {
76
+ reject ( err ) ;
36
77
} ) ;
37
78
} ) ;
79
+ } ) ;
38
80
} ;
39
81
40
- module . exports = getCount ;
82
+ getCount ( ) . then ( ( res ) => console . log ( res ) ) ;
83
+
84
+ module . exports = getCount ;
0 commit comments