-
Notifications
You must be signed in to change notification settings - Fork 126
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
Add optional support for using JS Map object for MessagePack maps #42
Conversation
Accepts Map objects in input and encodes them as MessagePack maps, and if the decoder option "usemap" is true, it decodes maps to Map objects instead of plain JS Objects. The key advantage of this is that Maps, like MessagePack maps, and unlike JS Objects, can have keys of arbitrary type.
Thank you for the contribution. I'd like to ask you a small modification as below: function map(decoder, len) {
var usemap = decoder.options && decoder.options.usemap && HAS_MAP;
return usemap ? map_to_map(decoder, len) : map_to_obj(decoder, len);
} The function getReadFormat(options) {
var binarraybuffer = HAS_UINT8ARRAY && options && options.binarraybuffer;
var readFormat = {
map: map,
array: array,
str: str,
bin: (binarraybuffer ? bin_arraybuffer : bin_buffer), It means it checks
I prefer the small trick which costs a bit less, as it's one of the keys to keep the encoding/decoding speed when adding some new features. |
By the way, I have a question (not a request). |
Improves performance since we don't have to check for it on every decode.
Tests that the right number of bytes are consumed when reading ext types, by unpacking an array of them.
Thanks for the feedback! I've restructured the options check, let me know if that's what you had in mind. I've also tried to fix the tests for running on platforms with a Map object, since I noticed they were failing some of the Travis checks. Finally, I added a test for the unrelated bugfix in c78ebd9; this test fails on current master but passes here. |
Oh, and I don't need |
It seems that Sauce's browser test platform itself is unstable and breaking sometimes, by the way.
I see. |
Sauce's test failure around |
It seems all tests pass except for some failures which are caused by Sauce platform. I'll be merging it. +var isBrowser = ("undefined" !== typeof window);
+var Map = isBrowser && window.Map || global.Map;
+var HAS_MAP = ("undefined" !== typeof Map); Do you have any reason to add those three lines? +var HAS_MAP = ("undefined" !== typeof Map);
"jshintConfig": {
"es3": true,
"globals": {
"JSON": true,
+ "Map": true,
"Symbol": true,
"window": true
},
"mocha": true,
"node": true,
"undef": true
}, |
Ah, you're right! I'd misinterpreted the jshint output as an error running the tests. I'm still pretty new to JS and I'll admit I'm still a bit confused about how different runtimes handle globals. Dropping the lines and adding the jshint config works for me locally; I've pushed the change to my branch. Since you've already merged this, I can do another PR if you like. |
Unlike JavaScript objects, Maps, like MP maps, can have arbitrary values as keys (not just strings). Map is an ES6 addition, I believe, so not all platforms will have it. But the latest browsers and Node all support it.
The first commit is an unrelated bug in unpacking fixed-length extension types; I found it while working on this. I can break that out as its own PR if desired.