diff --git a/lib/bson/parser/utils.js b/lib/bson/parser/utils.js index 4a241773..c86c0e37 100644 --- a/lib/bson/parser/utils.js +++ b/lib/bson/parser/utils.js @@ -5,7 +5,7 @@ * @param {Function} fn The function to stringify */ function normalizedFunctionString(fn) { - return fn.toString().replace('function(', 'function ('); + return fn.toString().replace(/function(.*)\(/, 'function ('); } module.exports = { diff --git a/test/node/bson_test.js b/test/node/bson_test.js index de7432f3..b06cd901 100644 --- a/test/node/bson_test.js +++ b/test/node/bson_test.js @@ -22,6 +22,7 @@ var Buffer = require('buffer').Buffer, vm = require('vm'); var createBSON = require('../utils'); +const normalizedFunctionString = require('../../lib/bson/parser/utils').normalizedFunctionString; // for tests BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0; @@ -2346,4 +2347,17 @@ describe('BSON', function() { expect(bufferRaw).to.deep.equal(uint8ArrayRaw); }); + + it('Should normalize variations of the same function to the same string', function() { + const testObj = { test: function() {}, test2: function test2() {} }; + const testFuncs = [ + function() {}, + function func() {}, + function fUnCtIoN() {}, + testObj['test'], + testObj['test2'] + ]; + const expectedString = 'function () {}'; + testFuncs.forEach(fn => expect(normalizedFunctionString(fn)).to.equal(expectedString)); + }); });