65 lines
2.3 KiB
JavaScript
65 lines
2.3 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.BSONRegExp = void 0;
|
|
function alphabetize(str) {
|
|
return str.split('').sort().join('');
|
|
}
|
|
/**
|
|
* A class representation of the BSON RegExp type.
|
|
* @public
|
|
*/
|
|
class BSONRegExp {
|
|
/**
|
|
* @param pattern - The regular expression pattern to match
|
|
* @param options - The regular expression options
|
|
*/
|
|
constructor(pattern, options) {
|
|
this.pattern = pattern;
|
|
this.options = options !== null && options !== void 0 ? options : '';
|
|
// Execute
|
|
alphabetize(this.options);
|
|
// Validate options
|
|
for (let i = 0; i < this.options.length; i++) {
|
|
if (!(this.options[i] === 'i' ||
|
|
this.options[i] === 'm' ||
|
|
this.options[i] === 'x' ||
|
|
this.options[i] === 'l' ||
|
|
this.options[i] === 's' ||
|
|
this.options[i] === 'u')) {
|
|
throw new Error(`The regular expression option [${this.options[i]}] is not supported`);
|
|
}
|
|
}
|
|
}
|
|
static parseOptions(options) {
|
|
return options ? options.split('').sort().join('') : '';
|
|
}
|
|
/** @internal */
|
|
toExtendedJSON(options) {
|
|
options = options || {};
|
|
if (options.legacy) {
|
|
return { $regex: this.pattern, $options: this.options };
|
|
}
|
|
return { $regularExpression: { pattern: this.pattern, options: this.options } };
|
|
}
|
|
/** @internal */
|
|
static fromExtendedJSON(doc) {
|
|
if ('$regex' in doc) {
|
|
if (typeof doc.$regex !== 'string') {
|
|
// This is for $regex query operators that have extended json values.
|
|
if (doc.$regex._bsontype === 'BSONRegExp') {
|
|
return doc;
|
|
}
|
|
}
|
|
else {
|
|
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
|
|
}
|
|
}
|
|
if ('$regularExpression' in doc) {
|
|
return new BSONRegExp(doc.$regularExpression.pattern, BSONRegExp.parseOptions(doc.$regularExpression.options));
|
|
}
|
|
throw new TypeError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
|
|
}
|
|
}
|
|
exports.BSONRegExp = BSONRegExp;
|
|
Object.defineProperty(BSONRegExp.prototype, '_bsontype', { value: 'BSONRegExp' });
|
|
//# sourceMappingURL=regexp.js.map
|