binstring58
0.0.6 • Public • Published
binString = require('binstring58').binString
hex = binString('this is utf', {out: 'hex'})
utf8 = binString('hex', {in:'hex', out: 'utf8'})
bs58 = require('bs58')
nestedOptions = (options) ->
options ?= {}
nested =
in:
sha3: options.sha3 || false
out:
sha3: options.sha3 || false
if options.in then nested.in.in = options.in
if options.out then nested.out.out = options.out
nested
isBuffer = (obj) ->
! !(obj != null and (obj._isBuffer or obj.constructor and typeof obj.constructor.isBuffer == 'function' and obj.constructor.isBuffer(obj)))
binString = (data, options) ->
options ?= {}
if !isBuffer(data)
if Array.isArray(data)
data = new Buffer(data)
else if typeof data == 'number'
data = new Buffer(data.toString(16), 'hex')
else if typeof data == 'string'
if options.in
if options.in == 'base58' then data = new Buffer(bs58.decode data)
# else if options.in == 'base91' then data = base91.decode data
else data = new Buffer(data, options.in)
else
if data.slice(0, 2) == '0x'
data = new Buffer(data.slice(2), 'hex')
else
data = new Buffer(data, 'binary')
switch options.out
when 'bytes' then bufferToBytes data
when 'base58' then bs58.encode data
# when 'base91' then base91.encode data
when 'hex', 'binary', 'utf8', 'base64' then data.toString(options.out)
else data
bufferToBytes = (data) ->
rs = []
i = 0
while i < data.length
rs.push data[i]
i++
rs
module.exports =
nestedOptions: nestedOptions
binString: binString
Versions
Current Tags
- VersionDownloads (Last 7 Days)
- 0.0.6
0
Version History
Package Sidebar
Install
Weekly Downloads