double.js 2.5 KB

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