From 3771f499935c899a76b4cec767bf75f1544bf0ff Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Mon, 6 Nov 2017 08:32:59 -0800 Subject: [PATCH] refactor(query): move selectedInclusively() into separate helper Re: #5737 --- lib/query.js | 19 ++----------------- lib/services/projection/isInclusive.js | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 lib/services/projection/isInclusive.js diff --git a/lib/query.js b/lib/query.js index bd781e852d9..4fd66362eda 100644 --- a/lib/query.js +++ b/lib/query.js @@ -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'); @@ -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); }; /** diff --git a/lib/services/projection/isInclusive.js b/lib/services/projection/isInclusive.js new file mode 100644 index 00000000000..e8dded7dfb3 --- /dev/null +++ b/lib/services/projection/isInclusive.js @@ -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; +};