Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records list now accepts any iterable, not only an array #11

Merged
merged 3 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/csv-stringifiers/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class AbstractCsvStringifier {
}

stringifyRecords(records) {
const csvLines = records
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it also work if we just change records to Array.from(records)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be functional, but it would be very inefficient.

.map(record => this._getRecordAsArray(record))
.map(record => this._getCsvLine(record));
return csvLines.join(RECORD_DELIMITER) + RECORD_DELIMITER;
const array = [];
for (let record of records) {
array.push(this._getCsvLine(this._getRecordAsArray(record)));
}
array.push('');
return array.join(RECORD_DELIMITER);
}

/* istanbul ignore next */_getRecordAsArray(_record) {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/csv-stringifiers/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ describe('ArrayCsvStringifier', () => {
});
});

describe('When records input is an iterable other than an array', () => {
it('behaves the same as when the input was an array', () => {
const stringifier = createArrayCsvStringifier({
header: ['TITLE_A', 'TITLE_B']
});
function * recordGenerator() {
yield records[0];
yield records[1];
}
const recordArray = Array.from(recordGenerator());
const generatorOutput = stringifier.stringifyRecords(recordGenerator());
const arrayOutput = stringifier.stringifyRecords(recordArray);
assert.equal(generatorOutput, arrayOutput);
});
});

function generateTestCases(fieldDelimiter) {
const delim = resolveDelimiterChar(fieldDelimiter);
return () => {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/csv-stringifiers/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ describe('ObjectCsvStringifier', () => {
});
});

describe('When records input is an iterable other than an array', () => {
it('behaves the same as when the input was an array', () => {
const stringifier = createObjectCsvStringifier({
header: ['TITLE_A', 'TITLE_B']
});
function * recordGenerator() {
yield records[0];
yield records[1];
}
const recordArray = Array.from(recordGenerator());
const generatorOutput = stringifier.stringifyRecords(recordGenerator());
const arrayOutput = stringifier.stringifyRecords(recordArray);
assert.equal(generatorOutput, arrayOutput);
});
});

function generateTestCases(fieldDelimiter) {
const delim = resolveDelimiterChar(fieldDelimiter);
return () => {
Expand Down