Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
encoding: return buffer when null or false
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 4, 2020
1 parent f2c9f85 commit 8446675
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
8 changes: 5 additions & 3 deletions lib/ResizeableBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
8 changes: 5 additions & 3 deletions lib/es5/ResizeableBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/ResizableBuffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/option.encoding.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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', ->

Expand Down

0 comments on commit 8446675

Please # to comment.