double.js 2.5 KB

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