6
6
7
7
'use strict' ;
8
8
9
+ const fs = require ( 'fs' ) ;
10
+ const csv = require ( 'fast-csv' ) ;
11
+ const moment = require ( 'moment' ) ;
12
+ const PromishLib = require ( '../utils/promish' ) ;
13
+ const StreamBuf = require ( '../utils/stream-buf' ) ;
9
14
10
- var fs = require ( 'fs' ) ;
11
- var csv = require ( 'fast-csv' ) ;
12
- var moment = require ( 'moment' ) ;
13
- var PromishLib = require ( '../utils/promish' ) ;
14
- var StreamBuf = require ( '../utils/stream-buf' ) ;
15
+ const utils = require ( '../utils/utils' ) ;
15
16
16
- var utils = require ( '../utils/utils' ) ;
17
-
18
- var CSV = module . exports = function ( workbook ) {
17
+ const CSV = ( module . exports = function ( workbook ) {
19
18
this . workbook = workbook ;
20
19
this . worksheet = null ;
21
- } ;
20
+ } ) ;
22
21
23
22
/* eslint-disable quote-props */
24
- var SpecialValues = {
25
- ' true' : true ,
26
- ' false' : false ,
23
+ const SpecialValues = {
24
+ true : true ,
25
+ false : false ,
27
26
'#N/A' : { error : '#N/A' } ,
28
27
'#REF!' : { error : '#REF!' } ,
29
28
'#NAME?' : { error : '#NAME?' } ,
@@ -34,89 +33,91 @@ var SpecialValues = {
34
33
} ;
35
34
/* eslint-ensable quote-props */
36
35
37
-
38
36
CSV . prototype = {
39
- readFile : function ( filename , options ) {
40
- var self = this ;
37
+ readFile ( filename , options ) {
38
+ const self = this ;
41
39
options = options || { } ;
42
- var stream ;
43
- return utils . fs . exists ( filename )
44
- . then ( function ( exists ) {
40
+ let stream ;
41
+ return utils . fs
42
+ . exists ( filename )
43
+ . then ( exists => {
45
44
if ( ! exists ) {
46
- throw new Error ( ' File not found: ' + filename ) ;
45
+ throw new Error ( ` File not found: ${ filename } ` ) ;
47
46
}
48
47
stream = fs . createReadStream ( filename ) ;
49
48
return self . read ( stream , options ) ;
50
49
} )
51
- . then ( function ( worksheet ) {
50
+ . then ( worksheet => {
52
51
stream . close ( ) ;
53
52
return worksheet ;
54
53
} ) ;
55
54
} ,
56
- read : function ( stream , options ) {
55
+ read ( stream , options ) {
57
56
options = options || { } ;
58
57
return new PromishLib . Promish ( ( resolve , reject ) => {
59
- var csvStream = this . createInputStream ( options )
58
+ const csvStream = this . createInputStream ( options )
60
59
. on ( 'worksheet' , resolve )
61
60
. on ( 'error' , reject ) ;
62
61
63
62
stream . pipe ( csvStream ) ;
64
63
} ) ;
65
64
} ,
66
- createInputStream : function ( options ) {
65
+ createInputStream ( options ) {
67
66
options = options || { } ;
68
- var worksheet = this . workbook . addWorksheet ( options . sheetName ) ;
69
-
70
- var dateFormats = options . dateFormats || [
71
- moment . ISO_8601 ,
72
- 'MM-DD-YYYY' ,
73
- 'YYYY-MM-DD'
74
- ] ;
75
- var map = options . map || function ( datum ) {
67
+ const worksheet = this . workbook . addWorksheet ( options . sheetName ) ;
68
+
69
+ const dateFormats = options . dateFormats || [ moment . ISO_8601 , 'MM-DD-YYYY' , 'YYYY-MM-DD' ] ;
70
+ const map =
71
+ options . map ||
72
+ function ( datum ) {
76
73
if ( datum === '' ) {
77
74
return null ;
78
75
}
79
76
if ( ! isNaN ( datum ) ) {
80
77
return parseFloat ( datum ) ;
81
78
}
82
- var dt = moment ( datum , dateFormats , true ) ;
79
+ const dt = moment ( datum , dateFormats , true ) ;
83
80
if ( dt . isValid ( ) ) {
84
81
return new Date ( dt . valueOf ( ) ) ;
85
82
}
86
- var special = SpecialValues [ datum ] ;
83
+ const special = SpecialValues [ datum ] ;
87
84
if ( special !== undefined ) {
88
85
return special ;
89
86
}
90
87
return datum ;
91
88
} ;
92
89
93
- var csvStream = csv ( options )
94
- . on ( 'data' , function ( data ) {
90
+ const csvStream = csv ( options )
91
+ . on ( 'data' , data => {
95
92
worksheet . addRow ( data . map ( map ) ) ;
96
93
} )
97
- . on ( 'end' , function ( ) {
94
+ . on ( 'end' , ( ) => {
98
95
csvStream . emit ( 'worksheet' , worksheet ) ;
99
96
} ) ;
100
97
return csvStream ;
101
98
} ,
102
99
103
- write : function ( stream , options ) {
100
+ write ( stream , options ) {
104
101
return new PromishLib . Promish ( ( resolve , reject ) => {
105
102
options = options || { } ;
106
- // var encoding = options.encoding || 'utf8';
107
- // var separator = options.separator || ',';
108
- // var quoteChar = options.quoteChar || '\'';
103
+ // const encoding = options.encoding || 'utf8';
104
+ // const separator = options.separator || ',';
105
+ // const quoteChar = options.quoteChar || '\'';
109
106
110
- var worksheet = this . workbook . getWorksheet ( options . sheetName || options . sheetId ) ;
107
+ const worksheet = this . workbook . getWorksheet ( options . sheetName || options . sheetId ) ;
111
108
112
- var csvStream = csv . createWriteStream ( options ) ;
113
- stream . on ( 'finish' , ( ) => { resolve ( ) ; } ) ;
109
+ const csvStream = csv . createWriteStream ( options ) ;
110
+ stream . on ( 'finish' , ( ) => {
111
+ resolve ( ) ;
112
+ } ) ;
114
113
csvStream . on ( 'error' , reject ) ;
115
114
csvStream . pipe ( stream ) ;
116
115
117
- var dateFormat = options . dateFormat ;
118
- var dateUTC = options . dateUTC ;
119
- var map = options . map || ( value => {
116
+ const dateFormat = options . dateFormat ;
117
+ const dateUTC = options . dateUTC ;
118
+ const map =
119
+ options . map ||
120
+ ( value => {
120
121
if ( value ) {
121
122
if ( value . text || value . hyperlink ) {
122
123
return value . hyperlink || value . text || '' ;
@@ -128,7 +129,7 @@ CSV.prototype = {
128
129
if ( dateFormat ) {
129
130
dateUTC ? moment . utc ( value ) . format ( dateFormat ) : moment ( value ) . format ( dateFormat ) ;
130
131
}
131
- return dateUTC ? moment . utc ( value ) . format ( ) : moment ( value ) . format ( )
132
+ return dateUTC ? moment . utc ( value ) . format ( ) : moment ( value ) . format ( ) ;
132
133
}
133
134
if ( value . error ) {
134
135
return value . error ;
@@ -140,16 +141,16 @@ CSV.prototype = {
140
141
return value ;
141
142
} ) ;
142
143
143
- var includeEmptyRows = ( options . includeEmptyRows === undefined ) || options . includeEmptyRows ;
144
- var lastRow = 1 ;
144
+ const includeEmptyRows = options . includeEmptyRows === undefined || options . includeEmptyRows ;
145
+ let lastRow = 1 ;
145
146
if ( worksheet ) {
146
- worksheet . eachRow ( function ( row , rowNumber ) {
147
+ worksheet . eachRow ( ( row , rowNumber ) => {
147
148
if ( includeEmptyRows ) {
148
149
while ( lastRow ++ < rowNumber - 1 ) {
149
150
csvStream . write ( [ ] ) ;
150
151
}
151
152
}
152
- var values = row . values ;
153
+ const values = row . values ;
153
154
values . shift ( ) ;
154
155
csvStream . write ( values . map ( map ) ) ;
155
156
lastRow = rowNumber ;
@@ -158,22 +159,19 @@ CSV.prototype = {
158
159
csvStream . end ( ) ;
159
160
} ) ;
160
161
} ,
161
- writeFile : function ( filename , options ) {
162
+ writeFile ( filename , options ) {
162
163
options = options || { } ;
163
164
164
- var streamOptions = {
165
- encoding : options . encoding || 'utf8'
165
+ const streamOptions = {
166
+ encoding : options . encoding || 'utf8' ,
166
167
} ;
167
- var stream = fs . createWriteStream ( filename , streamOptions ) ;
168
+ const stream = fs . createWriteStream ( filename , streamOptions ) ;
168
169
169
170
return this . write ( stream , options ) ;
170
171
} ,
171
- writeBuffer : function ( options ) {
172
- var self = this ;
173
- var stream = new StreamBuf ( ) ;
174
- return self . write ( stream , options )
175
- . then ( function ( ) {
176
- return stream . read ( ) ;
177
- } ) ;
178
- }
172
+ writeBuffer ( options ) {
173
+ const self = this ;
174
+ const stream = new StreamBuf ( ) ;
175
+ return self . write ( stream , options ) . then ( ( ) => stream . read ( ) ) ;
176
+ } ,
179
177
} ;
0 commit comments