code.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Code = void 0;
  4. /**
  5. * A class representation of the BSON Code type.
  6. * @public
  7. * @category BSONType
  8. */
  9. var Code = /** @class */ (function () {
  10. /**
  11. * @param code - a string or function.
  12. * @param scope - an optional scope for the function.
  13. */
  14. function Code(code, scope) {
  15. if (!(this instanceof Code))
  16. return new Code(code, scope);
  17. this.code = code;
  18. this.scope = scope;
  19. }
  20. Code.prototype.toJSON = function () {
  21. return { code: this.code, scope: this.scope };
  22. };
  23. /** @internal */
  24. Code.prototype.toExtendedJSON = function () {
  25. if (this.scope) {
  26. return { $code: this.code, $scope: this.scope };
  27. }
  28. return { $code: this.code };
  29. };
  30. /** @internal */
  31. Code.fromExtendedJSON = function (doc) {
  32. return new Code(doc.$code, doc.$scope);
  33. };
  34. /** @internal */
  35. Code.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  36. return this.inspect();
  37. };
  38. Code.prototype.inspect = function () {
  39. var codeJson = this.toJSON();
  40. return "new Code(\"" + codeJson.code + "\"" + (codeJson.scope ? ", " + JSON.stringify(codeJson.scope) : '') + ")";
  41. };
  42. return Code;
  43. }());
  44. exports.Code = Code;
  45. Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
  46. //# sourceMappingURL=code.js.map