From a978ae8339ec43b801c6b53526b98499a9ac6885 Mon Sep 17 00:00:00 2001 From: Agnes Lin Date: Tue, 7 Jul 2020 19:38:33 -0400 Subject: [PATCH] fix: rename and improve function --- lib/mongodb.js | 11 ++++------- test/mongodb.test.js | 32 +++++++++++++++++--------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/lib/mongodb.js b/lib/mongodb.js index 9c661f10d..ab46ec062 100644 --- a/lib/mongodb.js +++ b/lib/mongodb.js @@ -992,7 +992,7 @@ MongoDB.prototype.buildWhere = function(modelName, where, options) { const modelCtor = self._models[modelName]; if (spec) { - spec = removingDollarSign(spec); + spec = trimLeadingDollarSigns(spec); if (spec === 'between') { query[k] = {$gte: cond[0], $lte: cond[1]}; } else if (spec === 'inq') { @@ -1999,14 +1999,11 @@ function isObjectIDProperty(modelCtor, propDef, value, options) { * * @param {*} spec the operator for Where filter */ -function removingDollarSign(spec) { - while (spec.charAt(0) === '$') { - spec = spec.substr(1); - } - return spec; +function trimLeadingDollarSigns(spec) { + return spec.replace(/^(\$)+/, ''); } -exports.removingDollarSign = removingDollarSign; +exports.trimLeadingDollarSigns = trimLeadingDollarSigns; function sanitizeFilter(filter, options) { options = Object.assign({}, options); diff --git a/test/mongodb.test.js b/test/mongodb.test.js index 859334b01..2ffb09f73 100644 --- a/test/mongodb.test.js +++ b/test/mongodb.test.js @@ -12,7 +12,7 @@ const testUtils = require('../lib/test-utils'); const async = require('async'); const sinon = require('sinon'); const sanitizeFilter = require('../lib/mongodb').sanitizeFilter; -const removingDollarSign = require('../lib/mongodb').removingDollarSign; +const trimLeadingDollarSigns = require('../lib/mongodb').trimLeadingDollarSigns; const GeoPoint = require('loopback-datasource-juggler').GeoPoint; @@ -3727,25 +3727,27 @@ describe('mongodb connector', function() { }); }); - context('removingDollarSign', () =>{ - it('removes extra dollar sign(s) in ths operators', () => { - let spec = '$eq'; - let updatedSpec = removingDollarSign(spec); + context('trimLeadingDollarSigns', () =>{ + it('removes an extra leading dollar sign in ths operators', () => { + const spec = '$eq'; + const updatedSpec = trimLeadingDollarSigns(spec); updatedSpec.should.equal('eq'); - - spec = '$$eq'; - updatedSpec = removingDollarSign(spec); + }); + it('removes extra leading dollar signs in ths operators', () => { + const spec = '$$eq'; + const updatedSpec = trimLeadingDollarSigns(spec); updatedSpec.should.equal('eq'); }); - it('remains the same if there is the input does not start with dollar sign(s)', () => { - let spec = 'eq'; - let updatedSpec = removingDollarSign(spec); + it('remains the same if the input does not contain any dollar signs', () => { + const spec = 'eq'; + const updatedSpec = trimLeadingDollarSigns(spec); + updatedSpec.should.equal(spec); + }); + it('remains the same if the input does not start with dollar signs', () => { + const spec = 'eq$'; + const updatedSpec = trimLeadingDollarSigns(spec); updatedSpec.should.equal(spec); - - spec = 'eq$'; - updatedSpec = removingDollarSign(spec); - updatedSpec.should.equal('eq$'); }); }); context('sanitizeFilter()', () => {