Skip to content

Commit

Permalink
Merge pull request #14156 from hasezoey/removeUtilsOptions
Browse files Browse the repository at this point in the history
refactor: remove `utils.options` / `utils.object.shallowCopy`
  • Loading branch information
vkarpov15 committed Dec 5, 2023
2 parents 2b93a2e + 006740a commit f3ca867
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 112 deletions.
5 changes: 2 additions & 3 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -3690,8 +3690,7 @@ Document.prototype.$toObject = function(options, json) {
const schemaOptions = this.$__schema && this.$__schema.options || {};
// merge base default options with Schema's set default options if available.
// `clone` is necessary here because `utils.options` directly modifies the second input.
defaultOptions = utils.options(defaultOptions, clone(baseOptions));
defaultOptions = utils.options(defaultOptions, clone(schemaOptions[path] || {}));
defaultOptions = { ...defaultOptions, ...baseOptions, ...schemaOptions[path] };

// If options do not exist or is not an object, set it to empty object
options = utils.isPOJO(options) ? { ...options } : {};
Expand Down Expand Up @@ -3753,7 +3752,7 @@ Document.prototype.$toObject = function(options, json) {
}

// merge default options with input options.
options = utils.options(defaultOptions, options);
options = { ...defaultOptions, ...options };
options._isNested = true;
options.json = json;
options.minimize = _minimize;
Expand Down
7 changes: 4 additions & 3 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ Schema.prototype.defaultOptions = function(options) {
const strict = 'strict' in baseOptions ? baseOptions.strict : true;
const strictQuery = 'strictQuery' in baseOptions ? baseOptions.strictQuery : false;
const id = 'id' in baseOptions ? baseOptions.id : true;
options = utils.options({
options = {
strict,
strictQuery,
bufferCommands: true,
Expand All @@ -577,8 +577,9 @@ Schema.prototype.defaultOptions = function(options) {
// the following are only applied at construction time
_id: true,
id: id,
typeKey: 'type'
}, clone(options));
typeKey: 'type',
...options
};

if (options.versionKey && typeof options.versionKey !== 'string') {
throw new MongooseError('`versionKey` must be falsy or string, got `' + (typeof options.versionKey) + '`');
Expand Down
6 changes: 3 additions & 3 deletions lib/schema/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
const CastError = require('../error/cast');
const SchemaType = require('../schematype');
const castBigInt = require('../cast/bigint');
const utils = require('../utils');

/**
* BigInt SchemaType constructor.
Expand Down Expand Up @@ -177,12 +176,13 @@ SchemaBigInt.prototype.cast = function(value) {
* ignore
*/

SchemaBigInt.$conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, {
SchemaBigInt.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
});
};

/*!
* ignore
Expand Down
4 changes: 1 addition & 3 deletions lib/schema/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
const CastError = require('../error/cast');
const SchemaType = require('../schematype');
const castBoolean = require('../cast/boolean');
const utils = require('../utils');

/**
* Boolean SchemaType constructor.
Expand Down Expand Up @@ -235,8 +234,7 @@ SchemaBoolean.prototype.cast = function(value) {
}
};

SchemaBoolean.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {});
SchemaBoolean.$conditionalHandlers = { ...SchemaType.prototype.$conditionalHandlers };

/**
* Casts contents for queries.
Expand Down
22 changes: 11 additions & 11 deletions lib/schema/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,17 +250,17 @@ function handleSingle(val, context) {
return this.castForQuery(null, val, context);
}

SchemaBuffer.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
$bitsAllClear: handleBitwiseOperator,
$bitsAnyClear: handleBitwiseOperator,
$bitsAllSet: handleBitwiseOperator,
$bitsAnySet: handleBitwiseOperator,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
});
SchemaBuffer.prototype.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$bitsAllClear: handleBitwiseOperator,
$bitsAnyClear: handleBitwiseOperator,
$bitsAllSet: handleBitwiseOperator,
$bitsAnySet: handleBitwiseOperator,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
};

/**
* Casts contents for queries.
Expand Down
14 changes: 7 additions & 7 deletions lib/schema/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ function handleSingle(val) {
return this.cast(val);
}

SchemaDate.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
});
SchemaDate.prototype.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
};


/**
Expand Down
15 changes: 7 additions & 8 deletions lib/schema/decimal128.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
const SchemaType = require('../schematype');
const CastError = SchemaType.CastError;
const castDecimal128 = require('../cast/decimal128');
const utils = require('../utils');
const isBsonType = require('../helpers/isBsonType');

/**
Expand Down Expand Up @@ -214,13 +213,13 @@ function handleSingle(val) {
return this.cast(val);
}

Decimal128.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
});
Decimal128.prototype.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
};

/*!
* Module exports.
Expand Down
24 changes: 12 additions & 12 deletions lib/schema/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,18 +399,18 @@ function handleArray(val) {
});
}

SchemaNumber.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
$bitsAllClear: handleBitwiseOperator,
$bitsAnyClear: handleBitwiseOperator,
$bitsAllSet: handleBitwiseOperator,
$bitsAnySet: handleBitwiseOperator,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle,
$mod: handleArray
});
SchemaNumber.prototype.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$bitsAllClear: handleBitwiseOperator,
$bitsAnyClear: handleBitwiseOperator,
$bitsAllSet: handleBitwiseOperator,
$bitsAnySet: handleBitwiseOperator,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle,
$mod: handleArray
};

/**
* Casts contents for queries.
Expand Down
14 changes: 7 additions & 7 deletions lib/schema/objectid.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,13 @@ function handleSingle(val) {
return this.cast(val);
}

ObjectId.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
});
ObjectId.prototype.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
};

/*!
* ignore
Expand Down
5 changes: 3 additions & 2 deletions lib/schema/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ function handleSingleNoSetters(val) {
return this.cast(val, this);
}

const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, {
const $conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$all: handleArray,
$gt: handleSingle,
$gte: handleSingle,
Expand All @@ -656,7 +657,7 @@ const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHand
return handleSingleNoSetters.call(this, val);
},
$not: handleSingle
});
};

Object.defineProperty(SchemaString.prototype, '$conditionalHandlers', {
configurable: false,
Expand Down
6 changes: 3 additions & 3 deletions lib/schema/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ function handleArray(val) {
});
}

SchemaUUID.prototype.$conditionalHandlers =
utils.options(SchemaType.prototype.$conditionalHandlers, {
SchemaUUID.prototype.$conditionalHandlers = {
...SchemaType.prototype.$conditionalHandlers,
$bitsAllClear: handleBitwiseOperator,
$bitsAnyClear: handleBitwiseOperator,
$bitsAllSet: handleBitwiseOperator,
Expand All @@ -327,7 +327,7 @@ utils.options(SchemaType.prototype.$conditionalHandlers, {
$lte: handleSingle,
$ne: handleSingle,
$nin: handleArray
});
};

/**
* Casts contents for queries.
Expand Down
27 changes: 0 additions & 27 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,33 +227,6 @@ exports.omit = function omit(obj, keys) {
return ret;
};


/**
* Shallow copies defaults into options.
*
* @param {Object} defaults
* @param {Object} [options]
* @return {Object} the merged object
* @api private
*/

exports.options = function(defaults, options) {
const keys = Object.keys(defaults);
let i = keys.length;
let k;

options = options || {};

while (i--) {
k = keys[i];
if (!(k in options)) {
options[k] = defaults[k];
}
}

return options;
};

/**
* Merges `from` into `to` without overwriting existing properties.
*
Expand Down
23 changes: 0 additions & 23 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,29 +121,6 @@ describe('utils', function() {
});
});

it('utils.options', function() {
const o = { a: 1, b: 2, c: 3, 0: 'zero1' };
const defaults = { b: 10, d: 20, 0: 'zero2' };
const result = utils.options(defaults, o);
assert.equal(result.a, 1);
assert.equal(result.b, 2);
assert.equal(result.c, 3);
assert.equal(result.d, 20);
assert.deepEqual(o.d, result.d);
assert.equal(result['0'], 'zero1');

const result2 = utils.options(defaults);
assert.equal(result2.b, 10);
assert.equal(result2.d, 20);
assert.equal(result2['0'], 'zero2');

// same properties/vals
assert.deepEqual(defaults, result2);

// same object
assert.notEqual(defaults, result2);
});

it('deepEquals on ObjectIds', function() {
const s = (new ObjectId()).toString();

Expand Down

0 comments on commit f3ca867

Please sign in to comment.