mcollina / msgpack5

A msgpack v5 implementation for node.js, with extension points / msgpack.org[Node]
MIT License
493 stars 76 forks source link

Too complicated encodeFloat function. #77

Closed thephoenixofthevoid closed 5 years ago

thephoenixofthevoid commented 5 years ago

https://github.com/mcollina/msgpack5/blob/827ed095d6f18ed52e7f4ddafe498e1e6d0a6a77/lib/encoder.js#L314

const fround = Math.fround;

function encodeFloat (obj, forceFloat64) { 
  var buf

  if (forceFloat64 || !fround || fround(obj) !== obj) {
    buf = Buffer.allocUnsafe(9)
    buf[0] = 0xcb
    buf.writeDoubleBE(obj, 1)
  } else {
    buf = Buffer.allocUnsafe(5)
    buf[0] = 0xca
    buf.writeFloatBE(obj, 1)
  }

  return buf
}
mcollina commented 5 years ago

Would you like to send a PR?

thephoenixofthevoid commented 5 years ago

I would better include this one into that big PR, if that's ok. When done refactoring decoder, I will do it.