cast-buffer
v1.2.0
Published
cast objects, arrays, numbers strings and booleans to buffers
Downloads
1,403
Maintainers
Readme
cast-buffer
cast objects, arrays, numbers, strings and booleans to buffers
Installation
npm i --save cast-buffer
Usage
var buff
buff = castBuffer({ 'foo': 1, 'bar': 2 }) // new Buffer('{ 'foo': 1, 'bar': 2 }')
buff = castBuffer([ 'foo', 'bar', 'qux' ]) // new Buffer('[ 'foo', 'bar', 'qux' ]')
buff = castBuffer('hello') // new Buffer('hello')
buff = castBuffer(10) // new Buffer('10') // stringified number
buff = castBuffer(1.1) // new Buffer('1.1') // stringified number
buff = castBuffer(true) // new Buffer('true') // stringified boolean
buff = castBuffer(new Buffer('hello')) // returns same buffer
// Calls toJSON if it exists
var foobar = {
_json: {}
get: function () {/*...*/}
set: function () {/*...*/}
toJSON: function () {
return this._json
}
}
buff = castBuffer(foobar) // new Buffer('{}')
buff = castBuffer([ foobar ]) // new Buffer('[{}]')
License
MIT