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 a978ae8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 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
32 changes: 17 additions & 15 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,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()', () => {
Expand Down

0 comments on commit a978ae8

Please sign in to comment.