Skip to content

Commit

Permalink
fix(bson): normalizedFunctionString handles named functions
Browse files Browse the repository at this point in the history
Update normalizedFunctionString to correctly normalize the string
representation of named functions.

Fixes NODE-1552
  • Loading branch information
rweinberger authored Jul 5, 2018
1 parent 2a54053 commit 6b49c23
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bson/parser/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
14 changes: 14 additions & 0 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
});
});

0 comments on commit 6b49c23

Please sign in to comment.