mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-08 13:00:05 +00:00
chore(*): change code style to remove semicolons
This commit is contained in:
@@ -3,14 +3,14 @@
|
||||
* @param data - the JSON object to convert.
|
||||
*/
|
||||
export const convertToCSV = (data: any) => {
|
||||
const replacer = (key: any, value: any) => (value === null ? "" : value);
|
||||
const headerFields = Object.keys(data[0]);
|
||||
let csvTest;
|
||||
let invalidString = false;
|
||||
const replacer = (key: any, value: any) => (value === null ? "" : value)
|
||||
const headerFields = Object.keys(data[0])
|
||||
let csvTest
|
||||
let invalidString = false
|
||||
const headers = headerFields.map((field) => {
|
||||
let firstFoundType: string | null = null;
|
||||
let hasMixedTypes: boolean = false;
|
||||
let rowNumError: number = -1;
|
||||
let firstFoundType: string | null = null
|
||||
let hasMixedTypes: boolean = false
|
||||
let rowNumError: number = -1
|
||||
|
||||
const longestValueForField = data
|
||||
.map((row: any, index: number) => {
|
||||
@@ -19,46 +19,46 @@ export const convertToCSV = (data: any) => {
|
||||
let currentFieldType =
|
||||
row[field] === "" || typeof row[field] === "string"
|
||||
? "chars"
|
||||
: "number";
|
||||
: "number"
|
||||
|
||||
if (!hasMixedTypes) {
|
||||
hasMixedTypes = currentFieldType !== firstFoundType;
|
||||
rowNumError = hasMixedTypes ? index + 1 : -1;
|
||||
hasMixedTypes = currentFieldType !== firstFoundType
|
||||
rowNumError = hasMixedTypes ? index + 1 : -1
|
||||
}
|
||||
} else {
|
||||
if (row[field] === "") {
|
||||
firstFoundType = "chars";
|
||||
firstFoundType = "chars"
|
||||
} else {
|
||||
firstFoundType =
|
||||
typeof row[field] === "string" ? "chars" : "number";
|
||||
typeof row[field] === "string" ? "chars" : "number"
|
||||
}
|
||||
}
|
||||
|
||||
let byteSize;
|
||||
let byteSize
|
||||
|
||||
if (typeof row[field] === "string") {
|
||||
let doubleQuotesFound = row[field]
|
||||
.split("")
|
||||
.filter((char: any) => char === '"');
|
||||
.filter((char: any) => char === '"')
|
||||
|
||||
byteSize = getByteSize(row[field]);
|
||||
byteSize = getByteSize(row[field])
|
||||
|
||||
if (doubleQuotesFound.length > 0) {
|
||||
byteSize += doubleQuotesFound.length;
|
||||
byteSize += doubleQuotesFound.length
|
||||
}
|
||||
}
|
||||
|
||||
return byteSize;
|
||||
return byteSize
|
||||
}
|
||||
})
|
||||
.sort((a: number, b: number) => b - a)[0];
|
||||
.sort((a: number, b: number) => b - a)[0]
|
||||
if (longestValueForField && longestValueForField > 32765) {
|
||||
invalidString = true;
|
||||
invalidString = true
|
||||
}
|
||||
if (hasMixedTypes) {
|
||||
console.error(
|
||||
`Row (${rowNumError}), Column (${field}) has mixed types: ERROR`
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
return `${field}:${firstFoundType === "chars" ? "$" : ""}${
|
||||
@@ -67,30 +67,30 @@ export const convertToCSV = (data: any) => {
|
||||
: firstFoundType === "chars"
|
||||
? "1"
|
||||
: "best"
|
||||
}.`;
|
||||
});
|
||||
}.`
|
||||
})
|
||||
|
||||
if (invalidString) {
|
||||
return "ERROR: LARGE STRING LENGTH";
|
||||
return "ERROR: LARGE STRING LENGTH"
|
||||
}
|
||||
csvTest = data.map((row: any) => {
|
||||
const fields = Object.keys(row).map((fieldName, index) => {
|
||||
let value;
|
||||
let containsSpecialChar = false;
|
||||
const currentCell = row[fieldName];
|
||||
let value
|
||||
let containsSpecialChar = false
|
||||
const currentCell = row[fieldName]
|
||||
|
||||
if (JSON.stringify(currentCell).search(/(\\t|\\n|\\r)/gm) > -1) {
|
||||
value = currentCell.toString();
|
||||
containsSpecialChar = true;
|
||||
value = currentCell.toString()
|
||||
containsSpecialChar = true
|
||||
} else {
|
||||
value = JSON.stringify(currentCell, replacer);
|
||||
value = JSON.stringify(currentCell, replacer)
|
||||
}
|
||||
|
||||
value = value.replace(/\\\\/gm, "\\");
|
||||
value = value.replace(/\\\\/gm, "\\")
|
||||
|
||||
if (containsSpecialChar) {
|
||||
if (value.includes(",") || value.includes('"')) {
|
||||
value = '"' + value + '"';
|
||||
value = '"' + value + '"'
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
@@ -98,36 +98,36 @@ export const convertToCSV = (data: any) => {
|
||||
value.includes('"') &&
|
||||
!value.includes('\\"')
|
||||
) {
|
||||
value = value.substring(1, value.length - 1);
|
||||
value = value.substring(1, value.length - 1)
|
||||
}
|
||||
|
||||
value = value.replace(/\\"/gm, '""');
|
||||
value = value.replace(/\\"/gm, '""')
|
||||
}
|
||||
|
||||
value = value.replace(/\r\n/gm, "\n");
|
||||
value = value.replace(/\r\n/gm, "\n")
|
||||
|
||||
if (value === "" && headers[index].includes("best")) {
|
||||
value = ".";
|
||||
value = "."
|
||||
}
|
||||
|
||||
return value;
|
||||
});
|
||||
return fields.join(",");
|
||||
});
|
||||
return value
|
||||
})
|
||||
return fields.join(",")
|
||||
})
|
||||
|
||||
let finalCSV =
|
||||
headers.join(",").replace(/,/g, " ") + "\r\n" + csvTest.join("\r\n");
|
||||
headers.join(",").replace(/,/g, " ") + "\r\n" + csvTest.join("\r\n")
|
||||
|
||||
return finalCSV;
|
||||
};
|
||||
return finalCSV
|
||||
}
|
||||
|
||||
const getByteSize = (str: string) => {
|
||||
let byteSize = str.length;
|
||||
let byteSize = str.length
|
||||
for (let i = str.length - 1; i >= 0; i--) {
|
||||
const code = str.charCodeAt(i);
|
||||
if (code > 0x7f && code <= 0x7ff) byteSize++;
|
||||
else if (code > 0x7ff && code <= 0xffff) byteSize += 2;
|
||||
if (code >= 0xdc00 && code <= 0xdfff) i--; //trail surrogate
|
||||
const code = str.charCodeAt(i)
|
||||
if (code > 0x7f && code <= 0x7ff) byteSize++
|
||||
else if (code > 0x7ff && code <= 0xffff) byteSize += 2
|
||||
if (code >= 0xdc00 && code <= 0xdfff) i-- //trail surrogate
|
||||
}
|
||||
return byteSize;
|
||||
};
|
||||
return byteSize
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user