-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to prefer decoding as Map (#95)
* Test preferMap option * Add preferMap option to always decode as Map * Don't warn on string-keyed Maps when preferMap set * Document preferMap option * Polyfill Object.fromEntries() in tests for node 10
- Loading branch information
Showing
5 changed files
with
94 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
const test = require('tape').test | ||
const msgpack = require('../') | ||
|
||
const map = new Map() | ||
.set('a', 1) | ||
.set('1', 'hello') | ||
.set('world', 2) | ||
.set('0', 'again') | ||
.set('01', null) | ||
|
||
test('round-trip string-keyed Maps', function (t) { | ||
const encoder = msgpack({preferMap: true}) | ||
|
||
for (const input of [new Map(), map]) { | ||
const result = encoder.decode(encoder.encode(input)) | ||
t.assert(result instanceof Map) | ||
t.deepEqual(result, input) | ||
} | ||
|
||
t.end() | ||
}) | ||
|
||
test('preserve iteration order of string-keyed Maps', function (t) { | ||
const encoder = msgpack({preferMap: true}) | ||
const decoded = encoder.decode(encoder.encode(map)) | ||
|
||
t.deepEqual([...decoded.keys()], [...map.keys()]) | ||
|
||
t.end() | ||
}) | ||
|
||
test('user can still encode objects as ext maps', function (t) { | ||
const encoder = msgpack({preferMap: true}) | ||
const tag = 0x42 | ||
|
||
// Polyfill Object.fromEntries for node 10 | ||
const fromEntries = Object.fromEntries || (iterable => { | ||
const object = {} | ||
for (const [property, value] of iterable) { | ||
object[property] = value | ||
} | ||
return object | ||
}) | ||
|
||
encoder.register( | ||
tag, | ||
Object, | ||
obj => encoder.encode(new Map(Object.entries(obj))), | ||
data => fromEntries(encoder.decode(data)) | ||
) | ||
|
||
const inputs = [ | ||
{}, | ||
new Map(), | ||
{foo: 'bar'}, | ||
new Map().set('foo', 'bar'), | ||
new Map().set(null, null), | ||
{0: 'baz'}, | ||
['baz'] | ||
] | ||
|
||
for (const input of inputs) { | ||
const buf = encoder.encode(input) | ||
const result = encoder.decode(buf) | ||
|
||
t.deepEqual(result, input) | ||
t.equal(Object.getPrototypeOf(result), Object.getPrototypeOf(input)) | ||
} | ||
|
||
t.end() | ||
}) |