Skip to content

Commit

Permalink
feat!: Remove async/await and make conversions sync
Browse files Browse the repository at this point in the history
- There were no requirements within the code to have them async

Signed-off-by: Sebastian Malton <[email protected]>
  • Loading branch information
Nokel81 committed Oct 25, 2023
1 parent aafbbf0 commit 71f7b5c
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 355 deletions.
8 changes: 4 additions & 4 deletions src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import { buildJ2COptions, buildC2JOptions, validate, isObject, isString } from '

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

export async function json2csv(data: object[], options?: Json2CsvOptions): Promise<string> {
export function json2csv(data: object[], options?: Json2CsvOptions): string {
const builtOptions = buildJ2COptions(options ?? {});

// Validate the parameters before calling the converter's convert function
validate(data, isObject, errors.json2csv);

return Json2Csv(builtOptions).convert(data);
}

export async function csv2json(data: string, options?: Csv2JsonOptions): Promise<object[]> {
export function csv2json(data: string, options?: Csv2JsonOptions): object[] {
const builtOptions = buildC2JOptions(options ?? {});

// Validate the parameters before calling the converter's convert function
validate(data, isString, errors.csv2json);

Expand Down
17 changes: 9 additions & 8 deletions src/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
/**
* Removes the Excel BOM value, if specified by the options object
*/
async function stripExcelBOM(csv: string) {
function stripExcelBOM(csv: string) {
if (options.excelBOM) {
return csv.replace(excelBOMRegex, '');
}
Expand All @@ -80,7 +80,7 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
justParsedDoubleQuote: false,
startIndex: 0
};

let splitLine = [],
character,
charBefore,
Expand Down Expand Up @@ -363,13 +363,14 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
/**
* Internally exported csv2json function
*/
async function convert(data: string) {
function convert(data: string) {
// Split the CSV into lines using the specified EOL option
return stripExcelBOM(data)
.then(splitLines)
.then(retrieveHeading) // Retrieve the headings from the CSV, unless the user specified the keys
.then(retrieveRecordLines) // Retrieve the record lines from the CSV
.then(transformRecordLines); // Retrieve the JSON document array
const stripped = stripExcelBOM(data);
const split = splitLines(stripped);
const heading = retrieveHeading(split); // Retrieve the headings from the CSV, unless the user specified the keys
const lines = retrieveRecordLines(heading); // Retrieve the record lines from the CSV

return transformRecordLines(lines); // Retrieve the JSON document array
}

return {
Expand Down
98 changes: 49 additions & 49 deletions src/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
/**
* Returns the list of data field names of all documents in the provided list
*/
async function getFieldNameList(data: object[]) {
function getFieldNameList(data: object[]) {
// If keys weren't specified, then we'll use the list of keys generated by the deeks module
return deepKeysFromList(data, deeksOptions);
}
Expand Down Expand Up @@ -161,21 +161,23 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
*/
function retrieveHeaderFields(data: object[]) {
const keyStrings = convertKeysToHeaderFields();

if (options.keys) {
options.keys = keyStrings;

if (!options.unwindArrays) {
return Promise.resolve(keyStrings)
.then(filterExcludedKeys)
.then(sortHeaderFields);
const filtered = filterExcludedKeys(keyStrings);


return sortHeaderFields(filtered);
}
}

return getFieldNameList(data)
.then(processSchemas)
.then(filterExcludedKeys)
.then(sortHeaderFields);
const fieldNames = getFieldNameList(data);
const processed = processSchemas(fieldNames);
const filtered = filterExcludedKeys(processed);

return sortHeaderFields(filtered);
}

/** RECORD FIELD FUNCTIONS **/
Expand All @@ -184,11 +186,11 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
* Unwinds objects in arrays within record objects if the user specifies the
* expandArrayObjects option. If not specified, this passes the params
* argument through to the next function in the promise chain.
*
*
* The `finalPass` parameter is used to trigger one last pass to ensure no more
* arrays need to be expanded
*/
async function unwindRecordsIfNecessary(params: Json2CsvParams, finalPass = false): Promise<Json2CsvParams> {
function unwindRecordsIfNecessary(params: Json2CsvParams, finalPass = false): Json2CsvParams {
if (options.unwindArrays) {
const originalRecordsLength = params.records.length;

Expand All @@ -197,30 +199,28 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
params.records = utils.unwind(params.records, headerField);
});

return retrieveHeaderFields(params.records)
.then((headerFields) => {
params.headerFields = headerFields;

// If we were able to unwind more arrays, then try unwinding again...
if (originalRecordsLength !== params.records.length) {
return unwindRecordsIfNecessary(params);
}
// Otherwise, we didn't unwind any additional arrays, so continue...

// Run a final time in case the earlier unwinding exposed additional
// arrays to unwind...
if (!finalPass) {
return unwindRecordsIfNecessary(params, true);
}

// If keys were provided, set the headerFields back to the provided keys after unwinding:
if (options.keys) {
const userSelectedFields = convertKeysToHeaderFields();
params.headerFields = filterExcludedKeys(userSelectedFields);
}

return params;
});
const headerFields = retrieveHeaderFields(params.records);
params.headerFields = headerFields;

// If we were able to unwind more arrays, then try unwinding again...
if (originalRecordsLength !== params.records.length) {
return unwindRecordsIfNecessary(params);
}
// Otherwise, we didn't unwind any additional arrays, so continue...

// Run a final time in case the earlier unwinding exposed additional
// arrays to unwind...
if (!finalPass) {
return unwindRecordsIfNecessary(params, true);
}

// If keys were provided, set the headerFields back to the provided keys after unwinding:
if (options.keys) {
const userSelectedFields = convertKeysToHeaderFields();
params.headerFields = filterExcludedKeys(userSelectedFields);
}

return params;
}
return params;
}
Expand Down Expand Up @@ -403,26 +403,26 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
/**
* Internally exported json2csv function
*/
async function convert(data: object[]) {
function convert(data: object[]) {
// Single document, not an array
if (utils.isObject(data) && !data.length) {
data = [data]; // Convert to an array of the given document
}

// Retrieve the heading and then generate the CSV with the keys that are identified
return retrieveHeaderFields(data)
.then((headerFields) => ({
headerFields,
records: data,
header: '',
recordString: '',
}))
.then(unwindRecordsIfNecessary)
.then(processRecords)
.then(wrapHeaderFields)
.then(trimHeaderFields)
.then(generateCsvHeader)
.then(generateCsvFromComponents);
const headerFields = {
headerFields: retrieveHeaderFields(data),
records: data,
header: '',
recordString: '',
};
const unwinded = unwindRecordsIfNecessary(headerFields);
const processed = processRecords(unwinded);
const wrapped = wrapHeaderFields(processed);
const trimmed = trimHeaderFields(wrapped);
const generated = generateCsvHeader(trimmed);

return generateCsvFromComponents(generated);
}

return {
Expand Down
Loading

0 comments on commit 71f7b5c

Please sign in to comment.