Skip to content

Commit 712aecc

Browse files
refactor: replace hasOwnProperty with Object.hasOwn for safer property checks
1 parent d043ae1 commit 712aecc

File tree

5 files changed

+10
-24
lines changed

5 files changed

+10
-24
lines changed

lib/helpers/projection/applyProjection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function applyExclusiveProjection(doc, projection, hasIncludedChildren, projecti
4343

4444
for (const key of Object.keys(ret)) {
4545
const fullPath = prefix ? prefix + '.' + key : key;
46-
if (projection.hasOwnProperty(fullPath) || projectionLimb.hasOwnProperty(key)) {
46+
if (Object.hasOwn(projection, fullPath) || Object.hasOwn(projectionLimb, key)) {
4747
if (isPOJO(projection[fullPath]) || isPOJO(projectionLimb[key])) {
4848
ret[key] = applyExclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath);
4949
} else {

lib/helpers/update/decorateUpdateWithVersionKey.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ function hasKey(obj, key) {
3131
if (obj == null || typeof obj !== 'object') {
3232
return false;
3333
}
34-
return Object.prototype.hasOwnProperty.call(obj, key);
34+
return Object.hasOwn(obj, key);
3535
}

lib/types/array/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ function MongooseArray(values, path, doc, schematype) {
7979

8080
const proxy = new Proxy(__array, {
8181
get: function(target, prop) {
82-
if (internals.hasOwnProperty(prop)) {
82+
if (Object.hasOwn(internals, prop)) {
8383
return internals[prop];
8484
}
85-
if (mongooseArrayMethods.hasOwnProperty(prop)) {
85+
if (Object.hasOwn(mongooseArrayMethods, prop)) {
8686
return mongooseArrayMethods[prop];
8787
}
88-
if (schematype && schematype.virtuals && schematype.virtuals.hasOwnProperty(prop)) {
88+
if (schematype && schematype.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
8989
return schematype.virtuals[prop].applyGetters(undefined, target);
9090
}
9191
if (typeof prop === 'string' && numberRE.test(prop) && schematype?.embeddedSchemaType != null) {

lib/types/documentArray/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ function MongooseDocumentArray(values, path, doc, schematype) {
7373
prop === 'isMongooseDocumentArrayProxy') {
7474
return true;
7575
}
76-
if (internals.hasOwnProperty(prop)) {
76+
if (Object.hasOwn(internals, prop)) {
7777
return internals[prop];
7878
}
79-
if (DocumentArrayMethods.hasOwnProperty(prop)) {
79+
if (Object.hasOwn(DocumentArrayMethods, prop)) {
8080
return DocumentArrayMethods[prop];
8181
}
82-
if (schematype && schematype.virtuals && schematype.virtuals.hasOwnProperty(prop)) {
82+
if (schematype && schematype.virtuals && Object.hasOwn(schematype.virtuals, prop)) {
8383
return schematype.virtuals[prop].applyGetters(undefined, target);
8484
}
85-
if (ArrayMethods.hasOwnProperty(prop)) {
85+
if (Object.hasOwn(ArrayMethods, prop)) {
8686
return ArrayMethods[prop];
8787
}
8888

lib/utils.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -711,18 +711,6 @@ exports.object.vals = function vals(o) {
711711
return ret;
712712
};
713713

714-
const hop = Object.prototype.hasOwnProperty;
715-
716-
/**
717-
* Safer helper for hasOwnProperty checks
718-
*
719-
* @param {Object} obj
720-
* @param {String} prop
721-
*/
722-
723-
exports.object.hasOwnProperty = function(obj, prop) {
724-
return hop.call(obj, prop);
725-
};
726714

727715
/**
728716
* Determine if `val` is null or undefined
@@ -773,8 +761,6 @@ exports.array.flatten = function flatten(arr, filter, ret) {
773761
* ignore
774762
*/
775763

776-
const _hasOwnProperty = Object.prototype.hasOwnProperty;
777-
778764
exports.hasUserDefinedProperty = function(obj, key) {
779765
if (obj == null) {
780766
return false;
@@ -789,7 +775,7 @@ exports.hasUserDefinedProperty = function(obj, key) {
789775
return false;
790776
}
791777

792-
if (_hasOwnProperty.call(obj, key)) {
778+
if (Object.hasOwn(obj, key)) {
793779
return true;
794780
}
795781
if (typeof obj === 'object' && key in obj) {

0 commit comments

Comments
 (0)