Skip to content

Commit

Permalink
refactor(createIndex): simplify argument management for createIndex
Browse files Browse the repository at this point in the history
Collection.prototype.createIndex, and Db.prototype.createIndex both
had rather complicated argument management code to handle legacy
forms of the method. Since we have committed to an explicit
signature, we can now simplfy this code as well.
  • Loading branch information
mbroadst committed Dec 24, 2017
1 parent ad6f67c commit 5461f46
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
8 changes: 2 additions & 6 deletions lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,12 +1512,8 @@ define.classMethod('isCapped', { callback: true, promise: true });
* @return {Promise} returns Promise if no callback passed
*/
Collection.prototype.createIndex = function(fieldOrSpec, options, callback) {
var args = Array.prototype.slice.call(arguments, 1);
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;

options = args.length ? args.shift() || {} : {};
options = typeof callback === 'function' ? options : callback;
options = options == null ? {} : options;
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};

return executeOperation(this.s.topology, createIndex, [this, fieldOrSpec, options, callback]);
};
Expand Down
9 changes: 2 additions & 7 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,13 +1043,8 @@ define.classMethod('executeDbAdminCommand', { callback: true, promise: true });
* @return {Promise} returns Promise if no callback passed
*/
Db.prototype.createIndex = function(name, fieldOrSpec, options, callback) {
var args = Array.prototype.slice.call(arguments, 2);
callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
options = args.length ? args.shift() || {} : {};
options = typeof callback === 'function' ? options : callback;
options = options == null ? {} : options;
// Shallow clone the options
options = shallowClone(options);
if (typeof options === 'function') (callback = options), (options = {});
options = options ? shallowClone(options) : {};

return executeOperation(this.s.topology, createIndex, [
this,
Expand Down

0 comments on commit 5461f46

Please sign in to comment.