Skip to content

Commit 087e580

Browse files
vershwalPrinci Vershwal
andauthored
🐛 Handled BOM character for Unicode encoded file uploads (#17104)
fixes TryGhost/Ghost#16917 refs TryGhost/Ghost#16917 (comment) Co-authored-by: Princi Vershwal <[email protected]>
1 parent c86fb64 commit 087e580

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

packages/members-csv/lib/parse.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ module.exports = (path, headerMapping, defaultLabels = []) => {
1515
const csvParserStream = papaparse.parse(papaparse.NODE_STREAM_INPUT, {
1616
header: true,
1717
transformHeader(_header) {
18-
if (!headerMapping || !Reflect.has(headerMapping, _header)) {
18+
const cleanHeader = _header.replace(papaparse.BYTE_ORDER_MARK, ''); //Removing BOM characters for Unicode-based encodings
19+
if (!headerMapping || !Reflect.has(headerMapping, cleanHeader)) {
1920
return undefined;
2021
}
21-
22-
return headerMapping[_header];
22+
return headerMapping[cleanHeader];
2323
},
2424
transform(value, header) {
2525
if (header === 'labels') {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
email
2+
3+

packages/members-csv/test/parse.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ describe('parse', function () {
3535
assert.equal(result.length, 0);
3636
});
3737

38+
it('one column with bom', async function () {
39+
const filePath = csvPath + 'single-column-with-header-bom.csv';
40+
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);
41+
42+
assert.ok(result);
43+
assert.equal(result.length, 2);
44+
assert.equal(result[0].email, '[email protected]');
45+
assert.equal(result[1].email, '[email protected]');
46+
});
47+
48+
it('one column with bom and without header mapping returns empty result', async function () {
49+
const filePath = csvPath + 'single-column-with-header-bom.csv';
50+
const result = await parse(filePath);
51+
52+
assert.ok(result);
53+
assert.equal(result.length, 0);
54+
});
55+
3856
it('two columns, 1 filter', async function () {
3957
const filePath = csvPath + 'two-columns-with-header.csv';
4058
const result = await parse(filePath, DEFAULT_HEADER_MAPPING);

0 commit comments

Comments
 (0)