Skip to content

Commit 593f257

Browse files
committed
encapsulate some more built-ins, reuse array-unique-by in reflect.get-metadata-keys
1 parent a37a799 commit 593f257

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

packages/core-js/internals/array-unique-by.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ var lengthOfArrayLike = require('../internals/length-of-array-like');
66
var toObject = require('../internals/to-object');
77
var arraySpeciesCreate = require('../internals/array-species-create');
88

9+
var Map = getBuiltIn('Map');
10+
var MapPrototype = Map.prototype;
11+
var mapForEach = uncurryThis(MapPrototype.forEach);
12+
var mapHas = uncurryThis(MapPrototype.has);
13+
var mapSet = uncurryThis(MapPrototype.set);
914
var push = uncurryThis([].push);
1015

1116
// `Array.prototype.uniqueBy` method
@@ -14,7 +19,6 @@ module.exports = function uniqueBy(resolver) {
1419
var that = toObject(this);
1520
var length = lengthOfArrayLike(that);
1621
var result = arraySpeciesCreate(that, 0);
17-
var Map = getBuiltIn('Map');
1822
var map = new Map();
1923
var resolverFunction, index, item, key;
2024
if (resolver != null) resolverFunction = aCallable(resolver);
@@ -24,9 +28,9 @@ module.exports = function uniqueBy(resolver) {
2428
for (index = 0; index < length; index++) {
2529
item = that[index];
2630
key = resolverFunction(item);
27-
if (!map.has(key)) map.set(key, item);
31+
if (!mapHas(map, key)) mapSet(map, key, item);
2832
}
29-
map.forEach(function (value) {
33+
mapForEach(map, function (value) {
3034
push(result, value);
3135
});
3236
return result;

packages/core-js/internals/reflect-metadata.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
require('../modules/es.map');
33
require('../modules/es.weak-map');
44
var getBuiltIn = require('../internals/get-built-in');
5+
var uncurryThis = require('../internals/function-uncurry-this');
56
var shared = require('../internals/shared');
67

78
var Map = getBuiltIn('Map');
89
var WeakMap = getBuiltIn('WeakMap');
10+
var push = uncurryThis([].push);
11+
912
var metadata = shared('metadata');
1013
var store = metadata.store || (metadata.store = new WeakMap());
1114

@@ -39,7 +42,7 @@ var ordinaryDefineOwnMetadata = function (MetadataKey, MetadataValue, O, P) {
3942
var ordinaryOwnMetadataKeys = function (target, targetKey) {
4043
var metadataMap = getOrCreateMetadataMap(target, targetKey, false);
4144
var keys = [];
42-
if (metadataMap) metadataMap.forEach(function (_, key) { keys.push(key); });
45+
if (metadataMap) metadataMap.forEach(function (_, key) { push(keys, key); });
4346
return keys;
4447
};
4548

packages/core-js/modules/esnext.reflect.get-metadata-keys.js

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
// TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
2-
require('../modules/es.set');
31
var $ = require('../internals/export');
4-
var getBuiltIn = require('../internals/get-built-in');
2+
var uncurryThis = require('../internals/function-uncurry-this');
53
var ReflectMetadataModule = require('../internals/reflect-metadata');
64
var anObject = require('../internals/an-object');
75
var getPrototypeOf = require('../internals/object-get-prototype-of');
8-
var iterate = require('../internals/iterate');
6+
var $arrayUniqueBy = require('../internals/array-unique-by');
97

10-
var Set = getBuiltIn('Set');
8+
var arrayUniqueBy = uncurryThis($arrayUniqueBy);
9+
var concat = uncurryThis([].concat);
1110
var ordinaryOwnMetadataKeys = ReflectMetadataModule.keys;
1211
var toMetadataKey = ReflectMetadataModule.toKey;
1312

14-
var from = function (iter) {
15-
var result = [];
16-
iterate(iter, result.push, { that: result });
17-
return result;
18-
};
19-
2013
var ordinaryMetadataKeys = function (O, P) {
2114
var oKeys = ordinaryOwnMetadataKeys(O, P);
2215
var parent = getPrototypeOf(O);
2316
if (parent === null) return oKeys;
2417
var pKeys = ordinaryMetadataKeys(parent, P);
25-
return pKeys.length ? oKeys.length ? from(new Set(oKeys.concat(pKeys))) : pKeys : oKeys;
18+
return pKeys.length ? oKeys.length ? arrayUniqueBy(concat(oKeys, pKeys)) : pKeys : oKeys;
2619
};
2720

2821
// `Reflect.getMetadataKeys` method

0 commit comments

Comments
 (0)