0.0.6 • Published 8 years ago

binstring58 v0.0.6

Weekly downloads
-
License
ISC
Repository
-
Last release
8 years ago

example

binString = require('binstring58').binString
hex = binString('this is utf', {out: 'hex'})
utf8 = binString('hex', {in:'hex', out: 'utf8'})

source

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
0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago