regexp.js 2.9 KB

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