Skip to content

Commit

Permalink
avoid instanceof where it's possible since .prototype and `@@hasI…
Browse files Browse the repository at this point in the history
…nstance` can be replaced
  • Loading branch information
zloirock committed Oct 24, 2021
1 parent 1a05cc3 commit 3ef0ded
Show file tree
Hide file tree
Showing 78 changed files with 291 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Expand Up @@ -225,7 +225,7 @@ const base = {
'linebreak-style': [ERROR, 'unix'],
// specify the maximum length of a line in your program
'max-len': [ERROR, {
code: 120,
code: 140,
tabWidth: 2,
ignoreRegExpLiterals: true,
ignoreTemplateLiterals: true,
Expand Down
8 changes: 5 additions & 3 deletions packages/core-js-pure/override/internals/collection.js
Expand Up @@ -34,19 +34,21 @@ module.exports = function (CONSTRUCTOR_NAME, wrapper, common) {
InternalMetadataModule.enable();
} else {
Constructor = wrapper(function (target, iterable) {
setInternalState(anInstance(target, Constructor, CONSTRUCTOR_NAME), {
setInternalState(anInstance(target, Prototype), {
type: CONSTRUCTOR_NAME,
collection: new NativeConstructor()
});
if (iterable != undefined) iterate(iterable, target[ADDER], { that: target, AS_ENTRIES: IS_MAP });
});

var Prototype = Constructor.prototype;

var getInternalState = internalStateGetterFor(CONSTRUCTOR_NAME);

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

IS_WEAK || defineProperty(Constructor.prototype, 'size', {
IS_WEAK || defineProperty(Prototype, 'size', {
configurable: true,
get: function () {
return getInternalState(this).collection.size;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js-pure/override/internals/export.js
Expand Up @@ -12,7 +12,7 @@ var hasOwn = require('../internals/has-own-property');

var wrapConstructor = function (NativeConstructor) {
var Wrapper = function (a, b, c) {
if (this instanceof NativeConstructor) {
if (this instanceof Wrapper) {
switch (arguments.length) {
case 0: return new NativeConstructor();
case 1: return new NativeConstructor(a);
Expand Down
11 changes: 6 additions & 5 deletions packages/core-js/es/instance/at.js
@@ -1,13 +1,14 @@
var arrayAt = require('../array/virtual/at');
var stringAt = require('../string/virtual/at');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var arrayMethod = require('../array/virtual/at');
var stringMethod = require('../string/virtual/at');

var ArrayPrototype = Array.prototype;
var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.at;
if (it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.at)) return arrayAt;
if (typeof it == 'string' || it === StringPrototype || (it instanceof String && own === StringPrototype.at)) {
return stringAt;
if (it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.at)) return arrayMethod;
if (typeof it == 'string' || it === StringPrototype || (isPrototypeOf(StringPrototype, it) && own === StringPrototype.at)) {
return stringMethod;
} return own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/bind.js
@@ -1,8 +1,9 @@
var bind = require('../function/virtual/bind');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../function/virtual/bind');

var FunctionPrototype = Function.prototype;

module.exports = function (it) {
var own = it.bind;
return it === FunctionPrototype || (it instanceof Function && own === FunctionPrototype.bind) ? bind : own;
return it === FunctionPrototype || (isPrototypeOf(FunctionPrototype, it) && own === FunctionPrototype.bind) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/code-point-at.js
@@ -1,9 +1,10 @@
var codePointAt = require('../string/virtual/code-point-at');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../string/virtual/code-point-at');

var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.codePointAt;
return typeof it == 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.codePointAt) ? codePointAt : own;
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.codePointAt) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/concat.js
@@ -1,8 +1,9 @@
var concat = require('../array/virtual/concat');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/concat');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.concat;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.concat) ? concat : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.concat) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/copy-within.js
@@ -1,8 +1,9 @@
var copyWithin = require('../array/virtual/copy-within');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/copy-within');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.copyWithin;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.copyWithin) ? copyWithin : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.copyWithin) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/ends-with.js
@@ -1,9 +1,10 @@
var endsWith = require('../string/virtual/ends-with');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../string/virtual/ends-with');

var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.endsWith;
return typeof it == 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.endsWith) ? endsWith : own;
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.endsWith) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/entries.js
@@ -1,8 +1,9 @@
var entries = require('../array/virtual/entries');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/entries');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.entries;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.entries) ? entries : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.entries) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/every.js
@@ -1,8 +1,9 @@
var every = require('../array/virtual/every');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/every');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.every;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.every) ? every : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.every) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/fill.js
@@ -1,8 +1,9 @@
var fill = require('../array/virtual/fill');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/fill');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.fill;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.fill) ? fill : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.fill) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/filter.js
@@ -1,8 +1,9 @@
var filter = require('../array/virtual/filter');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/filter');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.filter;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.filter) ? filter : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.filter) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/find-index.js
@@ -1,8 +1,9 @@
var findIndex = require('../array/virtual/find-index');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/find-index');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.findIndex;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.findIndex) ? findIndex : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.findIndex) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/find.js
@@ -1,8 +1,9 @@
var find = require('../array/virtual/find');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/find');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.find;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.find) ? find : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.find) ? method : own;
};
3 changes: 2 additions & 1 deletion packages/core-js/es/instance/flags.js
@@ -1,7 +1,8 @@
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var flags = require('../regexp/flags');

var RegExpPrototype = RegExp.prototype;

module.exports = function (it) {
return (it === RegExpPrototype || it instanceof RegExp) && !('flags' in it) ? flags(it) : it.flags;
return (it === RegExpPrototype || isPrototypeOf(RegExpPrototype, it)) ? flags(it) : it.flags;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/flat-map.js
@@ -1,8 +1,9 @@
var flatMap = require('../array/virtual/flat-map');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/flat-map');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.flatMap;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.flatMap) ? flatMap : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.flatMap) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/flat.js
@@ -1,8 +1,9 @@
var flat = require('../array/virtual/flat');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/flat');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.flat;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.flat) ? flat : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.flat) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/for-each.js
@@ -1,8 +1,9 @@
var forEach = require('../array/virtual/for-each');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/for-each');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.forEach;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.forEach) ? forEach : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.forEach) ? method : own;
};
11 changes: 6 additions & 5 deletions packages/core-js/es/instance/includes.js
@@ -1,13 +1,14 @@
var arrayIncludes = require('../array/virtual/includes');
var stringIncludes = require('../string/virtual/includes');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var arrayMethod = require('../array/virtual/includes');
var stringMethod = require('../string/virtual/includes');

var ArrayPrototype = Array.prototype;
var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.includes;
if (it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.includes)) return arrayIncludes;
if (typeof it == 'string' || it === StringPrototype || (it instanceof String && own === StringPrototype.includes)) {
return stringIncludes;
if (it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.includes)) return arrayMethod;
if (typeof it == 'string' || it === StringPrototype || (isPrototypeOf(StringPrototype, it) && own === StringPrototype.includes)) {
return stringMethod;
} return own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/index-of.js
@@ -1,8 +1,9 @@
var indexOf = require('../array/virtual/index-of');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/index-of');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.indexOf;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.indexOf) ? indexOf : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.indexOf) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/keys.js
@@ -1,8 +1,9 @@
var keys = require('../array/virtual/keys');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/keys');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.keys;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.keys) ? keys : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.keys) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/last-index-of.js
@@ -1,8 +1,9 @@
var lastIndexOf = require('../array/virtual/last-index-of');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/last-index-of');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.lastIndexOf;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.lastIndexOf) ? lastIndexOf : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.lastIndexOf) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/map.js
@@ -1,8 +1,9 @@
var map = require('../array/virtual/map');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/map');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.map;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.map) ? map : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.map) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/match-all.js
@@ -1,9 +1,10 @@
var matchAll = require('../string/virtual/match-all');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../string/virtual/match-all');

var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.matchAll;
return typeof it == 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.matchAll) ? matchAll : own;
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.matchAll) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/pad-end.js
@@ -1,9 +1,10 @@
var padEnd = require('../string/virtual/pad-end');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../string/virtual/pad-end');

var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.padEnd;
return typeof it == 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.padEnd) ? padEnd : own;
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.padEnd) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/pad-start.js
@@ -1,9 +1,10 @@
var padStart = require('../string/virtual/pad-start');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../string/virtual/pad-start');

var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.padStart;
return typeof it == 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.padStart) ? padStart : own;
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.padStart) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/reduce-right.js
@@ -1,8 +1,9 @@
var reduceRight = require('../array/virtual/reduce-right');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/reduce-right');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.reduceRight;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.reduceRight) ? reduceRight : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.reduceRight) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/reduce.js
@@ -1,8 +1,9 @@
var reduce = require('../array/virtual/reduce');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../array/virtual/reduce');

var ArrayPrototype = Array.prototype;

module.exports = function (it) {
var own = it.reduce;
return it === ArrayPrototype || (it instanceof Array && own === ArrayPrototype.reduce) ? reduce : own;
return it === ArrayPrototype || (isPrototypeOf(ArrayPrototype, it) && own === ArrayPrototype.reduce) ? method : own;
};
5 changes: 3 additions & 2 deletions packages/core-js/es/instance/repeat.js
@@ -1,9 +1,10 @@
var repeat = require('../string/virtual/repeat');
var isPrototypeOf = require('../../internals/object-is-prototype-of');
var method = require('../string/virtual/repeat');

var StringPrototype = String.prototype;

module.exports = function (it) {
var own = it.repeat;
return typeof it == 'string' || it === StringPrototype
|| (it instanceof String && own === StringPrototype.repeat) ? repeat : own;
|| (isPrototypeOf(StringPrototype, it) && own === StringPrototype.repeat) ? method : own;
};

0 comments on commit 3ef0ded

Please sign in to comment.