Sitelet https://github.com/varstruct/varstruct/issues/43
Skip to content

Boolean type #43

Description

@feross

Is there any interest in adding support for a Boolean type? If you think this fits, I can PR it.

I implemented it like this:

function encode (value, buffer, offset) {
  if (typeof value !== 'boolean') throw new TypeError('value must be a boolean')
  if (!buffer) buffer = Buffer.allocUnsafe(1)
  if (!offset) offset = 0
  buffer.writeUInt8(Number(value), offset)
  return buffer
}

function decode (buffer, offset, end) {
  if (!offset) offset = 0
  if (!end) return Boolean(buffer.readUInt8(offset))
  return Boolean(buffer.slice(offset, end).readUInt8(0))
}

function encodingLength () {
  return 1
}

encode.bytes = decode.bytes = 1

export const VBoolean = {
  encode,
  decode,
  encodingLength
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions