Skip to content

Commit f3ca867

Browse files
authored
Merge pull request #14156 from hasezoey/removeUtilsOptions
refactor: remove `utils.options` / `utils.object.shallowCopy`
2 parents 2b93a2e + 006740a commit f3ca867

File tree

13 files changed

+60
-112
lines changed

13 files changed

+60
-112
lines changed

lib/document.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,8 +3690,7 @@ Document.prototype.$toObject = function(options, json) {
36903690
const schemaOptions = this.$__schema && this.$__schema.options || {};
36913691
// merge base default options with Schema's set default options if available.
36923692
// `clone` is necessary here because `utils.options` directly modifies the second input.
3693-
defaultOptions = utils.options(defaultOptions, clone(baseOptions));
3694-
defaultOptions = utils.options(defaultOptions, clone(schemaOptions[path] || {}));
3693+
defaultOptions = { ...defaultOptions, ...baseOptions, ...schemaOptions[path] };
36953694

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

37553754
// merge default options with input options.
3756-
options = utils.options(defaultOptions, options);
3755+
options = { ...defaultOptions, ...options };
37573756
options._isNested = true;
37583757
options.json = json;
37593758
options.minimize = _minimize;

lib/schema.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ Schema.prototype.defaultOptions = function(options) {
560560
const strict = 'strict' in baseOptions ? baseOptions.strict : true;
561561
const strictQuery = 'strictQuery' in baseOptions ? baseOptions.strictQuery : false;
562562
const id = 'id' in baseOptions ? baseOptions.id : true;
563-
options = utils.options({
563+
options = {
564564
strict,
565565
strictQuery,
566566
bufferCommands: true,
@@ -577,8 +577,9 @@ Schema.prototype.defaultOptions = function(options) {
577577
// the following are only applied at construction time
578578
_id: true,
579579
id: id,
580-
typeKey: 'type'
581-
}, clone(options));
580+
typeKey: 'type',
581+
...options
582+
};
582583

583584
if (options.versionKey && typeof options.versionKey !== 'string') {
584585
throw new MongooseError('`versionKey` must be falsy or string, got `' + (typeof options.versionKey) + '`');

lib/schema/bigint.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const CastError = require('../error/cast');
88
const SchemaType = require('../schematype');
99
const castBigInt = require('../cast/bigint');
10-
const utils = require('../utils');
1110

1211
/**
1312
* BigInt SchemaType constructor.
@@ -177,12 +176,13 @@ SchemaBigInt.prototype.cast = function(value) {
177176
* ignore
178177
*/
179178

180-
SchemaBigInt.$conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, {
179+
SchemaBigInt.$conditionalHandlers = {
180+
...SchemaType.prototype.$conditionalHandlers,
181181
$gt: handleSingle,
182182
$gte: handleSingle,
183183
$lt: handleSingle,
184184
$lte: handleSingle
185-
});
185+
};
186186

187187
/*!
188188
* ignore

lib/schema/boolean.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const CastError = require('../error/cast');
88
const SchemaType = require('../schematype');
99
const castBoolean = require('../cast/boolean');
10-
const utils = require('../utils');
1110

1211
/**
1312
* Boolean SchemaType constructor.
@@ -235,8 +234,7 @@ SchemaBoolean.prototype.cast = function(value) {
235234
}
236235
};
237236

238-
SchemaBoolean.$conditionalHandlers =
239-
utils.options(SchemaType.prototype.$conditionalHandlers, {});
237+
SchemaBoolean.$conditionalHandlers = { ...SchemaType.prototype.$conditionalHandlers };
240238

241239
/**
242240
* Casts contents for queries.

lib/schema/buffer.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,17 @@ function handleSingle(val, context) {
250250
return this.castForQuery(null, val, context);
251251
}
252252

253-
SchemaBuffer.prototype.$conditionalHandlers =
254-
utils.options(SchemaType.prototype.$conditionalHandlers, {
255-
$bitsAllClear: handleBitwiseOperator,
256-
$bitsAnyClear: handleBitwiseOperator,
257-
$bitsAllSet: handleBitwiseOperator,
258-
$bitsAnySet: handleBitwiseOperator,
259-
$gt: handleSingle,
260-
$gte: handleSingle,
261-
$lt: handleSingle,
262-
$lte: handleSingle
263-
});
253+
SchemaBuffer.prototype.$conditionalHandlers = {
254+
...SchemaType.prototype.$conditionalHandlers,
255+
$bitsAllClear: handleBitwiseOperator,
256+
$bitsAnyClear: handleBitwiseOperator,
257+
$bitsAllSet: handleBitwiseOperator,
258+
$bitsAnySet: handleBitwiseOperator,
259+
$gt: handleSingle,
260+
$gte: handleSingle,
261+
$lt: handleSingle,
262+
$lte: handleSingle
263+
};
264264

265265
/**
266266
* Casts contents for queries.

lib/schema/date.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,13 @@ function handleSingle(val) {
388388
return this.cast(val);
389389
}
390390

391-
SchemaDate.prototype.$conditionalHandlers =
392-
utils.options(SchemaType.prototype.$conditionalHandlers, {
393-
$gt: handleSingle,
394-
$gte: handleSingle,
395-
$lt: handleSingle,
396-
$lte: handleSingle
397-
});
391+
SchemaDate.prototype.$conditionalHandlers = {
392+
...SchemaType.prototype.$conditionalHandlers,
393+
$gt: handleSingle,
394+
$gte: handleSingle,
395+
$lt: handleSingle,
396+
$lte: handleSingle
397+
};
398398

399399

400400
/**

lib/schema/decimal128.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
const SchemaType = require('../schematype');
88
const CastError = SchemaType.CastError;
99
const castDecimal128 = require('../cast/decimal128');
10-
const utils = require('../utils');
1110
const isBsonType = require('../helpers/isBsonType');
1211

1312
/**
@@ -214,13 +213,13 @@ function handleSingle(val) {
214213
return this.cast(val);
215214
}
216215

217-
Decimal128.prototype.$conditionalHandlers =
218-
utils.options(SchemaType.prototype.$conditionalHandlers, {
219-
$gt: handleSingle,
220-
$gte: handleSingle,
221-
$lt: handleSingle,
222-
$lte: handleSingle
223-
});
216+
Decimal128.prototype.$conditionalHandlers = {
217+
...SchemaType.prototype.$conditionalHandlers,
218+
$gt: handleSingle,
219+
$gte: handleSingle,
220+
$lt: handleSingle,
221+
$lte: handleSingle
222+
};
224223

225224
/*!
226225
* Module exports.

lib/schema/number.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,18 @@ function handleArray(val) {
399399
});
400400
}
401401

402-
SchemaNumber.prototype.$conditionalHandlers =
403-
utils.options(SchemaType.prototype.$conditionalHandlers, {
404-
$bitsAllClear: handleBitwiseOperator,
405-
$bitsAnyClear: handleBitwiseOperator,
406-
$bitsAllSet: handleBitwiseOperator,
407-
$bitsAnySet: handleBitwiseOperator,
408-
$gt: handleSingle,
409-
$gte: handleSingle,
410-
$lt: handleSingle,
411-
$lte: handleSingle,
412-
$mod: handleArray
413-
});
402+
SchemaNumber.prototype.$conditionalHandlers = {
403+
...SchemaType.prototype.$conditionalHandlers,
404+
$bitsAllClear: handleBitwiseOperator,
405+
$bitsAnyClear: handleBitwiseOperator,
406+
$bitsAllSet: handleBitwiseOperator,
407+
$bitsAnySet: handleBitwiseOperator,
408+
$gt: handleSingle,
409+
$gte: handleSingle,
410+
$lt: handleSingle,
411+
$lte: handleSingle,
412+
$mod: handleArray
413+
};
414414

415415
/**
416416
* Casts contents for queries.

lib/schema/objectid.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,13 @@ function handleSingle(val) {
259259
return this.cast(val);
260260
}
261261

262-
ObjectId.prototype.$conditionalHandlers =
263-
utils.options(SchemaType.prototype.$conditionalHandlers, {
264-
$gt: handleSingle,
265-
$gte: handleSingle,
266-
$lt: handleSingle,
267-
$lte: handleSingle
268-
});
262+
ObjectId.prototype.$conditionalHandlers = {
263+
...SchemaType.prototype.$conditionalHandlers,
264+
$gt: handleSingle,
265+
$gte: handleSingle,
266+
$lt: handleSingle,
267+
$lte: handleSingle
268+
};
269269

270270
/*!
271271
* ignore

lib/schema/string.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ function handleSingleNoSetters(val) {
641641
return this.cast(val, this);
642642
}
643643

644-
const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHandlers, {
644+
const $conditionalHandlers = {
645+
...SchemaType.prototype.$conditionalHandlers,
645646
$all: handleArray,
646647
$gt: handleSingle,
647648
$gte: handleSingle,
@@ -656,7 +657,7 @@ const $conditionalHandlers = utils.options(SchemaType.prototype.$conditionalHand
656657
return handleSingleNoSetters.call(this, val);
657658
},
658659
$not: handleSingle
659-
});
660+
};
660661

661662
Object.defineProperty(SchemaString.prototype, '$conditionalHandlers', {
662663
configurable: false,

0 commit comments

Comments
 (0)