int_32.js 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. /**
  3. * A class representation of a BSON Int32 type.
  4. */
  5. class Int32 {
  6. /**
  7. * Create an Int32 type
  8. *
  9. * @param {number} value the number we want to represent as an int32.
  10. * @return {Int32}
  11. */
  12. constructor(value) {
  13. this.value = value;
  14. }
  15. /**
  16. * Access the number value.
  17. *
  18. * @method
  19. * @return {number} returns the wrapped int32 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) return this.value;
  35. return { $numberInt: this.value.toString() };
  36. }
  37. /**
  38. * @ignore
  39. */
  40. static fromExtendedJSON(doc, options) {
  41. return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
  42. }
  43. }
  44. Object.defineProperty(Int32.prototype, '_bsontype', { value: 'Int32' });
  45. module.exports = Int32;