Skip to content

Commit 3ef0ded

Browse files
committed
avoid instanceof where it's possible since .prototype and @@hasInstance can be replaced
1 parent 1a05cc3 commit 3ef0ded

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+291
-200
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ const base = {
225225
'linebreak-style': [ERROR, 'unix'],
226226
// specify the maximum length of a line in your program
227227
'max-len': [ERROR, {
228-
code: 120,
228+
code: 140,
229229
tabWidth: 2,
230230
ignoreRegExpLiterals: true,
231231
ignoreTemplateLiterals: true,

packages/core-js-pure/override/internals/collection.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,21 @@ module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {
3434
InternalMetadataModule.enable();
3535
} else {
3636
Constructor = wrapper(function (target, iterable) {
37-
setInternalState(anInstance(target, Constructor, CONSTRUCTOR_NAME), {
37+
setInternalState(anInstance(target, Prototype), {
3838
type: CONSTRUCTOR_NAME,
3939
collection: new NativeConstructor()
4040
});
4141
if (iterable != undefined) iterate(iterable, target[ADDER], { that: target, AS_ENTRIES: IS_MAP });
4242
});
4343

44+
var Prototype = Constructor.prototype;
45+
4446
var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);
4547

4648
forEach(['add', 'clear', 'delete', 'forEach', 'get', 'has', 'set', 'keys', 'values', 'entries'], function (KEY) {
4749
var IS_ADDER = KEY == 'add' || KEY == 'set';
4850
if (KEY in NativePrototype && !(IS_WEAK && KEY == 'clear')) {
49-
createNonEnumerableProperty(Constructor.prototype, KEY, function (a, b) {
51+
createNonEnumerableProperty(Prototype, KEY, function (a, b) {
5052
var collection = getInternalState(this).collection;
5153
if (!IS_ADDER && IS_WEAK && !isObject(a)) return KEY == 'get' ? undefined : false;
5254
var result = collection[KEY](a === 0 ? 0 : a, b);
@@ -55,7 +57,7 @@ module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {
5557
}
5658
});
5759

58-
IS_WEAK || defineProperty(Constructor.prototype, 'size', {
60+
IS_WEAK || defineProperty(Prototype, 'size', {
5961
configurable: true,
6062
get: function () {
6163
return getInternalState(this).collection.size;

packages/core-js-pure/override/internals/export.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var hasOwn = require('../internals/has-own-property');
1212

1313
var wrapConstructor = function (NativeConstructor) {
1414
var Wrapper = function (a, b, c) {
15-
if (this instanceof NativeConstructor) {
15+
if (this instanceof Wrapper) {
1616
switch (arguments.length) {
1717
case 0: return new NativeConstructor();
1818
case 1: return new NativeConstructor(a);

packages/core-js/es/instance/at.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var arrayAt = require('../array/virtual/at');
2-
var stringAt = require('../string/virtual/at');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var arrayMethod = require('../array/virtual/at');
3+
var stringMethod = require('../string/virtual/at');
34

45
var ArrayPrototype = Array.prototype;
56
var StringPrototype = String.prototype;
67

78
module.exports = function (it) {
89
var own = it.at;
9-
if (it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.at)) return arrayAt;
10-
if (typeof it == 'string' || it === StringPrototype || (it instanceof String && own === StringPrototype.at)) {
11-
return stringAt;
10+
if (it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.at)) return arrayMethod;
11+
if (typeof it == 'string' || it === StringPrototype || (isPrototypeOf(StringPrototype, it) && own === StringPrototype.at)) {
12+
return stringMethod;
1213
} return own;
1314
};

packages/core-js/es/instance/bind.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var bind = require('../function/virtual/bind');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../function/virtual/bind');
23

34
var FunctionPrototype = Function.prototype;
45

56
module.exports = function (it) {
67
var own = it.bind;
7-
return it === FunctionPrototype || (it instanceof Function && own === FunctionPrototype.bind) ? bind : own;
8+
return it === FunctionPrototype || (isPrototypeOf(FunctionPrototype, it) && own === FunctionPrototype.bind) ? method : own;
89
};
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var codePointAt = require('../string/virtual/code-point-at');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../string/virtual/code-point-at');
23

34
var StringPrototype = String.prototype;
45

56
module.exports = function (it) {
67
var own = it.codePointAt;
78
return typeof it == 'string' || it === StringPrototype
8-
|| (it instanceof String && own === StringPrototype.codePointAt) ? codePointAt : own;
9+
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.codePointAt) ? method : own;
910
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var concat = require('../array/virtual/concat');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/concat');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.concat;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.concat) ? concat : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.concat) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var copyWithin = require('../array/virtual/copy-within');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/copy-within');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.copyWithin;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.copyWithin) ? copyWithin : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.copyWithin) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var endsWith = require('../string/virtual/ends-with');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../string/virtual/ends-with');
23

34
var StringPrototype = String.prototype;
45

56
module.exports = function (it) {
67
var own = it.endsWith;
78
return typeof it == 'string' || it === StringPrototype
8-
|| (it instanceof String && own === StringPrototype.endsWith) ? endsWith : own;
9+
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.endsWith) ? method : own;
910
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var entries = require('../array/virtual/entries');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/entries');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.entries;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.entries) ? entries : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.entries) ? method : own;
89
};

packages/core-js/es/instance/every.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var every = require('../array/virtual/every');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/every');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.every;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.every) ? every : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.every) ? method : own;
89
};

packages/core-js/es/instance/fill.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var fill = require('../array/virtual/fill');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/fill');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.fill;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.fill) ? fill : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.fill) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var filter = require('../array/virtual/filter');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/filter');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.filter;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.filter) ? filter : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.filter) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var findIndex = require('../array/virtual/find-index');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/find-index');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.findIndex;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.findIndex) ? findIndex : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.findIndex) ? method : own;
89
};

packages/core-js/es/instance/find.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var find = require('../array/virtual/find');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/find');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.find;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.find) ? find : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.find) ? method : own;
89
};

packages/core-js/es/instance/flags.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
12
var flags = require('../regexp/flags');
23

34
var RegExpPrototype = RegExp.prototype;
45

56
module.exports = function (it) {
6-
return (it === RegExpPrototype || it instanceof RegExp) && !('flags' in it) ? flags(it) : it.flags;
7+
return (it === RegExpPrototype || isPrototypeOf(RegExpPrototype, it)) ? flags(it) : it.flags;
78
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var flatMap = require('../array/virtual/flat-map');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/flat-map');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.flatMap;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.flatMap) ? flatMap : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.flatMap) ? method : own;
89
};

packages/core-js/es/instance/flat.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var flat = require('../array/virtual/flat');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/flat');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.flat;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.flat) ? flat : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.flat) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var forEach = require('../array/virtual/for-each');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/for-each');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.forEach;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.forEach) ? forEach : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.forEach) ? method : own;
89
};
+6-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
var arrayIncludes = require('../array/virtual/includes');
2-
var stringIncludes = require('../string/virtual/includes');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var arrayMethod = require('../array/virtual/includes');
3+
var stringMethod = require('../string/virtual/includes');
34

45
var ArrayPrototype = Array.prototype;
56
var StringPrototype = String.prototype;
67

78
module.exports = function (it) {
89
var own = it.includes;
9-
if (it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.includes)) return arrayIncludes;
10-
if (typeof it == 'string' || it === StringPrototype || (it instanceof String && own === StringPrototype.includes)) {
11-
return stringIncludes;
10+
if (it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.includes)) return arrayMethod;
11+
if (typeof it == 'string' || it === StringPrototype || (isPrototypeOf(StringPrototype, it) && own === StringPrototype.includes)) {
12+
return stringMethod;
1213
} return own;
1314
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var indexOf = require('../array/virtual/index-of');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/index-of');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.indexOf;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.indexOf) ? indexOf : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.indexOf) ? method : own;
89
};

packages/core-js/es/instance/keys.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var keys = require('../array/virtual/keys');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/keys');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.keys;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.keys) ? keys : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.keys) ? method : own;
89
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var lastIndexOf = require('../array/virtual/last-index-of');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/last-index-of');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.lastIndexOf;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.lastIndexOf) ? lastIndexOf : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.lastIndexOf) ? method : own;
89
};

packages/core-js/es/instance/map.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var map = require('../array/virtual/map');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/map');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.map;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.map) ? map : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.map) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var matchAll = require('../string/virtual/match-all');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../string/virtual/match-all');
23

34
var StringPrototype = String.prototype;
45

56
module.exports = function (it) {
67
var own = it.matchAll;
78
return typeof it == 'string' || it === StringPrototype
8-
|| (it instanceof String && own === StringPrototype.matchAll) ? matchAll : own;
9+
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.matchAll) ? method : own;
910
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var padEnd = require('../string/virtual/pad-end');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../string/virtual/pad-end');
23

34
var StringPrototype = String.prototype;
45

56
module.exports = function (it) {
67
var own = it.padEnd;
78
return typeof it == 'string' || it === StringPrototype
8-
|| (it instanceof String && own === StringPrototype.padEnd) ? padEnd : own;
9+
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.padEnd) ? method : own;
910
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var padStart = require('../string/virtual/pad-start');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../string/virtual/pad-start');
23

34
var StringPrototype = String.prototype;
45

56
module.exports = function (it) {
67
var own = it.padStart;
78
return typeof it == 'string' || it === StringPrototype
8-
|| (it instanceof String && own === StringPrototype.padStart) ? padStart : own;
9+
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.padStart) ? method : own;
910
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var reduceRight = require('../array/virtual/reduce-right');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/reduce-right');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.reduceRight;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.reduceRight) ? reduceRight : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.reduceRight) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
var reduce = require('../array/virtual/reduce');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../array/virtual/reduce');
23

34
var ArrayPrototype = Array.prototype;
45

56
module.exports = function (it) {
67
var own = it.reduce;
7-
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.reduce) ? reduce : own;
8+
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.reduce) ? method : own;
89
};
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var repeat = require('../string/virtual/repeat');
1+
var isPrototypeOf = require('../../internals/object-is-prototype-of');
2+
var method = require('../string/virtual/repeat');
23

34
var StringPrototype = String.prototype;
45

56
module.exports = function (it) {
67
var own = it.repeat;
78
return typeof it == 'string' || it === StringPrototype
8-
|| (it instanceof String && own === StringPrototype.repeat) ? repeat : own;
9+
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.repeat) ? method : own;
910
};

0 commit comments

Comments
 (0)