msgpackr 1.9.3 → 1.9.5-debug.1

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.
@@ -1574,8 +1574,13 @@
1574
1574
  if (Array.isArray(value)) {
1575
1575
  packArray(value);
1576
1576
  } else {
1577
- if (value.toJSON) // use this as an alternate mechanism for expressing how to serialize
1578
- return pack(value.toJSON());
1577
+ // use this as an alternate mechanism for expressing how to serialize
1578
+ if (value.toJSON) {
1579
+ const json = value.toJSON();
1580
+ // if for some reason value.toJSON returns itself it'll loop forever
1581
+ if (json !== value)
1582
+ return pack(json)
1583
+ }
1579
1584
 
1580
1585
  // if there is a writeFunction, use it, otherwise just encode as undefined
1581
1586
  if (type === 'function')
@@ -1620,7 +1625,7 @@
1620
1625
  }
1621
1626
  };
1622
1627
 
1623
- const writePlainObject = this.variableMapSize ? (object) => {
1628
+ const writePlainObject = (this.variableMapSize || this.coercibleKeyAsNumber) ? (object) => {
1624
1629
  // this method is slightly slower, but generates "preferred serialization" (optimally small for smaller objects)
1625
1630
  let keys = Object.keys(object);
1626
1631
  let length = keys.length;
@@ -1636,9 +1641,19 @@
1636
1641
  position += 4;
1637
1642
  }
1638
1643
  let key;
1639
- for (let i = 0; i < length; i++) {
1640
- pack(key = keys[i]);
1641
- pack(object[key]);
1644
+ if (this.coercibleKeyAsNumber) {
1645
+ for (let i = 0; i < length; i++) {
1646
+ key = keys[i];
1647
+ let num = Number(key);
1648
+ pack(isNaN(num) ? key : num);
1649
+ pack(object[key]);
1650
+ }
1651
+
1652
+ } else {
1653
+ for (let i = 0; i < length; i++) {
1654
+ pack(key = keys[i]);
1655
+ pack(object[key]);
1656
+ }
1642
1657
  }
1643
1658
  } :
1644
1659
  (object, safePrototype) => {