Skip to content

Commit 033d929

Browse files
committed
Removed hidden row mapping in csv parser
refs https://github.com/TryGhost/Toolbox/issues/430 refs TryGhost/Ghost#14882 - Having an explicit mappings passed into the members CSV parser makes it easier to control and understand the transforms for package clients - Eventually the parser will receive a strict map with the fields it should parse - skipping all unknown & unmapped fields
1 parent 3450b4b commit 033d929

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

packages/members-csv/lib/parse.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,19 @@ const fs = require('fs-extra');
66
/**
77
*
88
* @param {string} path - The path to the CSV to prepare
9-
* @param {Object.<string, string>} mapping - An object whose keys are headers in the input CSV and values are the header to replace it with
9+
* @param {Object.<string, string>} headerMapping - An object whose keys are headers in the input CSV and values are the header to replace it with
1010
* @param {Array<string>} [defaultLabels] - A list of labels to apply to every parsed member row
1111
* @returns
1212
*/
13-
module.exports = (path, mapping, defaultLabels = []) => {
14-
const inputMapping = Object.assign({}, mapping, {
15-
subscribed_to_emails: 'subscribed'
16-
});
17-
13+
module.exports = (path, headerMapping = {}, defaultLabels = []) => {
1814
return new Promise(function (resolve, reject) {
1915
const csvFileStream = fs.createReadStream(path);
2016
const csvParserStream = papaparse.parse(papaparse.NODE_STREAM_INPUT, {
2117
header: true,
2218
transformHeader(_header) {
2319
let header = _header;
24-
if (inputMapping && Reflect.has(inputMapping, _header)) {
25-
header = inputMapping[_header];
20+
if (headerMapping && Reflect.has(headerMapping, _header)) {
21+
header = headerMapping[_header];
2622
}
2723

2824
return header;

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,13 @@ describe('parse', function () {
124124
should.equal(result[1].name, null);
125125
});
126126

127-
it('read csv: transforms "subscribed_to_emails" column to "subscribed" property', async function () {
127+
it('read csv: transforms "subscribed_to_emails" column to "subscribed" property when the mapping is passed in', async function () {
128+
const mapping = {
129+
subscribed_to_emails: 'subscribed'
130+
};
128131
const result = await readCSV({
129-
filePath: csvPath + 'subscribed-to-emails-header.csv'
132+
filePath: csvPath + 'subscribed-to-emails-header.csv',
133+
mapping
130134
});
131135

132136
assert.ok(result);
@@ -137,4 +141,18 @@ describe('parse', function () {
137141
assert.equal(result[1].email, '[email protected]');
138142
assert.equal(result[1].subscribed, false);
139143
});
144+
145+
it('read csv: DOES NOT transforms "subscribed_to_emails" column to "subscribed" property when the WITHOUT mapping', async function () {
146+
const result = await readCSV({
147+
filePath: csvPath + 'subscribed-to-emails-header.csv'
148+
});
149+
150+
assert.ok(result);
151+
assert.equal(result.length, 2);
152+
assert.equal(result[0].email, '[email protected]');
153+
assert.ok(result[0].subscribed_to_emails);
154+
155+
assert.equal(result[1].email, '[email protected]');
156+
assert.equal(result[1].subscribed_to_emails, false);
157+
});
140158
});

0 commit comments

Comments
 (0)