double.js
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Double = void 0;
/**
* A class representation of the BSON Double type.
* @public
* @category BSONType
*/
var Double = /** @class */ (function () {
/**
* Create a Double type
*
* @param value - the number we want to represent as a double.
*/
function Double(value) {
if (!(this instanceof Double))
return new Double(value);
if (value instanceof Number) {
value = value.valueOf();
}
this.value = +value;
}
/**
* Access the number value.
*
* @returns returns the wrapped double number.
*/
Double.prototype.valueOf = function () {
return this.value;
};
Double.prototype.toJSON = function () {
return this.value;
};
Double.prototype.toString = function (radix) {
return this.value.toString(radix);
};
/** @internal */
Double.prototype.toExtendedJSON = function (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) };
}
var $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: $numberDouble };
};
/** @internal */
Double.fromExtendedJSON = function (doc, options) {
var doubleValue = parseFloat(doc.$numberDouble);
return options && options.relaxed ? doubleValue : new Double(doubleValue);
};
/** @internal */
Double.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
return this.inspect();
};
Double.prototype.inspect = function () {
var eJSON = this.toExtendedJSON();
return "new Double(" + eJSON.$numberDouble + ")";
};
return Double;
}());
exports.Double = Double;
Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
//# sourceMappingURL=double.js.map