Skip to content

Commit

Permalink
Export objects in fields when exporting csv
Browse files Browse the repository at this point in the history
  • Loading branch information
juanborre committed Apr 27, 2018
1 parent f15caf2 commit a759884
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions helpers/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ function formatRegularValue(value) {
return '';
}

value = (typeof value === 'string') ? value : String(value);
const type = typeof value;
value = (type === 'object') ? JSON.stringify(value) : (
(type === 'string') ? value : String(value)
);

if (value.match(SPECIAL_CHARACTERS_REGEX)) {
return `"${value.replace(/"/g, '""')}"`;
Expand Down Expand Up @@ -77,17 +80,17 @@ function fromCsvStringToArray(string, tableName) {
}

const a = []; // Initialize array to receive values.
// "Walk" the string using replace with callback.
// "Walk" the string using replace with callback.
string.replace(reValue, (m0, /* m1, */ m2, m3) => {
// Remove backslash from \' in single quoted values.
/* if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'")); */
// Remove backslash from \" in double quoted values.
/* else */
// Remove backslash from \' in single quoted values.
/* if (m1 !== undefined) a.push(m1.replace(/\\'/g, "'")); */
// Remove backslash from \" in double quoted values.
/* else */
if (m2 !== undefined) a.push(m2.replace(/\\"/g, '"'));
else if (m3 !== undefined) a.push(m3);
return []; // Return empty string.
});
// Handle special case of empty last value.
// Handle special case of empty last value.
if (/,\s*$/.test(string)) {
a.push('');
}
Expand Down

0 comments on commit a759884

Please sign in to comment.