diff --git a/src/regexp.ts b/src/regexp.ts index 4225f136..76d1f5e9 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -35,9 +35,7 @@ export class BSONRegExp { if (!(this instanceof BSONRegExp)) return new BSONRegExp(pattern, options); this.pattern = pattern; - this.options = options ?? ''; - // Execute - alphabetize(this.options); + this.options = alphabetize(options ?? ''); // Validate options for (let i = 0; i < this.options.length; i++) { diff --git a/test/node/bson_corpus_tests.js b/test/node/bson_corpus_tests.js index 1c4c6eec..d6be7fa3 100644 --- a/test/node/bson_corpus_tests.js +++ b/test/node/bson_corpus_tests.js @@ -107,6 +107,11 @@ describe('BSON Corpus', function () { if (v.degenerate_bson) { const dB = Buffer.from(v.degenerate_bson, 'hex'); + // Degenerate BSON to JS equals canonical BSON in JS + expect(BSON.deserialize(cB, deserializeOptions)).to.deep.equal( + BSON.deserialize(dB, deserializeOptions) + ); + // Dengenerate BSON roundtripped is transformed to canonical BSON expect(cB).to.deep.equal( BSON.serialize(BSON.deserialize(dB, deserializeOptions), serializeOptions) ); diff --git a/test/node/bson_test.js b/test/node/bson_test.js index 81134994..92646c1b 100644 --- a/test/node/bson_test.js +++ b/test/node/bson_test.js @@ -2164,6 +2164,13 @@ describe('BSON', function () { done(); }); + describe('BSONRegExp', () => { + it('Should alphabetize options', () => { + const b = new BSONRegExp('cba', 'mix'); + expect(b.options).to.equal('imx'); + }); + }); + /** * @ignore */