Skip to content

Commit bff158f

Browse files
committed
fix(csv-stringify): bom and header in sync mode with no records (fix #343)
1 parent 2807d29 commit bff158f

15 files changed

Lines changed: 100 additions & 68 deletions

File tree

packages/csv-stringify/dist/cjs/sync.cjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -536,18 +536,20 @@ const stringify = function(records, opts={}){
536536
records: 0
537537
};
538538
const api = stringifier(options, state, info);
539-
// stringifier.push = function(record){
540-
// if(record === null){
541-
// return;
542-
// }
543-
// data.push(record.toString());
544-
// };
545539
for(const record of records){
546540
const err = api.__transform(record, function(record){
547541
data.push(record);
548542
});
549543
if(err !== undefined) throw err;
550544
}
545+
if(data.length === 0){
546+
api.bom((d) => {
547+
data.push(d);
548+
});
549+
api.headers((headers) => {
550+
data.push(headers);
551+
});
552+
}
551553
return data.join('');
552554
};
553555

packages/csv-stringify/dist/esm/sync.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,18 +2504,20 @@ const stringify = function(records, opts={}){
25042504
records: 0
25052505
};
25062506
const api = stringifier(options, state, info);
2507-
// stringifier.push = function(record){
2508-
// if(record === null){
2509-
// return;
2510-
// }
2511-
// data.push(record.toString());
2512-
// };
25132507
for(const record of records){
25142508
const err = api.__transform(record, function(record){
25152509
data.push(record);
25162510
});
25172511
if(err !== undefined) throw err;
25182512
}
2513+
if(data.length === 0){
2514+
api.bom((d) => {
2515+
data.push(d);
2516+
});
2517+
api.headers((headers) => {
2518+
data.push(headers);
2519+
});
2520+
}
25192521
return data.join('');
25202522
};
25212523

packages/csv-stringify/dist/iife/sync.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,18 +2507,20 @@ var csv_stringify_sync = (function (exports) {
25072507
records: 0
25082508
};
25092509
const api = stringifier(options, state, info);
2510-
// stringifier.push = function(record){
2511-
// if(record === null){
2512-
// return;
2513-
// }
2514-
// data.push(record.toString());
2515-
// };
25162510
for(const record of records){
25172511
const err = api.__transform(record, function(record){
25182512
data.push(record);
25192513
});
25202514
if(err !== undefined) throw err;
25212515
}
2516+
if(data.length === 0){
2517+
api.bom((d) => {
2518+
data.push(d);
2519+
});
2520+
api.headers((headers) => {
2521+
data.push(headers);
2522+
});
2523+
}
25222524
return data.join('');
25232525
};
25242526

packages/csv-stringify/dist/umd/sync.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,18 +2510,20 @@
25102510
records: 0
25112511
};
25122512
const api = stringifier(options, state, info);
2513-
// stringifier.push = function(record){
2514-
// if(record === null){
2515-
// return;
2516-
// }
2517-
// data.push(record.toString());
2518-
// };
25192513
for(const record of records){
25202514
const err = api.__transform(record, function(record){
25212515
data.push(record);
25222516
});
25232517
if(err !== undefined) throw err;
25242518
}
2519+
if(data.length === 0){
2520+
api.bom((d) => {
2521+
data.push(d);
2522+
});
2523+
api.headers((headers) => {
2524+
data.push(headers);
2525+
});
2526+
}
25252527
return data.join('');
25262528
};
25272529

packages/csv-stringify/lib/sync.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@ const stringify = function(records, opts={}){
1414
records: 0
1515
};
1616
const api = stringifier(options, state, info);
17-
// stringifier.push = function(record){
18-
// if(record === null){
19-
// return;
20-
// }
21-
// data.push(record.toString());
22-
// };
2317
for(const record of records){
2418
const err = api.__transform(record, function(record){
2519
data.push(record);
2620
});
2721
if(err !== undefined) throw err;
2822
}
23+
if(data.length === 0){
24+
api.bom((d) => {
25+
data.push(d);
26+
});
27+
const err = api.headers((headers) => {
28+
data.push(headers);
29+
});
30+
}
2931
return data.join('');
3032
};
3133

32-
// export default stringify
3334
export { stringify };

packages/csv-stringify/test/option.bom.coffee

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ describe 'Option `bom`', ->
1111
code: 'CSV_OPTION_BOOLEAN_INVALID_TYPE'
1212
message: 'option `bom` is optional and must be a boolean value, got "invalid"'
1313

