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