66
77'use strict' ;
88
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' ) ;
914
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' ) ;
1516
16- var utils = require ( '../utils/utils' ) ;
17-
18- var CSV = module . exports = function ( workbook ) {
17+ const CSV = ( module . exports = function ( workbook ) {
1918 this . workbook = workbook ;
2019 this . worksheet = null ;
21- } ;
20+ } ) ;
2221
2322/* eslint-disable quote-props */
24- var SpecialValues = {
25- ' true' : true ,
26- ' false' : false ,
23+ const SpecialValues = {
24+ true : true ,
25+ false : false ,
2726 '#N/A' : { error : '#N/A' } ,
2827 '#REF!' : { error : '#REF!' } ,
2928 '#NAME?' : { error : '#NAME?' } ,
@@ -34,89 +33,91 @@ var SpecialValues = {
3433} ;
3534/* eslint-ensable quote-props */
3635
37-
3836CSV . prototype = {
39- readFile : function ( filename , options ) {
40- var self = this ;
37+ readFile ( filename , options ) {
38+ const self = this ;
4139 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 => {
4544 if ( ! exists ) {
46- throw new Error ( ' File not found: ' + filename ) ;
45+ throw new Error ( ` File not found: ${ filename } ` ) ;
4746 }
4847 stream = fs . createReadStream ( filename ) ;
4948 return self . read ( stream , options ) ;
5049 } )
51- . then ( function ( worksheet ) {
50+ . then ( worksheet => {
5251 stream . close ( ) ;
5352 return worksheet ;
5453 } ) ;
5554 } ,
56- read : function ( stream , options ) {
55+ read ( stream , options ) {
5756 options = options || { } ;
5857 return new PromishLib . Promish ( ( resolve , reject ) => {
59- var csvStream = this . createInputStream ( options )
58+ const csvStream = this . createInputStream ( options )
6059 . on ( 'worksheet' , resolve )
6160 . on ( 'error' , reject ) ;
6261
6362 stream . pipe ( csvStream ) ;
6463 } ) ;
6564 } ,
66- createInputStream : function ( options ) {
65+ createInputStream ( options ) {
6766 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 ) {
7673 if ( datum === '' ) {
7774 return null ;
7875 }
7976 if ( ! isNaN ( datum ) ) {
8077 return parseFloat ( datum ) ;
8178 }
82- var dt = moment ( datum , dateFormats , true ) ;
79+ const dt = moment ( datum , dateFormats , true ) ;
8380 if ( dt . isValid ( ) ) {
8481 return new Date ( dt . valueOf ( ) ) ;
8582 }
86- var special = SpecialValues [ datum ] ;
83+ const special = SpecialValues [ datum ] ;
8784 if ( special !== undefined ) {
8885 return special ;
8986 }
9087 return datum ;
9188 } ;
9289
93- var csvStream = csv ( options )
94- . on ( 'data' , function ( data ) {
90+ const csvStream = csv ( options )
91+ . on ( 'data' , data => {
9592 worksheet . addRow ( data . map ( map ) ) ;
9693 } )
97- . on ( 'end' , function ( ) {
94+ . on ( 'end' , ( ) => {
9895 csvStream . emit ( 'worksheet' , worksheet ) ;
9996 } ) ;
10097 return csvStream ;
10198 } ,
10299
103- write : function ( stream , options ) {
100+ write ( stream , options ) {
104101 return new PromishLib . Promish ( ( resolve , reject ) => {
105102 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 || '\'';
109106
110- var worksheet = this . workbook . getWorksheet ( options . sheetName || options . sheetId ) ;
107+ const worksheet = this . workbook . getWorksheet ( options . sheetName || options . sheetId ) ;
111108
112- var csvStream = csv . createWriteStream ( options ) ;
113- stream . on ( 'finish' , ( ) => { resolve ( ) ; } ) ;
109+ const csvStream = csv . createWriteStream ( options ) ;
110+ stream . on ( 'finish' , ( ) => {
111+ resolve ( ) ;
112+ } ) ;
114113 csvStream . on ( 'error' , reject ) ;
115114 csvStream . pipe ( stream ) ;
116115
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 => {
120121 if ( value ) {
121122 if ( value . text || value . hyperlink ) {
122123 return value . hyperlink || value . text || '' ;
@@ -128,7 +129,7 @@ CSV.prototype = {
128129 if ( dateFormat ) {
129130 dateUTC ? moment . utc ( value ) . format ( dateFormat ) : moment ( value ) . format ( dateFormat ) ;
130131 }
131- return dateUTC ? moment . utc ( value ) . format ( ) : moment ( value ) . format ( )
132+ return dateUTC ? moment . utc ( value ) . format ( ) : moment ( value ) . format ( ) ;
132133 }
133134 if ( value . error ) {
134135 return value . error ;
@@ -140,16 +141,16 @@ CSV.prototype = {
140141 return value ;
141142 } ) ;
142143
143- var includeEmptyRows = ( options . includeEmptyRows === undefined ) || options . includeEmptyRows ;
144- var lastRow = 1 ;
144+ const includeEmptyRows = options . includeEmptyRows === undefined || options . includeEmptyRows ;
145+ let lastRow = 1 ;
145146 if ( worksheet ) {
146- worksheet . eachRow ( function ( row , rowNumber ) {
147+ worksheet . eachRow ( ( row , rowNumber ) => {
147148 if ( includeEmptyRows ) {
148149 while ( lastRow ++ < rowNumber - 1 ) {
149150 csvStream . write ( [ ] ) ;
150151 }
151152 }
152- var values = row . values ;
153+ const values = row . values ;
153154 values . shift ( ) ;
154155 csvStream . write ( values . map ( map ) ) ;
155156 lastRow = rowNumber ;
@@ -158,22 +159,19 @@ CSV.prototype = {
158159 csvStream . end ( ) ;
159160 } ) ;
160161 } ,
161- writeFile : function ( filename , options ) {
162+ writeFile ( filename , options ) {
162163 options = options || { } ;
163164
164- var streamOptions = {
165- encoding : options . encoding || 'utf8'
165+ const streamOptions = {
166+ encoding : options . encoding || 'utf8' ,
166167 } ;
167- var stream = fs . createWriteStream ( filename , streamOptions ) ;
168+ const stream = fs . createWriteStream ( filename , streamOptions ) ;
168169
169170 return this . write ( stream , options ) ;
170171 } ,
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+ } ,
179177} ;
0 commit comments