regexp.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BSONRegExp = void 0;
  4. var error_1 = require("./error");
  5. function alphabetize(str) {
  6. return str.split('').sort().join('');
  7. }
  8. /**
  9. * A class representation of the BSON RegExp type.
  10. * @public
  11. * @category BSONType
  12. */
  13. var BSONRegExp = /** @class */ (function () {
  14. /**
  15. * @param pattern - The regular expression pattern to match
  16. * @param options - The regular expression options
  17. */
  18. function BSONRegExp(pattern, options) {
  19. if (!(this instanceof BSONRegExp))
  20. return new BSONRegExp(pattern, options);
  21. this.pattern = pattern;
  22. this.options = alphabetize(options !== null && options !== void 0 ? options : '');
  23. if (this.pattern.indexOf('\x00') !== -1) {
  24. throw new error_1.BSONError("BSON Regex patterns cannot contain null bytes, found: " + JSON.stringify(this.pattern));
  25. }
  26. if (this.options.indexOf('\x00') !== -1) {
  27. throw new error_1.BSONError("BSON Regex options cannot contain null bytes, found: " + JSON.stringify(this.options));
  28. }
  29. // Validate options
  30. for (var i = 0; i < this.options.length; i++) {
  31. if (!(this.options[i] === 'i' ||
  32. this.options[i] === 'm' ||
  33. this.options[i] === 'x' ||
  34. this.options[i] === 'l' ||
  35. this.options[i] === 's' ||
  36. this.options[i] === 'u')) {
  37. throw new error_1.BSONError("The regular expression option [" + this.options[i] + "] is not supported");
  38. }
  39. }
  40. }
  41. BSONRegExp.parseOptions = function (options) {
  42. return options ? options.split('').sort().join('') : '';
  43. };
  44. /** @internal */
  45. BSONRegExp.prototype.toExtendedJSON = function (options) {
  46. options = options || {};
  47. if (options.legacy) {
  48. return { $regex: this.pattern, $options: this.options };
  49. }
  50. return { $regularExpression: { pattern: this.pattern, options: this.options } };
  51. };
  52. /** @internal */
  53. BSONRegExp.fromExtendedJSON = function (doc) {
  54. if ('$regex' in doc) {
  55. if (typeof doc.$regex !== 'string') {
  56. // This is for $regex query operators that have extended json values.
  57. if (doc.$regex._bsontype === 'BSONRegExp') {
  58. return doc;
  59. }
  60. }
  61. else {
  62. return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
  63. }
  64. }
  65. if ('$regularExpression' in doc) {
  66. return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
  67. }
  68. throw new error_1.BSONTypeError("Unexpected BSONRegExp EJSON object form: " + JSON.stringify(doc));
  69. };
  70. return BSONRegExp;
  71. }());
  72. exports.BSONRegExp = BSONRegExp;
  73. Object.defineProperty(BSONRegExp.prototype, '_bsontype', { value: 'BSONRegExp' });
  74. //# sourceMappingURL=regexp.js.map