1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-13 23:20:05 +00:00

refactor(convert-to-csv): change func arguments

This commit is contained in:
Yury Shkoda
2022-03-03 15:07:01 +03:00
parent ff5dc5f196
commit 5927910a52
7 changed files with 165 additions and 112 deletions

View File

@@ -3,10 +3,15 @@
* @param data - the array of JSON objects to convert.
*/
export const convertToCSV = (
data: any[],
sasFormats?: { formats: { [key: string]: string } }
data: { [key: string]: any },
tableName: string
) => {
let formats = sasFormats?.formats
if (!data[tableName]) {
throw new Error('No table provided to be converted to CSV')
}
const table = data[tableName]
let formats = data[`$${tableName}`]?.formats
let headers: string[] = []
let csvTest
let invalidString = false
@@ -16,14 +21,14 @@ export const convertToCSV = (
headers = Object.keys(formats).map((key) => `${key}:${formats![key]}`)
}
const headerFields = Object.keys(data[0])
const headerFields = Object.keys(table[0])
headerFields.forEach((field) => {
if (!formats || !Object.keys(formats).includes(field)) {
let hasNullOrNumber = false
let hasSpecialMissingString = false
data.forEach((row: { [key: string]: any }) => {
table.forEach((row: { [key: string]: any }) => {
if (row[field] === null || typeof row[field] === 'number') {
hasNullOrNumber = true
} else if (
@@ -45,7 +50,7 @@ export const convertToCSV = (
let hasMixedTypes: boolean = false
let rowNumError: number = -1
const longestValueForField = data
const longestValueForField = table
.map((row: any, index: number) => {
if (row[field] || row[field] === '') {
if (firstFoundType) {
@@ -101,7 +106,7 @@ export const convertToCSV = (
}
})
if (sasFormats) {
if (formats) {
headers = headers.sort(
(a, b) =>
headerFields.indexOf(a.replace(/:.*/, '')) -
@@ -111,7 +116,7 @@ export const convertToCSV = (
if (invalidString) return 'ERROR: LARGE STRING LENGTH'
csvTest = data.map((row: any) => {
csvTest = table.map((row: any) => {
const fields = Object.keys(row).map((fieldName, index) => {
let value
const currentCell = row[fieldName]