Skip to content

Commit c2b9819

Browse files
author
substack
committedMar 22, 2022
isConstructorOrProto adapted from PR
1 parent bc8ecee commit c2b9819

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed
 

‎index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ module.exports = function (args, opts) {
7070
var o = obj;
7171
for (var i = 0; i < keys.length-1; i++) {
7272
var key = keys[i];
73-
if (key === '__proto__') return;
73+
if (isConstructorOrProto(o, key)) return;
7474
if (o[key] === undefined) o[key] = {};
7575
if (o[key] === Object.prototype || o[key] === Number.prototype
7676
|| o[key] === String.prototype) o[key] = {};
@@ -79,7 +79,7 @@ module.exports = function (args, opts) {
7979
}
8080

8181
var key = keys[keys.length - 1];
82-
if (key === '__proto__') return;
82+
if (isConstructorOrProto(o, key)) return;
8383
if (o === Object.prototype || o === Number.prototype
8484
|| o === String.prototype) o = {};
8585
if (o === Array.prototype) o = [];
@@ -243,3 +243,7 @@ function isNumber (x) {
243243
return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
244244
}
245245

246+
247+
function isConstructorOrProto (obj, key) {
248+
return key === 'constructor' && typeof obj[key] === 'function' || key === '__proto__';
249+
}

0 commit comments

Comments
 (0)
Please sign in to comment.