Skip to content

Commit 6b49c23

Browse files
authored
fix(bson): normalizedFunctionString handles named functions
Update normalizedFunctionString to correctly normalize the string representation of named functions. Fixes NODE-1552
1 parent 2a54053 commit 6b49c23

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/bson/parser/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @param {Function} fn The function to stringify
66
*/
77
function normalizedFunctionString(fn) {
8-
return fn.toString().replace('function(', 'function (');
8+
return fn.toString().replace(/function(.*)\(/, 'function (');
99
}
1010

1111
module.exports = {

test/node/bson_test.js

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var Buffer = require('buffer').Buffer,
2222
vm = require('vm');
2323

2424
var createBSON = require('../utils');
25+
const normalizedFunctionString = require('../../lib/bson/parser/utils').normalizedFunctionString;
2526

2627
// for tests
2728
BSON.BSON_BINARY_SUBTYPE_DEFAULT = 0;
@@ -2346,4 +2347,17 @@ describe('BSON', function() {
23462347

23472348
expect(bufferRaw).to.deep.equal(uint8ArrayRaw);
23482349
});
2350+
2351+
it('Should normalize variations of the same function to the same string', function() {
2352+
const testObj = { test: function() {}, test2: function test2() {} };
2353+
const testFuncs = [
2354+
function() {},
2355+
function func() {},
2356+
function fUnCtIoN() {},
2357+
testObj['test'],
2358+
testObj['test2']
2359+
];
2360+
const expectedString = 'function () {}';
2361+
testFuncs.forEach(fn => expect(normalizedFunctionString(fn)).to.equal(expectedString));
2362+
});
23492363
});

0 commit comments

Comments
 (0)