@@ -23,7 +23,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
2323 /**
2424 * Returns the list of data field names of all documents in the provided list
2525 */
26- async function getFieldNameList ( data : object [ ] ) {
26+ function getFieldNameList ( data : object [ ] ) {
2727 // If keys weren't specified, then we'll use the list of keys generated by the deeks module
2828 return deepKeysFromList ( data , deeksOptions ) ;
2929 }
@@ -161,21 +161,23 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
161161 */
162162 function retrieveHeaderFields ( data : object [ ] ) {
163163 const keyStrings = convertKeysToHeaderFields ( ) ;
164-
164+
165165 if ( options . keys ) {
166166 options . keys = keyStrings ;
167167
168168 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 ) ;
172173 }
173174 }
174175
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 ) ;
179181 }
180182
181183 /** RECORD FIELD FUNCTIONS **/
@@ -184,11 +186,11 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
184186 * Unwinds objects in arrays within record objects if the user specifies the
185187 * expandArrayObjects option. If not specified, this passes the params
186188 * argument through to the next function in the promise chain.
187- *
189+ *
188190 * The `finalPass` parameter is used to trigger one last pass to ensure no more
189191 * arrays need to be expanded
190192 */
191- async function unwindRecordsIfNecessary ( params : Json2CsvParams , finalPass = false ) : Promise < Json2CsvParams > {
193+ function unwindRecordsIfNecessary ( params : Json2CsvParams , finalPass = false ) : Json2CsvParams {
192194 if ( options . unwindArrays ) {
193195 const originalRecordsLength = params . records . length ;
194196
@@ -197,30 +199,28 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
197199 params . records = utils . unwind ( params . records , headerField ) ;
198200 } ) ;
199201
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 ;
224224 }
225225 return params ;
226226 }
@@ -403,26 +403,26 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
403403 /**
404404 * Internally exported json2csv function
405405 */
406- async function convert ( data : object [ ] ) {
406+ function convert ( data : object [ ] ) {
407407 // Single document, not an array
408408 if ( utils . isObject ( data ) && ! data . length ) {
409409 data = [ data ] ; // Convert to an array of the given document
410410 }
411411
412412 // 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 ) ;
426426 }
427427
428428 return {
0 commit comments