mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-08 13:00:05 +00:00
feat(nullVars): add SAS null vars support
This commit is contained in:
@@ -2,70 +2,92 @@
|
||||
* 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)
|
||||
const headerFields = Object.keys(data[0])
|
||||
export const convertToCSV = (
|
||||
data: any,
|
||||
sasFormats?: { formats: { [key: string]: string } }
|
||||
) => {
|
||||
const formats = sasFormats?.formats
|
||||
let headers: string[] = []
|
||||
let csvTest
|
||||
let invalidString = false
|
||||
const headers = headerFields.map((field) => {
|
||||
let firstFoundType: string | null = null
|
||||
let hasMixedTypes: boolean = false
|
||||
let rowNumError: number = -1
|
||||
|
||||
const longestValueForField = data
|
||||
.map((row: any, index: number) => {
|
||||
if (row[field] || row[field] === '') {
|
||||
if (firstFoundType) {
|
||||
let currentFieldType =
|
||||
row[field] === '' || typeof row[field] === 'string'
|
||||
? 'chars'
|
||||
: 'number'
|
||||
if (formats) {
|
||||
headers = Object.keys(formats).map((key) => `${key}:${formats[key]}`)
|
||||
}
|
||||
|
||||
if (!hasMixedTypes) {
|
||||
hasMixedTypes = currentFieldType !== firstFoundType
|
||||
rowNumError = hasMixedTypes ? index + 1 : -1
|
||||
}
|
||||
} else {
|
||||
if (row[field] === '') {
|
||||
firstFoundType = 'chars'
|
||||
const headerFields = Object.keys(data[0])
|
||||
|
||||
headerFields.forEach((field) => {
|
||||
if (!formats || !Object.keys(formats).includes(field)) {
|
||||
let firstFoundType: string | null = null
|
||||
let hasMixedTypes: boolean = false
|
||||
let rowNumError: number = -1
|
||||
|
||||
const longestValueForField = data
|
||||
.map((row: any, index: number) => {
|
||||
if (row[field] || row[field] === '') {
|
||||
if (firstFoundType) {
|
||||
let currentFieldType =
|
||||
row[field] === '' || typeof row[field] === 'string'
|
||||
? 'chars'
|
||||
: 'number'
|
||||
|
||||
if (!hasMixedTypes) {
|
||||
hasMixedTypes = currentFieldType !== firstFoundType
|
||||
rowNumError = hasMixedTypes ? index + 1 : -1
|
||||
}
|
||||
} else {
|
||||
firstFoundType =
|
||||
typeof row[field] === 'string' ? 'chars' : 'number'
|
||||
if (row[field] === '') {
|
||||
firstFoundType = 'chars'
|
||||
} else {
|
||||
firstFoundType =
|
||||
typeof row[field] === 'string' ? 'chars' : 'number'
|
||||
}
|
||||
}
|
||||
|
||||
let byteSize
|
||||
|
||||
if (typeof row[field] === 'string') {
|
||||
byteSize = getByteSize(row[field])
|
||||
}
|
||||
|
||||
return byteSize
|
||||
}
|
||||
})
|
||||
.sort((a: number, b: number) => b - a)[0]
|
||||
|
||||
let byteSize
|
||||
if (longestValueForField && longestValueForField > 32765) {
|
||||
invalidString = true
|
||||
}
|
||||
|
||||
if (typeof row[field] === 'string') {
|
||||
byteSize = getByteSize(row[field])
|
||||
}
|
||||
if (hasMixedTypes) {
|
||||
console.error(
|
||||
`Row (${rowNumError}), Column (${field}) has mixed types: ERROR`
|
||||
)
|
||||
}
|
||||
|
||||
return byteSize
|
||||
}
|
||||
})
|
||||
.sort((a: number, b: number) => b - a)[0]
|
||||
if (longestValueForField && longestValueForField > 32765) {
|
||||
invalidString = true
|
||||
}
|
||||
if (hasMixedTypes) {
|
||||
console.error(
|
||||
`Row (${rowNumError}), Column (${field}) has mixed types: ERROR`
|
||||
headers.push(
|
||||
`${field}:${firstFoundType === 'chars' ? '$char' : ''}${
|
||||
longestValueForField
|
||||
? longestValueForField
|
||||
: firstFoundType === 'chars'
|
||||
? '1'
|
||||
: 'best'
|
||||
}.`
|
||||
)
|
||||
}
|
||||
|
||||
return `${field}:${firstFoundType === 'chars' ? '$char' : ''}${
|
||||
longestValueForField
|
||||
? longestValueForField
|
||||
: firstFoundType === 'chars'
|
||||
? '1'
|
||||
: 'best'
|
||||
}.`
|
||||
})
|
||||
|
||||
if (invalidString) {
|
||||
return 'ERROR: LARGE STRING LENGTH'
|
||||
if (sasFormats) {
|
||||
headers = headers.sort(
|
||||
(a, b) =>
|
||||
headerFields.indexOf(a.replace(/:.*/, '')) -
|
||||
headerFields.indexOf(b.replace(/:.*/, ''))
|
||||
)
|
||||
}
|
||||
|
||||
if (invalidString) return 'ERROR: LARGE STRING LENGTH'
|
||||
|
||||
csvTest = data.map((row: any) => {
|
||||
const fields = Object.keys(row).map((fieldName, index) => {
|
||||
let value
|
||||
@@ -76,6 +98,10 @@ export const convertToCSV = (data: any) => {
|
||||
// stringify with replacer converts null values to empty strings
|
||||
value = currentCell === null ? '' : currentCell
|
||||
|
||||
if (formats && formats[fieldName] === 'best.') {
|
||||
return `.${value.toLowerCase()}`
|
||||
}
|
||||
|
||||
// if there any present, it should have preceding (") for escaping
|
||||
value = value.replace(/"/g, `""`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user