Skip to content
This repository has been archived by the owner on Feb 3, 2022. It is now read-only.

Commit

Permalink
fix(parsing): support encoding and decoding RegExps
Browse files Browse the repository at this point in the history
Fixes NODE-1464
  • Loading branch information
kvwalker committed May 22, 2018
1 parent 44afbb8 commit 06691f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/ext_json.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,14 @@ function serializeDocument(doc, options) {
} else if (val != null && typeof val === 'object') {
_doc[name] = serializeDocument(val, options);
}

_doc[name] = serializeValue(val, options);
if (val instanceof RegExp) {
let flags = val.flags;
if (flags === undefined) {
flags = val.toString().match(/[gimuy]*$/)[0];
}
_doc[name] = codecs['BSONRegExp'].toExtendedJSON({ pattern: val.source, options: flags });
}
}

return _doc;
Expand Down
9 changes: 9 additions & 0 deletions test/extend_mongodb_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ describe('Extended JSON', function() {
date: dt
});
});

it('should allow regexp', function() {
const parsedRegExp = extJSON.stringify({ test: /some-regex/i }, { relaxed: true });
const parsedBSONRegExp = extJSON.stringify(
{ test: new BSONRegExp('some-regex', 'i') },
{ relaxed: true }
);
expect(parsedRegExp).to.eql(parsedBSONRegExp);
});
});
});
});

0 comments on commit 06691f6

Please sign in to comment.