When using the writeToString() function, the writeBOM option doesn't add a Byte Order Mark (BOM) to the target file.
In our application, we have a feature to download CSV data. To ensure these files open correctly in Excel, a BOM is necessary. We tried to implement this functionality with the following code, creating a simple CSV file with only a header on the first line.
// generate CSV function in our product
async generate(headers: string[]): Promise<String> {
return writeToString([], {
headers: headers,
alwaysWriteHeaders: true,
writeBOM: true,
});
}
However, the BOM wasn't included. We then tried an alternative approach using the writeToBuffer() function with the following implementation, but this also didn't resolve the issue.
// generate CSV function in our product
async generate(headers: string[]): Promise<Uint8Array> {
return writeToBuffer([], {
headers: headers,
alwaysWriteHeaders: true,
writeBOM: true,
});
}
Finally, the following implementation successfully added the BOM, though it doesn't seem to be the intended use of the arguments. For this specific scenario, wouldn't it be more logical to specify the options as follows?
- First argument: an empty array
- headers: the array of headers to be specified
- alwaysWriteHeaders: true
- writeBOM: true
// generate CSV function in our product
async generate(headers: string[]): Promise<Uint8Array> {
return writeToBuffer([headers], {
writeBOM: true,
});
}
Thank you for reading all the way through; we'd appreciate it if you could confirm our findings.
When using the
writeToString()function, thewriteBOMoption doesn't add a Byte Order Mark (BOM) to the target file.In our application, we have a feature to download CSV data. To ensure these files open correctly in Excel, a BOM is necessary. We tried to implement this functionality with the following code, creating a simple CSV file with only a header on the first line.
However, the BOM wasn't included. We then tried an alternative approach using the
writeToBuffer()function with the following implementation, but this also didn't resolve the issue.Finally, the following implementation successfully added the BOM, though it doesn't seem to be the intended use of the arguments. For this specific scenario, wouldn't it be more logical to specify the options as follows?
Thank you for reading all the way through; we'd appreciate it if you could confirm our findings.