Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boolean type #43

Open
feross opened this issue Dec 28, 2020 · 0 comments
Open

Boolean type #43

feross opened this issue Dec 28, 2020 · 0 comments

Comments

@feross
Copy link

feross commented Dec 28, 2020

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant