timestamp.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.Timestamp = exports.LongWithoutOverridesClass = void 0;
  19. var long_1 = require("./long");
  20. var utils_1 = require("./parser/utils");
  21. /** @public */
  22. exports.LongWithoutOverridesClass = long_1.Long;
  23. /**
  24. * @public
  25. * @category BSONType
  26. * */
  27. var Timestamp = /** @class */ (function (_super) {
  28. __extends(Timestamp, _super);
  29. function Timestamp(low, high) {
  30. var _this = this;
  31. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  32. ///@ts-expect-error
  33. if (!(_this instanceof Timestamp))
  34. return new Timestamp(low, high);
  35. if (long_1.Long.isLong(low)) {
  36. _this = _super.call(this, low.low, low.high, true) || this;
  37. }
  38. else if (utils_1.isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
  39. _this = _super.call(this, low.i, low.t, true) || this;
  40. }
  41. else {
  42. _this = _super.call(this, low, high, true) || this;
  43. }
  44. Object.defineProperty(_this, '_bsontype', {
  45. value: 'Timestamp',
  46. writable: false,
  47. configurable: false,
  48. enumerable: false
  49. });
  50. return _this;
  51. }
  52. Timestamp.prototype.toJSON = function () {
  53. return {
  54. $timestamp: this.toString()
  55. };
  56. };
  57. /** Returns a Timestamp represented by the given (32-bit) integer value. */
  58. Timestamp.fromInt = function (value) {
  59. return new Timestamp(long_1.Long.fromInt(value, true));
  60. };
  61. /** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */
  62. Timestamp.fromNumber = function (value) {
  63. return new Timestamp(long_1.Long.fromNumber(value, true));
  64. };
  65. /**
  66. * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
  67. *
  68. * @param lowBits - the low 32-bits.
  69. * @param highBits - the high 32-bits.
  70. */
  71. Timestamp.fromBits = function (lowBits, highBits) {
  72. return new Timestamp(lowBits, highBits);
  73. };
  74. /**
  75. * Returns a Timestamp from the given string, optionally using the given radix.
  76. *
  77. * @param str - the textual representation of the Timestamp.
  78. * @param optRadix - the radix in which the text is written.
  79. */
  80. Timestamp.fromString = function (str, optRadix) {
  81. return new Timestamp(long_1.Long.fromString(str, true, optRadix));
  82. };
  83. /** @internal */
  84. Timestamp.prototype.toExtendedJSON = function () {
  85. return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
  86. };
  87. /** @internal */
  88. Timestamp.fromExtendedJSON = function (doc) {
  89. return new Timestamp(doc.$timestamp);
  90. };
  91. /** @internal */
  92. Timestamp.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  93. return this.inspect();
  94. };
  95. Timestamp.prototype.inspect = function () {
  96. return "new Timestamp({ t: " + this.getHighBits() + ", i: " + this.getLowBits() + " })";
  97. };
  98. Timestamp.MAX_VALUE = long_1.Long.MAX_UNSIGNED_VALUE;
  99. return Timestamp;
  100. }(exports.LongWithoutOverridesClass));
  101. exports.Timestamp = Timestamp;
  102. //# sourceMappingURL=timestamp.js.map