double.js 993 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. /**
  3. * A class representation of the BSON Double type.
  4. */
  5. class Double {
  6. /**
  7. * Create a Double type
  8. *
  9. * @param {number} value the number we want to represent as a double.
  10. * @return {Double}
  11. */
  12. constructor(value) {
  13. this.value = value;
  14. }
  15. /**
  16. * Access the number value.
  17. *
  18. * @method
  19. * @return {number} returns the wrapped double number.
  20. */
  21. valueOf() {
  22. return this.value;
  23. }
  24. /**
  25. * @ignore
  26. */
  27. toJSON() {
  28. return this.value;
  29. }
  30. /**
  31. * @ignore
  32. */
  33. toExtendedJSON(options) {
  34. if (options && options.relaxed && isFinite(this.value)) return this.value;
  35. return { $numberDouble: this.value.toString() };
  36. }
  37. /**
  38. * @ignore
  39. */
  40. static fromExtendedJSON(doc, options) {
  41. return options && options.relaxed
  42. ? parseFloat(doc.$numberDouble)
  43. : new Double(parseFloat(doc.$numberDouble));
  44. }
  45. }
  46. Object.defineProperty(Double.prototype, '_bsontype', { value: 'Double' });
  47. module.exports = Double;