msgpackr 1.9.3 → 1.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/test.js CHANGED
@@ -1635,8 +1635,13 @@
1635
1635
  if (Array.isArray(value)) {
1636
1636
  packArray(value);
1637
1637
  } else {
1638
- if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1639
- return pack(value.toJSON());
1638
+ // use this as an alternate mechanism for expressing how to serialize
1639
+ if (value.toJSON) {
1640
+ const json = value.toJSON();
1641
+ // if for some reason value.toJSON returns itself it'll loop forever
1642
+ if (json !== value)
1643
+ return pack(json)
1644
+ }
1640
1645
 
1641
1646
  // if there is a writeFunction, use it, otherwise just encode as undefined
1642
1647
  if (type === 'function')
@@ -1681,7 +1686,7 @@
1681
1686
  }
1682
1687
  };
1683
1688
 
1684
- const writePlainObject = this.variableMapSize ? (object) => {
1689
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1685
1690
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1686
1691
  let keys = Object.keys(object);
1687
1692
  let length = keys.length;
@@ -1697,9 +1702,19 @@
1697
1702
  position += 4;
1698
1703
  }
1699
1704
  let key;
1700
- for (let i = 0; i < length; i++) {
1701
- pack(key = keys[i]);
1702
- pack(object[key]);
1705
+ if (this.coercibleKeyAsNumber) {
1706
+ for (let i = 0; i < length; i++) {
1707
+ key = keys[i];
1708
+ let num = Number(key);
1709
+ pack(isNaN(num) ? key : num);
1710
+ pack(object[key]);
1711
+ }
1712
+
1713
+ } else {
1714
+ for (let i = 0; i < length; i++) {
1715
+ pack(key = keys[i]);
1716
+ pack(object[key]);
1717
+ }
1703
1718
  }
1704
1719
  } :
1705
1720
  (object, safePrototype) => {
@@ -3213,6 +3228,13 @@
3213
3228
  assert.deepEqual(unpacked.map, {});
3214
3229
  assert.deepEqual(unpacked.set, {});
3215
3230
  });
3231
+ test('pack/unpack numeric coercible keys', function () {
3232
+ var data = { a: 1, 2: 'test', '-3.45': 'test2'};
3233
+ let packr = new Packr({variableMapSize: true, coercibleKeyAsNumber: true, useRecords: false});
3234
+ var serialized = packr.pack(data);
3235
+ var deserialized = packr.unpack(serialized);
3236
+ assert.deepEqual(deserialized, data);
3237
+ });
3216
3238
  test('pack/unpack empty data with bundled strings', function () {
3217
3239
  var data = {};
3218
3240
  let packr = new Packr({bundleStrings: true});
@@ -4081,6 +4103,17 @@
4081
4103
  assert.deepEqual(values, [[1,0,1], [2,1,2], [3,2,3], [4,3,4]]);
4082
4104
  });
4083
4105
 
4106
+ test('pack toJSON returning this', () => {
4107
+ class Serializable {
4108
+ someData = [1, 2, 3, 4]
4109
+ toJSON() {
4110
+ return this
4111
+ }
4112
+ }
4113
+ const serialized = pack(new Serializable);
4114
+ const deserialized = unpack(serialized);
4115
+ assert.deepStrictEqual(deserialized, { someData: [1, 2, 3, 4] });
4116
+ });
4084
4117
  });
4085
4118
  suite('msgpackr performance tests', function(){
4086
4119
  test('performance JSON.parse', function() {