Skip to content

Commit 064a507

Browse files
committed
Add CSV BOM option
1 parent 8b50f42 commit 064a507

7 files changed

Lines changed: 26 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this package are documented here.
44

5+
## 0.1.1 - 2026-05-12
6+
7+
- Added an optional `bom` export setting for spreadsheet apps that need a UTF-8 byte order mark.
8+
59
## 0.1.0 - 2026-05-12
610

711
- Initial release with JSON records to CSV conversion.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ value
213213
'=SUM(A1:A2)
214214
```
215215

216+
Use `bom: true` when a spreadsheet app needs the UTF-8 byte order mark to detect accented characters correctly.
217+
216218
## Options
217219

218220
```ts
@@ -228,6 +230,7 @@ interface JsonToCsvOptions<TRecord> {
228230
arrayMode?: 'json' | 'join' | 'empty';
229231
arraySeparator?: string;
230232
escapeFormulae?: boolean | string;
233+
bom?: boolean;
231234
dateFormatter?: (date: Date) => string;
232235
}
233236
```
@@ -245,6 +248,7 @@ interface JsonToCsvOptions<TRecord> {
245248
| `arrayMode` | `'json'` | format arrays as JSON, joined text or empty |
246249
| `arraySeparator` | `', '` | separator used by `arrayMode: 'join'` |
247250
| `escapeFormulae` | `false` | prefix spreadsheet-like formulas |
251+
| `bom` | `false` | prefix the output with a UTF-8 BOM for spreadsheet apps |
248252
| `dateFormatter` | ISO string | format `Date` values |
249253

250254
## Arrays, dates and null values

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-csv-kit",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Convert JSON records to clean CSV with TypeScript-first options.",
55
"type": "module",
66
"sideEffects": false,

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export function jsonToCsv<TRecord extends object>(
5555
lines.push(formatRow(row, resolved));
5656
}
5757

58-
return lines.join(resolved.newline);
58+
const csv = lines.join(resolved.newline);
59+
return resolved.bom ? `\uFEFF${csv}` : csv;
5960
}
6061

6162
export const toCsv = jsonToCsv;
@@ -107,6 +108,7 @@ function resolveOptions<TRecord extends object>(
107108
arrayMode,
108109
arraySeparator,
109110
escapeFormulae,
111+
bom: options.bom ?? false,
110112
dateFormatter: options.dateFormatter ?? ((date) => date.toISOString())
111113
};
112114
}

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ export interface JsonToCsvOptions<TRecord extends object = CsvRecord> {
115115
*/
116116
escapeFormulae?: boolean | string;
117117

118+
/**
119+
* Prefix the CSV with a UTF-8 byte order mark for spreadsheet apps.
120+
*
121+
* @default false
122+
*/
123+
bom?: boolean;
124+
118125
/**
119126
* Format Date instances.
120127
*
@@ -135,6 +142,7 @@ export interface ResolvedJsonToCsvOptions<TRecord extends object = CsvRecord> {
135142
arrayMode: ArrayValueMode;
136143
arraySeparator: string;
137144
escapeFormulae: false | string;
145+
bom: boolean;
138146
dateFormatter: (date: Date) => string;
139147
}
140148

tests/index.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ describe('json-csv-kit', () => {
138138
).toBe('name;role\r\nAda;Engineer');
139139
});
140140

141+
test('can prefix CSV output with a UTF-8 BOM', () => {
142+
expect(jsonToCsv([{ name: 'Ada' }], { bom: true })).toBe('\uFEFFname\nAda');
143+
});
144+
141145
test('supports null values and dates', () => {
142146
expect(
143147
jsonToCsv(

0 commit comments

Comments
 (0)