Source: node_modules/bson/lib/bson/regexp.js

  1. 'use strict';
  2. function alphabetize(str) {
  3. return str
  4. .split('')
  5. .sort()
  6. .join('');
  7. }
  8. /**
  9. * A class representation of the BSON RegExp type.
  10. *
  11. * @class
  12. * @return {BSONRegExp} A MinKey instance
  13. */
  14. function BSONRegExp(pattern, options) {
  15. if (!(this instanceof BSONRegExp)) return new BSONRegExp(pattern, options);
  16. // Execute
  17. this._bsontype = 'BSONRegExp';
  18. this.pattern = pattern || '';
  19. this.options = options ? alphabetize(options) : '';
  20. // Validate options
  21. for (var i = 0; i < this.options.length; i++) {
  22. if (
  23. !(
  24. this.options[i] === 'i' ||
  25. this.options[i] === 'm' ||
  26. this.options[i] === 'x' ||
  27. this.options[i] === 'l' ||
  28. this.options[i] === 's' ||
  29. this.options[i] === 'u'
  30. )
  31. ) {
  32. throw new Error('the regular expression options [' + this.options[i] + '] is not supported');
  33. }
  34. }
  35. }
  36. module.exports = BSONRegExp;
  37. module.exports.BSONRegExp = BSONRegExp;