Skip to content

Commit

Permalink
fix: improve EJSON generation for previously skipped edge cases
Browse files Browse the repository at this point in the history
These cases were explicitly disabled in the BSON corpus test runner
but are now supported.
  • Loading branch information
mbroadst committed Mar 26, 2020
1 parent 35b151c commit 30f5a8f
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/double.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

/**
* A class representation of the BSON Double type.
*/
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 30f5a8f

Please sign in to comment.