Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/export csv refactor #29

Merged
3 commits merged into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/export.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const exportCsv = {
NAME: 'BusinessIndexResults',
FILE_NAME: 'BusinessIndexResults',
};

export default exportCsv;
57 changes: 31 additions & 26 deletions src/utils/export.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
import config from '../config/export';

const { NAME } = config;
const { FILE_NAME } = config;

/**
* @function exportCSV
*
* @param {string} header - The header to use in the CSV
* @param {Array<JSON>} results - The results to save in a CSV file
*
* @return {string} A string of all the results in CSV format
*/
export function exportCSV(header, results) {
const columnNames = ['id', 'businessName', 'postCode', 'industryCode', 'legalStatus', 'tradingStatus', 'turnover', 'employmentBands', 'companyNo'];
let CSV = '';
CSV += `${header}\r\n`; // Firstly, insert the header
// Go through the results, putting them in the CSV string
for (let i = 0; i < results.length; i += 1) {
let row = '';
// If there is missing data, insert an empty string
for (let j = 0; j < columnNames.length; j += 1) {
if (results[i][columnNames[j]] === undefined) {
row += '"",';
} else {
row += `"${results[i][columnNames[j]]}",`;
}
}
CSV += `${row}\r\n`;
}
return CSV;
const cols = ['id', 'businessName', 'postCode', 'industryCode', 'legalStatus', 'tradingStatus', 'turnover', 'employmentBands', 'companyNo'];
const rows = results.map(
leu => cols.map(
col => ((leu[col] === undefined) ? '"",' : `"${leu[col]}",`), // Use empty string if no value present
).join('').concat('\r\n'), // Make into a string and add tab + newline at the end
);
return `${header}\r\n`.concat(rows.join(''));
}

/**
* @function downloadCSV
*
* @param {Array<JSON>} results - The results to save in a CSV file
*
*/
export function downloadCSV(results) {
const header = 'UBRN,Business Name,PostCode,Industry Code,Legal Status,Trading Status,Turnover,Employment,Company Reference Number';
const csv = exportCSV(header, results, NAME);
const csv = exportCSV(header, results);
const uri = `data:text/csv;charset=utf-8,${escape(csv)}`;
const link = document.createElement('a');
link.href = uri;
const filename = `${NAME}.csv`;
link.download = filename;
// Below append/remove child is to ensure the download button works on
// Firefox, link.click()
document.body.appendChild(link);
link.download = `${FILE_NAME}.csv`;
link.click();
document.body.removeChild(link);
}

/**
* @function downloadJSON
*
* @param {Array<JSON>} results - The results to save in a JSON file
*
*/
export function downloadJSON(results) {
const jsonStr = JSON.stringify(results, null, 2);
const uri = `data:text/json;charset=utf-8,${escape(jsonStr)}`;
const link = document.createElement('a');
link.href = uri;
link.download = `${NAME}.json`;
link.download = `${FILE_NAME}.json`;
link.click();
}