double.js 2.2 KB

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