Skip to content

Commit

Permalink
encapsulate some more built-ins, reuse array-unique-by in `reflect.…
Browse files Browse the repository at this point in the history
…get-metadata-keys`
  • Loading branch information
zloirock committed Oct 23, 2021
1 parent a37a799 commit 593f257
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
10 changes: 7 additions & 3 deletions packages/core-js/internals/array-unique-by.js
Expand Up @@ -6,6 +6,11 @@ var lengthOfArrayLike = require('../internals/length-of-array-like');
var toObject = require('../internals/to-object');
var arraySpeciesCreate = require('../internals/array-species-create');

var Map = getBuiltIn('Map');
var MapPrototype = Map.prototype;
var mapForEach = uncurryThis(MapPrototype.forEach);
var mapHas = uncurryThis(MapPrototype.has);
var mapSet = uncurryThis(MapPrototype.set);
var push = uncurryThis([].push);

// `Array.prototype.uniqueBy` method
Expand All @@ -14,7 +19,6 @@ module.exports = function uniqueBy(resolver) {
var that = toObject(this);
var length = lengthOfArrayLike(that);
var result = arraySpeciesCreate(that, 0);
var Map = getBuiltIn('Map');
var map = new Map();
var resolverFunction, index, item, key;
if (resolver != null) resolverFunction = aCallable(resolver);
Expand All @@ -24,9 +28,9 @@ module.exports = function uniqueBy(resolver) {
for (index = 0; index < length; index++) {
item = that[index];
key = resolverFunction(item);
if (!map.has(key)) map.set(key, item);
if (!mapHas(map, key)) mapSet(map, key, item);
}
map.forEach(function (value) {
mapForEach(map, function (value) {
push(result, value);
});
return result;
Expand Down
5 changes: 4 additions & 1 deletion packages/core-js/internals/reflect-metadata.js
Expand Up @@ -2,10 +2,13 @@
require('../modules/es.map');
require('../modules/es.weak-map');
var getBuiltIn = require('../internals/get-built-in');
var uncurryThis = require('../internals/function-uncurry-this');
var shared = require('../internals/shared');

var Map = getBuiltIn('Map');
var WeakMap = getBuiltIn('WeakMap');
var push = uncurryThis([].push);

var metadata = shared('metadata');
var store = metadata.store || (metadata.store = new WeakMap());

Expand Down Expand Up @@ -39,7 +42,7 @@ var ordinaryDefineOwnMetadata = function (MetadataKey, MetadataValue, O, P) {
var ordinaryOwnMetadataKeys = function (target, targetKey) {
var metadataMap = getOrCreateMetadataMap(target, targetKey, false);
var keys = [];
if (metadataMap) metadataMap.forEach(function (_, key) { keys.push(key); });
if (metadataMap) metadataMap.forEach(function (_, key) { push(keys, key); });
return keys;
};

Expand Down
17 changes: 5 additions & 12 deletions packages/core-js/modules/esnext.reflect.get-metadata-keys.js
@@ -1,28 +1,21 @@
// TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
require('../modules/es.set');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var uncurryThis = require('../internals/function-uncurry-this');
var ReflectMetadataModule = require('../internals/reflect-metadata');
var anObject = require('../internals/an-object');
var getPrototypeOf = require('../internals/object-get-prototype-of');
var iterate = require('../internals/iterate');
var $arrayUniqueBy = require('../internals/array-unique-by');

var Set = getBuiltIn('Set');
var arrayUniqueBy = uncurryThis($arrayUniqueBy);
var concat = uncurryThis([].concat);
var ordinaryOwnMetadataKeys = ReflectMetadataModule.keys;
var toMetadataKey = ReflectMetadataModule.toKey;

var from = function (iter) {
var result = [];
iterate(iter, result.push, { that: result });
return result;
};

var ordinaryMetadataKeys = function (O, P) {
var oKeys = ordinaryOwnMetadataKeys(O, P);
var parent = getPrototypeOf(O);
if (parent === null) return oKeys;
var pKeys = ordinaryMetadataKeys(parent, P);
return pKeys.length ? oKeys.length ? from(new Set(oKeys.concat(pKeys))) : pKeys : oKeys;
return pKeys.length ? oKeys.length ? arrayUniqueBy(concat(oKeys, pKeys)) : pKeys : oKeys;
};

// `Reflect.getMetadataKeys` method
Expand Down

0 comments on commit 593f257

Please sign in to comment.