From 3332a5f8a5b3e2d136dca515e0aae598e109453d Mon Sep 17 00:00:00 2001 From: Tom Yam Date: Thu, 9 Jul 2020 14:17:37 +0300 Subject: [PATCH] fix(bom): BOM should be included when requested in sync Fixes #114 --- lib/sync.js | 5 ++++- test/option.bom.coffee | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/sync.js b/lib/sync.js index 761856a..af1b2f8 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -18,5 +18,8 @@ module.exports = function(records, options={}){ stringifier.write(record) } stringifier.end() - return data.join('') + const res = data.join(''); + return options.bom + ? '\ufeff' + res + : res; } diff --git a/test/option.bom.coffee b/test/option.bom.coffee index a91e655..6977ed4 100644 --- a/test/option.bom.coffee +++ b/test/option.bom.coffee @@ -1,5 +1,6 @@ stringify = require '../lib' +stringifySync = require '../lib/sync' describe 'Option `bom`', -> @@ -23,3 +24,23 @@ describe 'Option `bom`', -> ], bom: false, (err, data) -> data.should.eql 'ok\n' 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 'value is `true`', -> + res = stringifySync [ + value: 'ok' + ], bom: true + res.should.eql '\ufeffok\n' + + it 'value is `false`', -> + res = stringifySync [ + value: 'ok' + ], bom: false + res.should.eql 'ok\n'