Skip to content

Commit

Permalink
fix: rename and improve function
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Jul 10, 2020
1 parent 0cd851b commit ad96e63
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
11 changes: 4 additions & 7 deletions lib/mongodb.js
Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions test/mongodb.test.js
Expand Up @@ -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;

Expand Down Expand Up @@ -3727,24 +3727,24 @@ describe('mongodb connector', function() {
});
});

context('removingDollarSign', () =>{
context('trimLeadingDollarSigns', () =>{
it('removes extra dollar sign(s) in ths operators', () => {
let spec = '$eq';
let updatedSpec = removingDollarSign(spec);
let updatedSpec = trimLeadingDollarSigns(spec);
updatedSpec.should.equal('eq');

spec = '$$eq';
updatedSpec = removingDollarSign(spec);
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);
let updatedSpec = trimLeadingDollarSigns(spec);
updatedSpec.should.equal(spec);

spec = 'eq$';
updatedSpec = removingDollarSign(spec);
updatedSpec = trimLeadingDollarSigns(spec);
updatedSpec.should.equal('eq$');
});
});
Expand Down

0 comments on commit ad96e63

Please sign in to comment.