70 lines
2.1 KiB
JavaScript
70 lines
2.1 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Double = void 0;
|
|
/**
|
|
* A class representation of the BSON Double type.
|
|
* @public
|
|
*/
|
|
class Double {
|
|
/**
|
|
* Create a Double type
|
|
*
|
|
* @param value - the number we want to represent as a double.
|
|
*/
|
|
constructor(value) {
|
|
if (value instanceof Number) {
|
|
value = value.valueOf();
|
|
}
|
|
this.value = +value;
|
|
}
|
|
/**
|
|
* Access the number value.
|
|
*
|
|
* @returns returns the wrapped double number.
|
|
*/
|
|
valueOf() {
|
|
return this.value;
|
|
}
|
|
/** @internal */
|
|
toJSON() {
|
|
return this.value;
|
|
}
|
|
/** @internal */
|
|
toExtendedJSON(options) {
|
|
if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
|
|
return this.value;
|
|
}
|
|
// NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
|
|
// explicitly provided `-0` then we need to ensure the sign makes it into the output
|
|
if (Object.is(Math.sign(this.value), -0)) {
|
|
return { $numberDouble: `-${this.value.toFixed(1)}` };
|
|
}
|
|
let $numberDouble;
|
|
if (Number.isInteger(this.value)) {
|
|
$numberDouble = this.value.toFixed(1);
|
|
if ($numberDouble.length >= 13) {
|
|
$numberDouble = this.value.toExponential(13).toUpperCase();
|
|
}
|
|
}
|
|
else {
|
|
$numberDouble = this.value.toString();
|
|
}
|
|
return { $numberDouble };
|
|
}
|
|
/** @internal */
|
|
static fromExtendedJSON(doc, options) {
|
|
const doubleValue = parseFloat(doc.$numberDouble);
|
|
return options && options.relaxed ? doubleValue : new Double(doubleValue);
|
|
}
|
|
/** @internal */
|
|
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
return this.inspect();
|
|
}
|
|
inspect() {
|
|
const eJSON = this.toExtendedJSON();
|
|
return `Double(${eJSON.$numberDouble})`;
|
|
}
|
|
}
|
|
exports.Double = Double;
|
|
Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
|
|
//# sourceMappingURL=double.js.map
|