timestamp.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /** @public */
  24. var Timestamp = /** @class */ (function (_super) {
  25. __extends(Timestamp, _super);
  26. function Timestamp(low, high) {
  27. var _this = this;
  28. // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  29. ///@ts-expect-error
  30. if (!(_this instanceof Timestamp))
  31. return new Timestamp(low, high);
  32. if (long_1.Long.isLong(low)) {
  33. _this = _super.call(this, low.low, low.high, true) || this;
  34. }
  35. else if (utils_1.isObjectLike(low) && typeof low.t !== 'undefined' && typeof low.i !== 'undefined') {
  36. _this = _super.call(this, low.i, low.t, true) || this;
  37. }
  38. else {
  39. _this = _super.call(this, low, high, true) || this;
  40. }
  41. Object.defineProperty(_this, '_bsontype', {
  42. value: 'Timestamp',
  43. writable: false,
  44. configurable: false,
  45. enumerable: false
  46. });
  47. return _this;
  48. }
  49. Timestamp.prototype.toJSON = function () {
  50. return {
  51. $timestamp: this.toString()
  52. };
  53. };
  54. /** Returns a Timestamp represented by the given (32-bit) integer value. */
  55. Timestamp.fromInt = function (value) {
  56. return new Timestamp(long_1.Long.fromInt(value, true));
  57. };
  58. /** Returns a Timestamp representing the given number value, provided that it is a finite number. Otherwise, zero is returned. */
  59. Timestamp.fromNumber = function (value) {
  60. return new Timestamp(long_1.Long.fromNumber(value, true));
  61. };
  62. /**
  63. * Returns a Timestamp for the given high and low bits. Each is assumed to use 32 bits.
  64. *
  65. * @param lowBits - the low 32-bits.
  66. * @param highBits - the high 32-bits.
  67. */
  68. Timestamp.fromBits = function (lowBits, highBits) {
  69. return new Timestamp(lowBits, highBits);
  70. };
  71. /**
  72. * Returns a Timestamp from the given string, optionally using the given radix.
  73. *
  74. * @param str - the textual representation of the Timestamp.
  75. * @param optRadix - the radix in which the text is written.
  76. */
  77. Timestamp.fromString = function (str, optRadix) {
  78. return new Timestamp(long_1.Long.fromString(str, true, optRadix));
  79. };
  80. /** @internal */
  81. Timestamp.prototype.toExtendedJSON = function () {
  82. return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
  83. };
  84. /** @internal */
  85. Timestamp.fromExtendedJSON = function (doc) {
  86. return new Timestamp(doc.$timestamp);
  87. };
  88. /** @internal */
  89. Timestamp.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  90. return this.inspect();
  91. };
  92. Timestamp.prototype.inspect = function () {
  93. return "new Timestamp({ t: " + this.getHighBits() + ", i: " + this.getLowBits() + " })";
  94. };
  95. Timestamp.MAX_VALUE = long_1.Long.MAX_UNSIGNED_VALUE;
  96. return Timestamp;
  97. }(exports.LongWithoutOverridesClass));
  98. exports.Timestamp = Timestamp;
  99. //# sourceMappingURL=timestamp.js.map