@@ -23,7 +23,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
23
23
/**
24
24
* Returns the list of data field names of all documents in the provided list
25
25
*/
26
- async function getFieldNameList ( data : object [ ] ) {
26
+ function getFieldNameList ( data : object [ ] ) {
27
27
// If keys weren't specified, then we'll use the list of keys generated by the deeks module
28
28
return deepKeysFromList ( data , deeksOptions ) ;
29
29
}
@@ -161,21 +161,23 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
161
161
*/
162
162
function retrieveHeaderFields ( data : object [ ] ) {
163
163
const keyStrings = convertKeysToHeaderFields ( ) ;
164
-
164
+
165
165
if ( options . keys ) {
166
166
options . keys = keyStrings ;
167
167
168
168
if ( ! options . unwindArrays ) {
169
- return Promise . resolve ( keyStrings )
170
- . then ( filterExcludedKeys )
171
- . then ( sortHeaderFields ) ;
169
+ const filtered = filterExcludedKeys ( keyStrings ) ;
170
+
171
+
172
+ return sortHeaderFields ( filtered ) ;
172
173
}
173
174
}
174
175
175
- return getFieldNameList ( data )
176
- . then ( processSchemas )
177
- . then ( filterExcludedKeys )
178
- . then ( sortHeaderFields ) ;
176
+ const fieldNames = getFieldNameList ( data ) ;
177
+ const processed = processSchemas ( fieldNames ) ;
178
+ const filtered = filterExcludedKeys ( processed ) ;
179
+
180
+ return sortHeaderFields ( filtered ) ;
179
181
}
180
182
181
183
/** RECORD FIELD FUNCTIONS **/
@@ -184,11 +186,11 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
184
186
* Unwinds objects in arrays within record objects if the user specifies the
185
187
* expandArrayObjects option. If not specified, this passes the params
186
188
* argument through to the next function in the promise chain.
187
- *
189
+ *
188
190
* The `finalPass` parameter is used to trigger one last pass to ensure no more
189
191
* arrays need to be expanded
190
192
*/
191
- async function unwindRecordsIfNecessary ( params : Json2CsvParams , finalPass = false ) : Promise < Json2CsvParams > {
193
+ function unwindRecordsIfNecessary ( params : Json2CsvParams , finalPass = false ) : Json2CsvParams {
192
194
if ( options . unwindArrays ) {
193
195
const originalRecordsLength = params . records . length ;
194
196
@@ -197,30 +199,28 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
197
199
params . records = utils . unwind ( params . records , headerField ) ;
198
200
} ) ;
199
201
200
- return retrieveHeaderFields ( params . records )
201
- . then ( ( headerFields ) => {
202
- params . headerFields = headerFields ;
203
-
204
- // If we were able to unwind more arrays, then try unwinding again...
205
- if ( originalRecordsLength !== params . records . length ) {
206
- return unwindRecordsIfNecessary ( params ) ;
207
- }
208
- // Otherwise, we didn't unwind any additional arrays, so continue...
209
-
210
- // Run a final time in case the earlier unwinding exposed additional
211
- // arrays to unwind...
212
- if ( ! finalPass ) {
213
- return unwindRecordsIfNecessary ( params , true ) ;
214
- }
215
-
216
- // If keys were provided, set the headerFields back to the provided keys after unwinding:
217
- if ( options . keys ) {
218
- const userSelectedFields = convertKeysToHeaderFields ( ) ;
219
- params . headerFields = filterExcludedKeys ( userSelectedFields ) ;
220
- }
221
-
222
- return params ;
223
- } ) ;
202
+ const headerFields = retrieveHeaderFields ( params . records ) ;
203
+ params . headerFields = headerFields ;
204
+
205
+ // If we were able to unwind more arrays, then try unwinding again...
206
+ if ( originalRecordsLength !== params . records . length ) {
207
+ return unwindRecordsIfNecessary ( params ) ;
208
+ }
209
+ // Otherwise, we didn't unwind any additional arrays, so continue...
210
+
211
+ // Run a final time in case the earlier unwinding exposed additional
212
+ // arrays to unwind...
213
+ if ( ! finalPass ) {
214
+ return unwindRecordsIfNecessary ( params , true ) ;
215
+ }
216
+
217
+ // If keys were provided, set the headerFields back to the provided keys after unwinding:
218
+ if ( options . keys ) {
219
+ const userSelectedFields = convertKeysToHeaderFields ( ) ;
220
+ params . headerFields = filterExcludedKeys ( userSelectedFields ) ;
221
+ }
222
+
223
+ return params ;
224
224
}
225
225
return params ;
226
226
}
@@ -403,26 +403,26 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
403
403
/**
404
404
* Internally exported json2csv function
405
405
*/
406
- async function convert ( data : object [ ] ) {
406
+ function convert ( data : object [ ] ) {
407
407
// Single document, not an array
408
408
if ( utils . isObject ( data ) && ! data . length ) {
409
409
data = [ data ] ; // Convert to an array of the given document
410
410
}
411
411
412
412
// Retrieve the heading and then generate the CSV with the keys that are identified
413
- return retrieveHeaderFields ( data )
414
- . then ( ( headerFields ) => ( {
415
- headerFields ,
416
- records : data ,
417
- header : '' ,
418
- recordString : '' ,
419
- } ) )
420
- . then ( unwindRecordsIfNecessary )
421
- . then ( processRecords )
422
- . then ( wrapHeaderFields )
423
- . then ( trimHeaderFields )
424
- . then ( generateCsvHeader )
425
- . then ( generateCsvFromComponents ) ;
413
+ const headerFields = {
414
+ headerFields : retrieveHeaderFields ( data ) ,
415
+ records : data ,
416
+ header : '' ,
417
+ recordString : '' ,
418
+ } ;
419
+ const unwinded = unwindRecordsIfNecessary ( headerFields ) ;
420
+ const processed = processRecords ( unwinded ) ;
421
+ const wrapped = wrapHeaderFields ( processed ) ;
422
+ const trimmed = trimHeaderFields ( wrapped ) ;
423
+ const generated = generateCsvHeader ( trimmed ) ;
424
+
425
+ return generateCsvFromComponents ( generated ) ;
426
426
}
427
427
428
428
return {
0 commit comments