14+
it 'empty', (next) ->
15+
stringify [], bom: true, (err, data) ->
16+
data.should.eql Buffer.from([239, 187, 191]).toString()
17+
next()
18+
1419
it 'value is `true`', (next) ->
1520
stringify [
1621
value: 'ok'
@@ -26,13 +31,18 @@ describe 'Option `bom`', ->
2631
next()
2732

2833
describe 'sync ', ->
34+
2935
it 'validate', ->
3036
(->
3137
stringifySync [], bom: 'invalid'
3238
).should.throw
3339
code: 'CSV_OPTION_BOOLEAN_INVALID_TYPE'
3440
message: 'option `bom` is optional and must be a boolean value, got "invalid"'
3541

42+
it 'empty', ->
43+
data = stringifySync [], bom: true
44+
data.should.eql Buffer.from([239, 187, 191]).toString()
45+
3646
it 'value is `true`', ->
3747
res = stringifySync [
3848
value: 'ok'

packages/csv-stringify/test/option.header.coffee

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import { stringify } from '../lib/index.js'
3+
import { stringify as stringifySync } from '../lib/sync.js'
34

45
describe 'Option `header`', ->
56

@@ -64,12 +65,16 @@ describe 'Option `header`', ->
6465

6566
describe 'without records', ->
6667

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

72-
it 'should not print headers if no records to parse and no header option', (next) ->
73+
it 'print headers if no records to parse in sync mode, fix #343', ->
74+
data = stringifySync [], header: true, columns: ['some', 'headers']
75+
data.should.eql 'some,headers\n'
76+
77+
it 'not print headers if no records to parse and no header option', (next) ->
7378
stringify [], header: false, columns: ['some', 'headers'], (err, data) ->
7479
data.should.eql ''
7580
next()
@@ -100,15 +105,15 @@ describe 'Option `header`', ->
100105
"""
101106
next()
102107

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

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

129-
it 'should also work for nested properties', (next) ->
134+
it 'also work for nested properties', (next) ->
130135
stringify [
131136
{ field1: {nested: 'val11'}, field2: 'val12', field3: 'val13' }
132137
{ field1: {}, field2: 'val22', field3: 'val23' }

packages/csv/dist/cjs/index.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ const normalize_options$1 = function(opts){
469469
const date = Date.parse(value);
470470
return !isNaN(date) ? new Date(date) : value;
471471
};
472-
}else {
472+
}else if (typeof options.cast_date !== 'function'){
473473
throw new CsvError$1('CSV_INVALID_OPTION_CAST_DATE', [
474474
'Invalid option cast_date:', 'cast_date must be true or a function,',
475475
`got ${JSON.stringify(options.cast_date)}`

packages/csv/dist/cjs/sync.cjs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ const normalize_options$1 = function(opts){
466466
const date = Date.parse(value);
467467
return !isNaN(date) ? new Date(date) : value;
468468
};
469-
}else {
469+
}else if (typeof options.cast_date !== 'function'){
470470
throw new CsvError$1('CSV_INVALID_OPTION_CAST_DATE', [
471471
'Invalid option cast_date:', 'cast_date must be true or a function,',
472472
`got ${JSON.stringify(options.cast_date)}`
@@ -2076,18 +2076,20 @@ const stringify = function(records, opts={}){
20762076
records: 0
20772077
};
20782078
const api = stringifier(options, state, info);
2079-
// stringifier.push = function(record){
2080-
// if(record === null){
2081-
// return;
2082-
// }
2083-
// data.push(record.toString());
2084-
// };
20852079
for(const record of records){
20862080
const err = api.__transform(record, function(record){
20872081
data.push(record);
20882082
});
20892083
if(err !== undefined) throw err;
20902084
}
2085+
if(data.length === 0){
2086+
api.bom((d) => {
2087+
data.push(d);
2088+
});
2089+
api.headers((headers) => {
2090+
data.push(headers);
2091+
});
2092+
}
20912093
return data.join('');
20922094
};
20932095

packages/csv/dist/esm/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5589,7 +5589,7 @@ const normalize_options$1 = function(opts){
55895589
const date = Date.parse(value);
55905590
return !isNaN(date) ? new Date(date) : value;
55915591
};
5592-
}else {
5592+
}else if (typeof options.cast_date !== 'function'){
55935593
throw new CsvError$1('CSV_INVALID_OPTION_CAST_DATE', [
55945594
'Invalid option cast_date:', 'cast_date must be true or a function,',
55955595
`got ${JSON.stringify(options.cast_date)}`

0 commit comments

Comments
 (0)