mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-17 00:50:05 +00:00
fix(convert-to-csv): fix bug with escaping quoted string
This commit is contained in:
27
src/utils/convertToCsv.spec.ts
Normal file
27
src/utils/convertToCsv.spec.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { convertToCSV } from './convertToCsv'
|
||||||
|
|
||||||
|
describe('convertToCsv', () => {
|
||||||
|
it('should convert single quoted values', () => {
|
||||||
|
const data = [
|
||||||
|
{ foo: `'bar'`, bar: 'abc' },
|
||||||
|
{ foo: 'sadf', bar: 'def' },
|
||||||
|
{ foo: 'asd', bar: `'qwert'` }
|
||||||
|
]
|
||||||
|
|
||||||
|
const expectedOutput = `foo:$5. bar:$7.\r\n"'bar'",abc\r\nsadf,def\r\nasd,"'qwert'"`
|
||||||
|
|
||||||
|
expect(convertToCSV(data)).toEqual(expectedOutput)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should convert double quoted values', () => {
|
||||||
|
const data = [
|
||||||
|
{ foo: `"bar"`, bar: 'abc' },
|
||||||
|
{ foo: 'sadf', bar: 'def' },
|
||||||
|
{ foo: 'asd', bar: `"qwert"` }
|
||||||
|
]
|
||||||
|
|
||||||
|
const expectedOutput = `foo:$5. bar:$7.\r\n"""bar""",abc\r\nsadf,def\r\nasd,"""qwert"""`
|
||||||
|
|
||||||
|
expect(convertToCSV(data)).toEqual(expectedOutput)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Converts the given JSON object to a CSV string.
|
* Converts the given JSON object array to a CSV string.
|
||||||
* @param data - the JSON object to convert.
|
* @param data - the array of JSON objects to convert.
|
||||||
*/
|
*/
|
||||||
export const convertToCSV = (data: any) => {
|
export const convertToCSV = (data: any) => {
|
||||||
const replacer = (key: any, value: any) => (value === null ? '' : value)
|
const replacer = (key: any, value: any) => (value === null ? '' : value)
|
||||||
@@ -37,15 +37,7 @@ export const convertToCSV = (data: any) => {
|
|||||||
let byteSize
|
let byteSize
|
||||||
|
|
||||||
if (typeof row[field] === 'string') {
|
if (typeof row[field] === 'string') {
|
||||||
let doubleQuotesFound = row[field]
|
|
||||||
.split('')
|
|
||||||
.filter((char: any) => char === '"')
|
|
||||||
|
|
||||||
byteSize = getByteSize(row[field])
|
byteSize = getByteSize(row[field])
|
||||||
|
|
||||||
if (doubleQuotesFound.length > 0) {
|
|
||||||
byteSize += doubleQuotesFound.length
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return byteSize
|
return byteSize
|
||||||
@@ -73,6 +65,7 @@ export const convertToCSV = (data: any) => {
|
|||||||
if (invalidString) {
|
if (invalidString) {
|
||||||
return 'ERROR: LARGE STRING LENGTH'
|
return 'ERROR: LARGE STRING LENGTH'
|
||||||
}
|
}
|
||||||
|
|
||||||
csvTest = data.map((row: any) => {
|
csvTest = data.map((row: any) => {
|
||||||
const fields = Object.keys(row).map((fieldName, index) => {
|
const fields = Object.keys(row).map((fieldName, index) => {
|
||||||
let value
|
let value
|
||||||
@@ -80,12 +73,11 @@ export const convertToCSV = (data: any) => {
|
|||||||
const currentCell = row[fieldName]
|
const currentCell = row[fieldName]
|
||||||
|
|
||||||
if (JSON.stringify(currentCell).search(/(\\t|\\n|\\r)/gm) > -1) {
|
if (JSON.stringify(currentCell).search(/(\\t|\\n|\\r)/gm) > -1) {
|
||||||
value = currentCell.toString()
|
|
||||||
containsSpecialChar = true
|
containsSpecialChar = true
|
||||||
} else {
|
|
||||||
value = JSON.stringify(currentCell, replacer)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
value = JSON.stringify(currentCell, replacer)
|
||||||
|
|
||||||
value = value.replace(/\\\\/gm, '\\')
|
value = value.replace(/\\\\/gm, '\\')
|
||||||
|
|
||||||
if (containsSpecialChar) {
|
if (containsSpecialChar) {
|
||||||
@@ -101,6 +93,10 @@ export const convertToCSV = (data: any) => {
|
|||||||
value = value.substring(1, value.length - 1)
|
value = value.substring(1, value.length - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value.includes("'")) {
|
||||||
|
value = '"' + value + '"'
|
||||||
|
}
|
||||||
|
|
||||||
value = value.replace(/\\"/gm, '""')
|
value = value.replace(/\\"/gm, '""')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user