Skip to content

Commit

Permalink
use Buffer.from when avail
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Apr 26, 2018
1 parent e70eedf commit eebe20e
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module.exports = toBuffer

var makeBuffer = Buffer.from && Buffer.from !== Uint8Array.from ? Buffer.from : bufferFrom

function bufferFrom (buf, enc) {
return new Buffer(buf, enc)
}

function toBuffer (buf, enc) {
if (Buffer.isBuffer(buf)) return buf
if (typeof buf === 'string') return new Buffer(buf, enc)
if (Array.isArray(buf)) return new Buffer(buf)
if (typeof buf === 'string') return makeBuffer(buf, enc)
if (Array.isArray(buf)) return makeBuffer(buf)
throw new Error('Input should be a buffer or a string')
}

0 comments on commit eebe20e

Please sign in to comment.