1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-09 13:30:04 +00:00

fix(convert-to-csv): fix bug with escaping quoted string

This commit is contained in:
Krishna Acondy
2021-05-06 12:44:08 +01:00
parent bb2ad5bb9a
commit a32c0879b3
2 changed files with 36 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
/**
* Converts the given JSON object to a CSV string.
* @param data - the JSON object to convert.
* Converts the given JSON object array to a CSV string.
* @param data - the array of JSON objects to convert.
*/
export const convertToCSV = (data: any) => {
const replacer = (key: any, value: any) => (value === null ? '' : value)
@@ -37,15 +37,7 @@ export const convertToCSV = (data: any) => {
let byteSize
if (typeof row[field] === 'string') {
let doubleQuotesFound = row[field]
.split('')
.filter((char: any) => char === '"')
byteSize = getByteSize(row[field])
if (doubleQuotesFound.length > 0) {
byteSize += doubleQuotesFound.length
}
}
return byteSize
@@ -73,6 +65,7 @@ export const convertToCSV = (data: any) => {
if (invalidString) {
return 'ERROR: LARGE STRING LENGTH'
}
csvTest = data.map((row: any) => {
const fields = Object.keys(row).map((fieldName, index) => {
let value
@@ -80,12 +73,11 @@ export const convertToCSV = (data: any) => {
const currentCell = row[fieldName]
if (JSON.stringify(currentCell).search(/(\\t|\\n|\\r)/gm) > -1) {
value = currentCell.toString()
containsSpecialChar = true
} else {
value = JSON.stringify(currentCell, replacer)
}
value = JSON.stringify(currentCell, replacer)
value = value.replace(/\\\\/gm, '\\')
if (containsSpecialChar) {
@@ -101,6 +93,10 @@ export const convertToCSV = (data: any) => {
value = value.substring(1, value.length - 1)
}
if (value.includes("'")) {
value = '"' + value + '"'
}
value = value.replace(/\\"/gm, '""')
}