Support for Long and Float specific number types#1
Support for Long and Float specific number types#1mattbishop wants to merge 4 commits intoJoshuaWise:masterfrom
Conversation
Add Typescript definition file.
Updated docs to show how to use Long and Float.
|
@mattbishop rather than forcing people to use a third-party library for their big numbers, I'd rather support the official BigInt number type, which is available in node v10.4.0+. As for 32-bit floats, perhaps const { encode, as } = require('tiny-msgpack');
const uint8array = encode({ foo: as.u8(123) }); |
|
I love the idea of BigInt better than Long. 👍 As for float, it may not be obvious what type of float should be used. My approach was to just signify the number should be treated as in the float family. The lib would figure out the right size. How about |
|
BigInts are now supported for 64-bit integers (in version 2.0, just released). The problem of deterministic/specific typing still remains. |
Msgpack implementations on other platforms usually expect number formats to be in specific families like int and float. They do not do well when a value can be either int or float. Javascript's number type cannot be reliably pinned to be a float or an int, so tiny-msgpack (and msgpack-lite) make a best-guess effort to pick the right msgpack family.
This is not always acceptable for cross-platform message formats that use msgpack. This PR brings in support for specifying the number more specifically. One can encode a
Floatinstance and it will be encoded as float-32 or float-64, even if the value is an int, like 1.0.Similarly, if one has an int that is greater than 32 bits in length then you can now pass in a
Longinstance and see it encoded as either uint64 or int-64. This change adds a dependency on thelonglibrary, which is a pretty safe dependency.If you don't want to accept this PR, please let me know and I can release a new version of tiny-msgpack with these changes for my own purposes.