diff --git a/lib/serialize/primitives.js b/lib/serialize/primitives.js index 2fbec334..9eb5b4eb 100644 --- a/lib/serialize/primitives.js +++ b/lib/serialize/primitives.js @@ -24,7 +24,7 @@ const { const bigIntToString = BigInt.prototype.toString; -module.exports = {tracePrimitive}; +module.exports = {tracePrimitive, traceMinusZero}; /** * Trace primitive value. @@ -81,8 +81,7 @@ function traceBoolean(val, record) { * @returns {number} - Type ID */ function traceNumber(val, record) { - // `Object.is()` required to differentiate between 0 and -0 - if (val < 0 || Object.is(val, -0)) { + if (val < 0) { record.name = `minus${-val}`; record.extra = {positive: this.traceDependency(-val, null, null, record)}; return NEGATIVE_TYPE; @@ -93,6 +92,18 @@ function traceNumber(val, record) { return NUMBER_TYPE; } +/** + * Trace minus zero. + * @this {Object} Serializer + * @param {Object} record - Record + * @returns {number} - Type ID + */ +function traceMinusZero(record) { + record.name = 'minus0'; + record.extra = {positive: this.traceDependency(0, null, null, record)}; + return NEGATIVE_TYPE; +} + /** * Trace `BigInt`. * @this {Object} Serializer diff --git a/lib/serialize/trace.js b/lib/serialize/trace.js index f51aba4f..613a5d5f 100644 --- a/lib/serialize/trace.js +++ b/lib/serialize/trace.js @@ -33,6 +33,7 @@ module.exports = { */ initTrace() { this.traceStack = []; + this.minusZeroRecord = null; }, /** @@ -52,6 +53,15 @@ module.exports = { }, traceValueInner(val, name) { + // Special case -0 because 0 and -0 are treated as equal as Map keys + if (Object.is(val, -0)) { + if (this.minusZeroRecord) return this.minusZeroRecord; + const record = createRecord(name); + record.type = this.traceMinusZero(record); + this.minusZeroRecord = record; + return record; + } + // Use existing record const {records} = this; let record = records.get(val);