double.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /** @internal */
  31. Double.prototype.toJSON = function () {
  32. return this.value;
  33. };
  34. /** @internal */
  35. Double.prototype.toExtendedJSON = function (options) {
  36. if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
  37. return this.value;
  38. }
  39. // NOTE: JavaScript has +0 and -0, apparently to model limit calculations. If a user
  40. // explicitly provided `-0` then we need to ensure the sign makes it into the output
  41. if (Object.is(Math.sign(this.value), -0)) {
  42. return { $numberDouble: "-" + this.value.toFixed(1) };
  43. }
  44. var $numberDouble;
  45. if (Number.isInteger(this.value)) {
  46. $numberDouble = this.value.toFixed(1);
  47. if ($numberDouble.length >= 13) {
  48. $numberDouble = this.value.toExponential(13).toUpperCase();
  49. }
  50. }
  51. else {
  52. $numberDouble = this.value.toString();
  53. }
  54. return { $numberDouble: $numberDouble };
  55. };
  56. /** @internal */
  57. Double.fromExtendedJSON = function (doc, options) {
  58. var doubleValue = parseFloat(doc.$numberDouble);
  59. return options && options.relaxed ? doubleValue : new Double(doubleValue);
  60. };
  61. /** @internal */
  62. Double.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  63. return this.inspect();
  64. };
  65. Double.prototype.inspect = function () {
  66. var eJSON = this.toExtendedJSON();
  67. return "new Double(" + eJSON.$numberDouble + ")";
  68. };
  69. return Double;
  70. }());
  71. exports.Double = Double;
  72. Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
  73. //# sourceMappingURL=double.js.map