Skip to content

Commit 1f2e6f9

Browse files
authored
Merge pull request #239 from Nokel81/make-everything-sync
feat!: Remove async/await and make conversions sync
2 parents aafbbf0 + 71f7b5c commit 1f2e6f9

File tree

5 files changed

+360
-355
lines changed

5 files changed

+360
-355
lines changed

src/converter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ import { buildJ2COptions, buildC2JOptions, validate, isObject, isString } from '
88

99
export type { Json2CsvOptions, Csv2JsonOptions } from './types';
1010

11-
export async function json2csv(data: object[], options?: Json2CsvOptions): Promise<string> {
11+
export function json2csv(data: object[], options?: Json2CsvOptions): string {
1212
const builtOptions = buildJ2COptions(options ?? {});
13-
13+
1414
// Validate the parameters before calling the converter's convert function
1515
validate(data, isObject, errors.json2csv);
1616

1717
return Json2Csv(builtOptions).convert(data);
1818
}
1919

20-
export async function csv2json(data: string, options?: Csv2JsonOptions): Promise<object[]> {
20+
export function csv2json(data: string, options?: Csv2JsonOptions): object[] {
2121
const builtOptions = buildC2JOptions(options ?? {});
22-
22+
2323
// Validate the parameters before calling the converter's convert function
2424
validate(data, isString, errors.csv2json);
2525

src/csv2json.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
5959
/**
6060
* Removes the Excel BOM value, if specified by the options object
6161
*/
62-
async function stripExcelBOM(csv: string) {
62+
function stripExcelBOM(csv: string) {
6363
if (options.excelBOM) {
6464
return csv.replace(excelBOMRegex, '');
6565
}
@@ -80,7 +80,7 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
8080
justParsedDoubleQuote: false,
8181
startIndex: 0
8282
};
83-
83+
8484
let splitLine = [],
8585
character,
8686
charBefore,
@@ -363,13 +363,14 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
363363
/**
364364
* Internally exported csv2json function
365365
*/
366-
async function convert(data: string) {
366+
function convert(data: string) {
367367
// Split the CSV into lines using the specified EOL option
368-
return stripExcelBOM(data)
369-
.then(splitLines)
370-
.then(retrieveHeading) // Retrieve the headings from the CSV, unless the user specified the keys
371-
.then(retrieveRecordLines) // Retrieve the record lines from the CSV
372-
.then(transformRecordLines); // Retrieve the JSON document array
368+
const stripped = stripExcelBOM(data);
369+
const split = splitLines(stripped);
370+
const heading = retrieveHeading(split); // Retrieve the headings from the CSV, unless the user specified the keys
371+
const lines = retrieveRecordLines(heading); // Retrieve the record lines from the CSV
372+
373+
return transformRecordLines(lines); // Retrieve the JSON document array
373374
}
374375

375376
return {

src/json2csv.ts

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)