Skip to content

Commit

Permalink
fix(csv-stringify): bom and header in sync mode with no records (fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jun 14, 2022
1 parent 2807d29 commit 16135c6
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 36 deletions.
14 changes: 8 additions & 6 deletions packages/csv-stringify/dist/cjs/sync.cjs
Expand Up @@ -536,18 +536,20 @@ const stringify = function(records, opts={}){
records: 0
};
const api = stringifier(options, state, info);
// stringifier.push = function(record){
// if(record === null){
// return;
// }
// data.push(record.toString());
// };
for(const record of records){
const err = api.__transform(record, function(record){
data.push(record);
});
if(err !== undefined) throw err;
}
if(data.length === 0){
api.bom((d) => {
data.push(d);
});
api.headers((headers) => {
data.push(headers);
});
}
return data.join('');
};

Expand Down
14 changes: 8 additions & 6 deletions packages/csv-stringify/dist/esm/sync.js
Expand Up @@ -2504,18 +2504,20 @@ const stringify = function(records, opts={}){
records: 0
};
const api = stringifier(options, state, info);
// stringifier.push = function(record){
// if(record === null){
// return;
// }
// data.push(record.toString());
// };
for(const record of records){
const err = api.__transform(record, function(record){
data.push(record);
});
if(err !== undefined) throw err;
}
if(data.length === 0){
api.bom( (d) => {
data.push(d);
});
api.headers((headers) => {
data.push(headers);
});
}
return data.join('');
};

Expand Down
14 changes: 8 additions & 6 deletions packages/csv-stringify/dist/iife/sync.js
Expand Up @@ -2507,18 +2507,20 @@ var csv_stringify_sync = (function (exports) {
records: 0
};
const api = stringifier(options, state, info);
// stringifier.push = function(record){
// if(record === null){
// return;
// }
// data.push(record.toString());
// };
for(const record of records){
const err = api.__transform(record, function(record){
data.push(record);
});
if(err !== undefined) throw err;
}
if(data.length === 0){
api.bom( (d) => {
data.push(d);
});
api.headers((headers) => {
data.push(headers);
});
}
return data.join('');
};

Expand Down
14 changes: 8 additions & 6 deletions packages/csv-stringify/dist/umd/sync.js
Expand Up @@ -2510,18 +2510,20 @@
records: 0
};
const api = stringifier(options, state, info);
// stringifier.push = function(record){
// if(record === null){
// return;
// }
// data.push(record.toString());
// };
for(const record of records){
const err = api.__transform(record, function(record){
data.push(record);
});
if(err !== undefined) throw err;
}
if(data.length === 0){
api.bom( (d) => {
data.push(d);
});
api.headers((headers) => {
data.push(headers);
});
}
return data.join('');
};

Expand Down
15 changes: 8 additions & 7 deletions packages/csv-stringify/lib/sync.js
Expand Up @@ -14,20 +14,21 @@ const stringify = function(records, opts={}){
records: 0
};
const api = stringifier(options, state, info);
// stringifier.push = function(record){
// if(record === null){
// return;
// }
// data.push(record.toString());
// };
for(const record of records){
const err = api.__transform(record, function(record){
data.push(record);
});
if(err !== undefined) throw err;
}
if(data.length === 0){
api.bom((d) => {
data.push(d);
});
const err = api.headers((headers) => {
data.push(headers);
});
}
return data.join('');
};

// export default stringify
export { stringify };
10 changes: 10 additions & 0 deletions packages/csv-stringify/test/option.bom.coffee
Expand Up @@ -11,6 +11,11 @@ describe 'Option `bom`', ->
code: 'CSV_OPTION_BOOLEAN_INVALID_TYPE'
message: 'option `bom` is optional and must be a boolean value, got "invalid"'

it 'empty', (next) ->
stringify [], bom: true, (err, data) ->
data.should.eql Buffer.from([239, 187, 191]).toString()
next()

it 'value is `true`', (next) ->
stringify [
value: 'ok'
Expand All @@ -26,13 +31,18 @@ describe 'Option `bom`', ->
next()

describe 'sync ', ->

it 'validate', ->
(->
stringifySync [], bom: 'invalid'
).should.throw
code: 'CSV_OPTION_BOOLEAN_INVALID_TYPE'
message: 'option `bom` is optional and must be a boolean value, got "invalid"'

it 'empty', ->
data = stringifySync [], bom: true
data.should.eql Buffer.from([239, 187, 191]).toString()

it 'value is `true`', ->
res = stringifySync [
value: 'ok'
Expand Down
15 changes: 10 additions & 5 deletions packages/csv-stringify/test/option.header.coffee
@@ -1,5 +1,6 @@

import { stringify } from '../lib/index.js'
import { stringify as stringifySync } from '../lib/sync.js'

describe 'Option `header`', ->

Expand Down Expand Up @@ -64,12 +65,16 @@ describe 'Option `header`', ->

describe 'without records', ->

it 'should print headers if no records to parse', (next) ->
it 'print headers if no records to parse', (next) ->
stringify [], header: true, columns: ['some', 'headers'], (err, data) ->
data.should.eql 'some,headers\n'
next()

it 'should not print headers if no records to parse and no header option', (next) ->
it 'print headers if no records to parse in sync mode, fix #343', ->
data = stringifySync [], header: true, columns: ['some', 'headers']
data.should.eql 'some,headers\n'

it 'not print headers if no records to parse and no header option', (next) ->
stringify [], header: false, columns: ['some', 'headers'], (err, data) ->
data.should.eql ''
next()
Expand Down Expand Up @@ -100,15 +105,15 @@ describe 'Option `header`', ->
"""
next()

it 'should map the column property name to display name', (next) ->
it 'map the column property name to display name', (next) ->
stringify [
{ field1: 'val11', field2: 'val12', field3: 'val13' }
{ field1: 'val21', field2: 'val22', field3: 'val23' }
], header: true, columns: {field1: 'column1', field3: 'column3'}, (err, data) ->
data.should.eql 'column1,column3\nval11,val13\nval21,val23\n' unless err
next err

it 'should map the column property name to display name', (next) ->
it 'map the column property name to display name', (next) ->
stringify [
{ field1: 'val11', field2: 'val12', field3: 'val13' }
{ field1: 'val21', field2: 'val22', field3: 'val23' }
Expand All @@ -126,7 +131,7 @@ describe 'Option `header`', ->
data.should.eql 'column1,column3\nval11,val13\n,val23\n' unless err
next err

it 'should also work for nested properties', (next) ->
it 'also work for nested properties', (next) ->
stringify [
{ field1: {nested: 'val11'}, field2: 'val12', field3: 'val13' }
{ field1: {}, field2: 'val22', field3: 'val23' }
Expand Down

0 comments on commit 16135c6

Please sign in to comment.