From 516bd88e23dd0bf5d10347e7661621c1e381e895 Mon Sep 17 00:00:00 2001 From: ShubhamEHRAccount Date: Wed, 14 Aug 2024 22:37:16 +0530 Subject: [PATCH 1/2] check collection name to be no empty string before running function on it for pluralize --- lib/utils.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/utils.js b/lib/utils.js index 9af4b12727e..b7ed4ec0425 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -53,6 +53,12 @@ exports.toCollectionName = function(name, pluralize) { return name; } if (typeof pluralize === 'function') { + if (typeof name !== 'string') { + throw new TypeError('Collection name must be a string'); + } + if (name.length === 0) { + throw new TypeError('Collection name cannot be empty'); + } return pluralize(name); } return name; From c0801a861b1ddc2ba774fc21ae839eca002cd09b Mon Sep 17 00:00:00 2001 From: ShubhamEHRAccount Date: Wed, 14 Aug 2024 22:53:08 +0530 Subject: [PATCH 2/2] add test case for toCollectionName function --- test/utils.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/utils.test.js b/test/utils.test.js index e8cf47b76b8..97904166614 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -324,4 +324,31 @@ describe('utils', function() { assert.deepEqual(pojoError.metadata, { hello: 'world' }); }); }); + + describe('toCollectionName', function() { + it('returns the same name for system.profile', function() { + assert.equal(utils.toCollectionName('system.profile'), 'system.profile'); + }); + + it('returns the same name for system.indexes', function() { + assert.equal(utils.toCollectionName('system.indexes'), 'system.indexes'); + }); + + it('throws an error when name is not a string', function() { + assert.throws(() => { + utils.toCollectionName(123, () => {}); + }, /Collection name must be a string/); + }); + + it('throws an error when name is an empty string', function() { + assert.throws(() => { + utils.toCollectionName('', () => {}); + }, /Collection name cannot be empty/); + }); + + it('uses the pluralize function when provided', function() { + const pluralize = (name) => name + 's'; + assert.equal(utils.toCollectionName('test', pluralize), 'tests'); + }); + }); });