From d091b36dbf1e7a1c93d2e6f4c512dcc976141d4e Mon Sep 17 00:00:00 2001
From: uAndy Mina <andy.mina@mongodb.com>
Date: Fri, 4 Jun 2021 17:25:22 -0400
Subject: [PATCH 1/2] fix(NODE-3282): BSONRegExp options not alphabetized

---
 src/regexp.ts                  |  4 +---
 test/node/bson_corpus_tests.js |  5 +++++
 test/node/bson_regex_tests.js  | 10 ++++++++++
 3 files changed, 16 insertions(+), 3 deletions(-)
 create mode 100644 test/node/bson_regex_tests.js

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_regex_tests.js b/test/node/bson_regex_tests.js
new file mode 100644
index 00000000..7c22af12
--- /dev/null
+++ b/test/node/bson_regex_tests.js
@@ -0,0 +1,10 @@
+'use strict';
+const BSON = require('../register-bson');
+const BSONRegExp = BSON.BSONRegExp;
+
+describe('BSONRegExp', () => {
+  it('Should alphabetize options', () => {
+    const b = new BSONRegExp('cba', 'mix');
+    expect(b.options).to.equal('imx');
+  });
+});

From f07ef77c12a22ad9bbbd3fa8db2633cbcd710911 Mon Sep 17 00:00:00 2001
From: uAndy Mina <andy.mina@mongodb.com>
Date: Tue, 8 Jun 2021 15:38:37 -0400
Subject: [PATCH 2/2] fix(NODE-3282): moved alphabetize options test

---
 test/node/bson_regex_tests.js | 10 ----------
 test/node/bson_test.js        |  7 +++++++
 2 files changed, 7 insertions(+), 10 deletions(-)
 delete mode 100644 test/node/bson_regex_tests.js

diff --git a/test/node/bson_regex_tests.js b/test/node/bson_regex_tests.js
deleted file mode 100644
index 7c22af12..00000000
--- a/test/node/bson_regex_tests.js
+++ /dev/null
@@ -1,10 +0,0 @@
-'use strict';
-const BSON = require('../register-bson');
-const BSONRegExp = BSON.BSONRegExp;
-
-describe('BSONRegExp', () => {
-  it('Should alphabetize options', () => {
-    const b = new BSONRegExp('cba', 'mix');
-    expect(b.options).to.equal('imx');
-  });
-});
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
    */