typeson-registry 14.2.0 → 14.2.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
@@ -1312,8 +1312,51 @@ const primitiveObjects = {
1312
1312
  test (x) {
1313
1313
  return toStringTag(x) === 'Number' && typeof x === 'object';
1314
1314
  },
1315
- replace: Number, // convert to primitive number
1316
- revive (n) { return new Number(n); } // Revive to an objectified number
1315
+ // `_encapsulate`'s nested-replace guard (`_stateObj.replaced`)
1316
+ // means a bare `NaN`/`Infinity`/`-Infinity`/`-0` returned here
1317
+ // would skip the sentinel treatment the bare `nan`/`infinity`/
1318
+ // `negativeZero` type specs normally give those values (since
1319
+ // we're already inside this spec's own `replace()`) -- JSON
1320
+ // can't represent them, so they'd otherwise silently become
1321
+ // `null` (or lose their sign, for `-0`). Encode them the same
1322
+ // way those specs do, directly, rather than relying on that
1323
+ // nested pass.
1324
+ replace (o) {
1325
+ const n = o.valueOf();
1326
+ if (Number.isNaN(n)) {
1327
+ return 'NaN';
1328
+ }
1329
+ if (n === Infinity) {
1330
+ return 'Infinity';
1331
+ }
1332
+ if (n === -Infinity) {
1333
+ return '-Infinity';
1334
+ }
1335
+ if (Object.is(n, -0)) {
1336
+ // A plain `0` here would be indistinguishable from a
1337
+ // genuine positive `0` once round-tripped through JSON
1338
+ // (which can't represent the sign), so this needs its
1339
+ // own sentinel too, same as the others above.
1340
+ return '-0';
1341
+ }
1342
+ return n;
1343
+ },
1344
+ // Revive to an objectified number
1345
+ revive (n) {
1346
+ if (n === 'NaN') {
1347
+ return new Number(NaN);
1348
+ }
1349
+ if (n === 'Infinity') {
1350
+ return new Number(Infinity);
1351
+ }
1352
+ if (n === '-Infinity') {
1353
+ return new Number(-Infinity);
1354
+ }
1355
+ if (n === '-0') {
1356
+ return new Number(-0);
1357
+ }
1358
+ return new Number(n);
1359
+ }
1317
1360
  }
1318
1361
  };
1319
1362