diff --git a/index.js b/index.js index df1ff4d..540790f 100644 --- a/index.js +++ b/index.js @@ -2,12 +2,12 @@ const fs = require('fs'); -const defOpts = { - encoding: 'utf8', - lineEnding: '\n' -}; - -module.exports = (path, opts = defOpts) => { +module.exports = (path, usrOpts) => { + const opts = { + encoding: 'utf8', + lineEnding: '\n' + }; + Object.assign(opts, usrOpts); return new Promise((resolve, reject) => { const rs = fs.createReadStream(path, {encoding: opts.encoding}); let acc = ''; diff --git a/test/test.js b/test/test.js index abb57ea..b5ecd5b 100644 --- a/test/test.js +++ b/test/test.js @@ -50,6 +50,12 @@ describe('firstline', () => { .then(() => firstline(filePath).should.eventually.equal('')) ); + it( + 'should work with a different encoding when specified correctly', + () => promisify(fs.writeFile, [filePath, 'abc\ndef\nghi', { encoding: 'ascii' }]) + .then(() => firstline(filePath, { encoding: 'ascii' }).should.eventually.equal('abc')) + ); + it( 'should work with a different line ending when specified correctly', () => promisify(fs.writeFile, [filePath, 'abc\rdef\rghi'])