Skip to content

Commit

Permalink
refactor(query): move selectedInclusively() into separate helper
Browse files Browse the repository at this point in the history
Re: #5737
  • Loading branch information
vkarpov15 committed Nov 6, 2017
1 parent c93fdae commit 3771f49
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
19 changes: 2 additions & 17 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var cast = require('./cast');
var castUpdate = require('./services/query/castUpdate');
var hasDollarKeys = require('./services/query/hasDollarKeys');
var helpers = require('./queryhelpers');
var isInclusive = require('./services/projection/isInclusive');
var mquery = require('mquery');
var readPref = require('./drivers').ReadPreference;
var selectPopulatedFields = require('./services/query/selectPopulatedFields');
Expand Down Expand Up @@ -3828,23 +3829,7 @@ Query.prototype.centerSphere = function() {
*/

Query.prototype.selectedInclusively = function selectedInclusively() {
if (!this._fields) {
return false;
}

var keys = Object.keys(this._fields);
if (keys.length === 0) {
return false;
}

for (var i = 0; i < keys.length; ++i) {
var key = keys[i];
if (typeof this._fields[key] !== 'object' && !!this._fields[key]) {
return true;
}
}

return false;
return isInclusive(this._fields);
};

/**
Expand Down
24 changes: 24 additions & 0 deletions lib/services/projection/isInclusive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

module.exports = function isInclusive(projection) {
if (projection == null) {
return false;
}

var props = Object.keys(projection);
var numProps = props.length;
if (numProps === 0) {
return false;
}

for (var i = 0; i < numProps; ++i) {
var prop = props[i];
// If field is truthy (1, true, etc.) and not an object, then this
// projection must be inclusive. If object, assume its $meta, $slice, etc.
if (typeof projection[prop] !== 'object' && !!projection[prop]) {
return true;
}
}

return false;
};

0 comments on commit 3771f49

Please sign in to comment.