code.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. */
  8. var Code = /** @class */ (function () {
  9. /**
  10. * @param code - a string or function.
  11. * @param scope - an optional scope for the function.
  12. */
  13. function Code(code, scope) {
  14. if (!(this instanceof Code))
  15. return new Code(code, scope);
  16. this.code = code;
  17. this.scope = scope;
  18. }
  19. Code.prototype.toJSON = function () {
  20. return { code: this.code, scope: this.scope };
  21. };
  22. /** @internal */
  23. Code.prototype.toExtendedJSON = function () {
  24. if (this.scope) {
  25. return { $code: this.code, $scope: this.scope };
  26. }
  27. return { $code: this.code };
  28. };
  29. /** @internal */
  30. Code.fromExtendedJSON = function (doc) {
  31. return new Code(doc.$code, doc.$scope);
  32. };
  33. /** @internal */
  34. Code.prototype[Symbol.for('nodejs.util.inspect.custom')] = function () {
  35. return this.inspect();
  36. };
  37. Code.prototype.inspect = function () {
  38. var codeJson = this.toJSON();
  39. return "new Code(\"" + codeJson.code + "\"" + (codeJson.scope ? ", " + JSON.stringify(codeJson.scope) : '') + ")";
  40. };
  41. return Code;
  42. }());
  43. exports.Code = Code;
  44. Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
  45. //# sourceMappingURL=code.js.map