Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed; query hint order now retained

  • Loading branch information...
commit 418a5faa19d0bbabceb8cb377befd600447a9402 1 parent 7ae5a82
@aheckmann aheckmann authored
Showing with 55 additions and 22 deletions.
  1. +1 −1  lib/document.js
  2. +2 −1  lib/query.js
  3. +39 −20 lib/utils.js
  4. +13 −0 test/query.test.js
View
2  lib/document.js
@@ -756,7 +756,7 @@ Document.prototype.doQueue = function () {
Document.prototype.toObject = function (ref) {
if (ref) return this._doc;
- return clone(this._doc, true);
+ return clone(this._doc, { minimize: true });
};
/**
View
3  lib/query.js
@@ -251,7 +251,8 @@ Query.prototype.cast = function (model, obj) {
*/
Query.prototype._optionsForExec = function (model) {
- var options = utils.clone(this.options);
+ var options = utils.clone(this.options, { retainKeyOrder: true });
+ delete options.populate;
if (! ('safe' in options)) options.safe = model.options.safe;
return options;
};
View
59 lib/utils.js
@@ -197,23 +197,23 @@ exports.deepEqual = function deepEqual (a, b) {
* as possible.
*
* @param {Object} object to clone
- * @param {Boolean} shouldMinimizeData
+ * @param {Object} options - minimize , retainKeyOrder
* @return {Object} cloned object
* @api private
*/
-var clone = exports.clone = function (obj, shouldMinimizeData) {
+var clone = exports.clone = function clone (obj, options) {
if (obj === undefined || obj === null)
- return obj
+ return obj;
if (Array.isArray(obj))
- return cloneArray(obj, shouldMinimizeData);
+ return cloneArray(obj, options);
if (obj.toObject)
return obj.toObject();
if (obj.constructor == Object)
- return cloneObject(obj, shouldMinimizeData);
+ return cloneObject(obj, options);
if (obj.constructor == Date || obj.constructor == Function)
return new obj.constructor(+obj);
@@ -228,36 +228,55 @@ var clone = exports.clone = function (obj, shouldMinimizeData) {
return obj.valueOf();
};
-function cloneObject (obj, shouldMinimizeData) {
- var ret = {}
- , val
+function cloneObject (obj, options) {
+ var retainKeyOrder = options && options.retainKeyOrder
+ , minimize = options && options.minimize
+ , ret = {}
, hasKeys
- , keys = Object.keys(obj)
- , i = keys.length
- , key
+ , keys
+ , val
+ , k
+ , i
- while (i--) {
- key = keys[i];
- val = clone(obj[key], shouldMinimizeData);
+ if (retainKeyOrder) {
+ for (k in obj) {
+ val = clone(obj[k], options);
- if (!shouldMinimizeData || ('undefined' !== typeof val)) {
- if (!hasKeys) hasKeys = true;
- ret[key] = val;
+ if (!minimize || ('undefined' !== typeof val)) {
+ hasKeys || (hasKeys = true);
+ ret[k] = val;
+ }
+ }
+ } else {
+ // faster
+
+ keys = Object.keys(obj);
+ i = keys.length;
+
+ while (i--) {
+ k = keys[i];
+ val = clone(obj[k], options);
+
+ if (!minimize || ('undefined' !== typeof val)) {
+ if (!hasKeys) hasKeys = true;
+ ret[k] = val;
+ }
}
}
- return shouldMinimizeData
+ return minimize
? hasKeys && ret
: ret;
};
-function cloneArray (arr, shouldMinimizeData) {
+function cloneArray (arr, options) {
var ret = [];
for (var i = 0, l = arr.length; i < l; i++)
- ret.push(clone(arr[i], shouldMinimizeData));
+ ret.push(clone(arr[i], options));
return ret;
};
+
/**
* Copies and merges options with defaults.
*
View
13 test/query.test.js
@@ -729,6 +729,19 @@ module.exports = {
});
},
+ 'optionsForExecute should retain key order': function () {
+ // this is important for query hints
+ var hint = { x: 1, y: 1, z: 1 };
+ var a = JSON.stringify({ hint: hint, safe: true});
+
+ var q = new Query;
+ q.hint(hint);
+
+ var options = q._optionsForExec({ options: { safe: true } });
+
+ a.should.equal(JSON.stringify(options));
+ },
+
// Advanced Query options
'test Query#maxscan': function () {
Please sign in to comment.
Something went wrong with that request. Please try again.