code.js 815 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. /**
  3. * A class representation of the BSON Code type.
  4. */
  5. class Code {
  6. /**
  7. * Create a Code type
  8. *
  9. * @param {(string|function)} code a string or function.
  10. * @param {Object} [scope] an optional scope for the function.
  11. * @return {Code}
  12. */
  13. constructor(code, scope) {
  14. this.code = code;
  15. this.scope = scope;
  16. }
  17. /**
  18. * @ignore
  19. */
  20. toJSON() {
  21. return { scope: this.scope, code: this.code };
  22. }
  23. /**
  24. * @ignore
  25. */
  26. toExtendedJSON() {
  27. if (this.scope) {
  28. return { $code: this.code, $scope: this.scope };
  29. }
  30. return { $code: this.code };
  31. }
  32. /**
  33. * @ignore
  34. */
  35. static fromExtendedJSON(doc) {
  36. return new Code(doc.$code, doc.$scope);
  37. }
  38. }
  39. Object.defineProperty(Code.prototype, '_bsontype', { value: 'Code' });
  40. module.exports = Code;