From 84466759a1ea01460bc072dd68664453ad53d383 Mon Sep 17 00:00:00 2001 From: David Worms Date: Wed, 4 Nov 2020 18:26:54 +0100 Subject: [PATCH] encoding: return buffer when null or false --- lib/ResizeableBuffer.js | 8 +++++--- lib/es5/ResizeableBuffer.js | 8 +++++--- test/ResizableBuffer.coffee | 2 +- test/option.encoding.coffee | 10 ++++++++++ 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/ResizeableBuffer.js b/lib/ResizeableBuffer.js index f69c30c..467422c 100644 --- a/lib/ResizeableBuffer.js +++ b/lib/ResizeableBuffer.js @@ -48,9 +48,11 @@ class ResizeableBuffer{ this.buf = buf } toString(encoding){ - // console.log('!!', encoding) - // return this.buf.slice(0, this.length) - return this.buf.slice(0, this.length).toString(encoding) + if(encoding){ + return this.buf.slice(0, this.length).toString(encoding) + }else{ + return Uint8Array.prototype.slice.call(this.buf.slice(0, this.length)) + } } toJSON(){ return this.toString('utf8') diff --git a/lib/es5/ResizeableBuffer.js b/lib/es5/ResizeableBuffer.js index 993d02b..8df1db3 100644 --- a/lib/es5/ResizeableBuffer.js +++ b/lib/es5/ResizeableBuffer.js @@ -78,9 +78,11 @@ var ResizeableBuffer = /*#__PURE__*/function () { }, { key: "toString", value: function toString(encoding) { - // console.log('!!', encoding) - // return this.buf.slice(0, this.length) - return this.buf.slice(0, this.length).toString(encoding); + if (encoding) { + return this.buf.slice(0, this.length).toString(encoding); + } else { + return Uint8Array.prototype.slice.call(this.buf.slice(0, this.length)); + } } }, { key: "toJSON", diff --git a/test/ResizableBuffer.coffee b/test/ResizableBuffer.coffee index 3b5ed31..11a2a3f 100644 --- a/test/ResizableBuffer.coffee +++ b/test/ResizableBuffer.coffee @@ -84,7 +84,7 @@ describe 'ResizeableBuffer', -> rb.append(chr.charCodeAt()) rb.prepend(Buffer.from('abc')) rb.length.should.eql 6 - rb.toString().should.eql 'abcdef' + rb.toString('utf8').should.eql 'abcdef' it 'throw invalid state if size equal buffer size', -> try diff --git a/test/option.encoding.coffee b/test/option.encoding.coffee index eee3452..4c89991 100644 --- a/test/option.encoding.coffee +++ b/test/option.encoding.coffee @@ -46,6 +46,16 @@ describe 'Option `encoding`', -> [ '1', '2 "3" 4', '5' ] ] unless err next err + + it 'null return buffer', (next) -> + parse Buffer.from( 'a,b\n1,2' ), + encoding: null + , (err, data) -> + data.should.eql [ + [ Buffer.from('a'), Buffer.from('b') ] + [ Buffer.from('1'), Buffer.from('2') ] + ] unless err + next err describe 'with bom', ->