Skip to content

Commit

Permalink
Changed export format back to tsv to avoid additional csv format inco…
Browse files Browse the repository at this point in the history
…ncistencies (i.e. handling quotes and newlines). For all free-form responses, escapes any tabs to make sure it is properly parsed as TSV. #1000
  • Loading branch information
allennatang committed Jan 4, 2025
1 parent 5211e94 commit ace6ac3
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/features/Search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ class SearchContainer extends React.Component<{}, ISearchState> {
return search ? decodeURIComponent(search) : '';
}

// If the value contains a tab, wrap it in double quotes to make sure
// it is correctly interpreted when parsed as a tab-separated value (TSV).
private escapeTSV(value: string): string {
if (value.includes('\t')) {
value = `"${value}"`;
}
return value;
}

private downloadData(): void {
const headers = [
{ label: CONSTANTS.FIRST_NAME_LABEL, key: 'accountId.firstName' },
Expand Down Expand Up @@ -276,27 +285,51 @@ class SearchContainer extends React.Component<{}, ISearchState> {
headers.push({label: CONSTANTS.DIETARY_RESTRICTIONS_LABEL, key: 'accountId.dietaryRestrictions'});
headers.push({label: 'Authorize MLH to send emails', key: 'application.other.sendEmail'})
}

const tempHeaders: string[] = [];
headers.forEach((header) => {
tempHeaders.push(header.label);
});
const csvData: string[] = [tempHeaders.join('\t')]; // actually in tsv format

const csvData: string[] = [tempHeaders.join(',')];

this.filter().forEach((result) => {
if (result.selected) {
const row: string[] = [];
headers.forEach((header) => {
let value;
let free_response = false;

if (header.key.indexOf('.') >= 0) {

// Check if the field is a free-form response
if (header.key == "application.shortAnswer.question1" || header.key == "application.shortAnswer.question2" || header.key == "application.shortAnswer.comments") {
free_response = true
}

// Get the value of the field by navigating the nested structure of the hacker object,
// using the header key to determine the path
const nestedAttr = header.key.split('.');
value = getNestedAttr(result.hacker, nestedAttr);

// Format free responses to be properly parsed as tab-separated value (TSV)
if (free_response == true){

// If the value contains a tab, wrap it in double quotes
if (value.includes('\t')) {
value = `"${value}"`;
}
}

} else {
value = result.hacker[header.key];
value = result.hacker[header.key]
}
row.push(value);
});
csvData.push(row.join('\t'));
}
});

fileDownload(csvData.join('\n'), 'hackerData.tsv', 'text/tsv');
}

Expand Down

0 comments on commit ace6ac3

Please sign in to comment.