From 30f5a8f8ca2ffdf7fc36b451b52ebfe5757a837e Mon Sep 17 00:00:00 2001 From: Matt Broadstone Date: Wed, 25 Mar 2020 10:24:37 -0400 Subject: [PATCH] fix: improve EJSON generation for previously skipped edge cases These cases were explicitly disabled in the BSON corpus test runner but are now supported. --- lib/double.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/double.js b/lib/double.js index 4f2db4f2..b5eb36b1 100644 --- a/lib/double.js +++ b/lib/double.js @@ -1,4 +1,5 @@ 'use strict'; + /** * A class representation of the BSON Double type. */ @@ -41,16 +42,22 @@ class Double { if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) { return this.value; } - return { $numberDouble: this.value.toString() }; + + if (Object.is(Math.sign(this.value), -0)) { + return { $numberDouble: `-${this.value.toFixed(1)}` }; + } + + return { + $numberDouble: Number.isInteger(this.value) ? this.value.toFixed(1) : this.value.toString() + }; } /** * @ignore */ static fromExtendedJSON(doc, options) { - return options && options.relaxed - ? parseFloat(doc.$numberDouble) - : new Double(parseFloat(doc.$numberDouble)); + const doubleValue = parseFloat(doc.$numberDouble); + return options && options.relaxed ? doubleValue : new Double(doubleValue); } }