Skip to content

Commit

Permalink
encapsulate some more built-ins
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 23, 2021
1 parent 476fd3b commit 54aba11
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 58 deletions.
3 changes: 2 additions & 1 deletion packages/core-js/internals/collection-add-all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');

Expand All @@ -7,7 +8,7 @@ module.exports = function addAll(/* ...elements */) {
var set = anObject(this);
var adder = aCallable(set.add);
for (var k = 0, len = arguments.length; k < len; k++) {
adder.call(set, arguments[k]);
call(adder, set, arguments[k]);
}
return set;
};
3 changes: 2 additions & 1 deletion packages/core-js/internals/collection-delete-all.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');

Expand All @@ -9,7 +10,7 @@ module.exports = function deleteAll(/* ...elements */) {
var allDeleted = true;
var wasDeleted;
for (var k = 0, len = arguments.length; k < len; k++) {
wasDeleted = remover.call(collection, arguments[k]);
wasDeleted = call(remover, collection, arguments[k]);
allDeleted = allDeleted && wasDeleted;
}
return !!allDeleted;
Expand Down
10 changes: 5 additions & 5 deletions packages/core-js/internals/collection-from.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';
// https://tc39.github.io/proposal-setmap-offrom/
var uncurryThis = require('../internals/function-uncurry-this');
var bind = require('../internals/function-bind-context');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var aConstructor = require('../internals/a-constructor');
var bind = require('../internals/function-bind-context');
var iterate = require('../internals/iterate');

var push = uncurryThis([].push);
var push = [].push;

module.exports = function from(source /* , mapFn, thisArg */) {
var length = arguments.length;
Expand All @@ -21,10 +21,10 @@ module.exports = function from(source /* , mapFn, thisArg */) {
n = 0;
boundFunction = bind(mapFn, length > 2 ? arguments[2] : undefined);
iterate(source, function (nextItem) {
push(array, boundFunction(nextItem, n++));
call(push, array, boundFunction(nextItem, n++));
});
} else {
iterate(source, array.push, { that: array });
iterate(source, push, { that: array });
}
return new this(array);
};
9 changes: 2 additions & 7 deletions packages/core-js/internals/collection-of.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
'use strict';
var global = require('../internals/global');

var Array = global.Array;
var arraySlice = require('../internals/array-slice');

// https://tc39.github.io/proposal-setmap-offrom/
module.exports = function of() {
var length = arguments.length;
var A = Array(length);
while (length--) A[length] = arguments[length];
return new this(A);
return new this(arraySlice(arguments));
};
11 changes: 8 additions & 3 deletions packages/core-js/internals/map-emplace.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');

// `Map.prototype.emplace` method
// https://github.com/thumbsupep/proposal-upsert
module.exports = function emplace(key, handler) {
var map = anObject(this);
var value = (map.has(key) && 'update' in handler)
? handler.update(map.get(key), key, map)
var get = aCallable(map.get);
var has = aCallable(map.has);
var set = aCallable(map.set);
var value = (call(has, map, key) && 'update' in handler)
? handler.update(call(get, map, key), key, map)
: handler.insert(key, map);
map.set(key, value);
call(set, map, key, value);
return value;
};
13 changes: 9 additions & 4 deletions packages/core-js/internals/map-upsert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';
var global = require('../internals/global');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var isCallable = require('../internals/is-callable');
var anObject = require('../internals/an-object');

Expand All @@ -9,19 +11,22 @@ var TypeError = global.TypeError;
// https://github.com/thumbsupep/proposal-upsert
module.exports = function upsert(key, updateFn /* , insertFn */) {
var map = anObject(this);
var get = aCallable(map.get);
var has = aCallable(map.has);
var set = aCallable(map.set);
var insertFn = arguments.length > 2 ? arguments[2] : undefined;
var value;
if (!isCallable(updateFn) && !isCallable(insertFn)) {
throw TypeError('At least one callback required');
}
if (map.has(key)) {
value = map.get(key);
if (call(has, map, key)) {
value = call(get, map, key);
if (isCallable(updateFn)) {
value = updateFn(value);
map.set(key, value);
call(set, map, key, value);
}
} else if (isCallable(insertFn)) {
value = insertFn();
map.set(key, value);
call(set, map, key, value);
} return value;
};
7 changes: 4 additions & 3 deletions packages/core-js/modules/esnext.map.filter.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var bind = require('../internals/function-bind-context');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var bind = require('../internals/function-bind-context');
var speciesConstructor = require('../internals/species-constructor');
var getMapIterator = require('../internals/get-map-iterator');
var iterate = require('../internals/iterate');
Expand All @@ -19,7 +20,7 @@ $({ target: 'Map', proto: true, real: true, forced: IS_PURE }, {
var newMap = new (speciesConstructor(map, getBuiltIn('Map')))();
var setter = aCallable(newMap.set);
iterate(iterator, function (key, value) {
if (boundFunction(value, key, map)) setter.call(newMap, key, value);
if (boundFunction(value, key, map)) call(setter, newMap, key, value);
}, { AS_ENTRIES: true, IS_ITERATOR: true });
return newMap;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/core-js/modules/esnext.map.group-by.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict';
var $ = require('../internals/export');
var call = require('../internals/function-call');
var uncurryThis = require('../internals/function-uncurry-this');
var aCallable = require('../internals/a-callable');
var getIterator = require('../internals/get-iterator');
var iterate = require('../internals/iterate');

var push = uncurryThis([].push);

// `Map.groupBy` method
// https://github.com/tc39/proposal-collection-methods
$({ target: 'Map', stat: true }, {
Expand All @@ -16,8 +20,8 @@ $({ target: 'Map', stat: true }, {
var set = aCallable(newMap.set);
iterate(iterator, function (element) {
var derivedKey = keyDerivative(element);
if (!has.call(newMap, derivedKey)) set.call(newMap, derivedKey, [element]);
else get.call(newMap, derivedKey).push(element);
if (!call(has, newMap, derivedKey)) call(set, newMap, derivedKey, [element]);
else push(call(get, newMap, derivedKey), element);
}, { IS_ITERATOR: true });
return newMap;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.map.includes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var anObject = require('../internals/an-object');
var getMapIterator = require('../internals/get-map-iterator');
var sameValueZero = require('../internals/same-value-zero');
Expand Down
3 changes: 2 additions & 1 deletion packages/core-js/modules/esnext.map.key-by.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var $ = require('../internals/export');
var call = require('../internals/function-call');
var iterate = require('../internals/iterate');
var aCallable = require('../internals/a-callable');

Expand All @@ -11,7 +12,7 @@ $({ target: 'Map', stat: true }, {
aCallable(keyDerivative);
var setter = aCallable(newMap.set);
iterate(iterable, function (element) {
setter.call(newMap, keyDerivative(element), element);
call(setter, newMap, keyDerivative(element), element);
});
return newMap;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/core-js/modules/esnext.map.map-keys.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var bind = require('../internals/function-bind-context');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var bind = require('../internals/function-bind-context');
var speciesConstructor = require('../internals/species-constructor');
var getMapIterator = require('../internals/get-map-iterator');
var iterate = require('../internals/iterate');
Expand All @@ -19,7 +20,7 @@ $({ target: 'Map', proto: true, real: true, forced: IS_PURE }, {
var newMap = new (speciesConstructor(map, getBuiltIn('Map')))();
var setter = aCallable(newMap.set);
iterate(iterator, function (key, value) {
setter.call(newMap, boundFunction(value, key, map), value);
call(setter, newMap, boundFunction(value, key, map), value);
}, { AS_ENTRIES: true, IS_ITERATOR: true });
return newMap;
}
Expand Down
7 changes: 4 additions & 3 deletions packages/core-js/modules/esnext.map.map-values.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var bind = require('../internals/function-bind-context');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var bind = require('../internals/function-bind-context');
var speciesConstructor = require('../internals/species-constructor');
var getMapIterator = require('../internals/get-map-iterator');
var iterate = require('../internals/iterate');
Expand All @@ -19,7 +20,7 @@ $({ target: 'Map', proto: true, real: true, forced: IS_PURE }, {
var newMap = new (speciesConstructor(map, getBuiltIn('Map')))();
var setter = aCallable(newMap.set);
iterate(iterator, function (key, value) {
setter.call(newMap, key, boundFunction(value, key, map));
call(setter, newMap, key, boundFunction(value, key, map));
}, { AS_ENTRIES: true, IS_ITERATOR: true });
return newMap;
}
Expand Down
12 changes: 8 additions & 4 deletions packages/core-js/modules/esnext.map.update.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var global = require('../internals/global');
var IS_PURE = require('../internals/is-pure');
var call = require('../internals/function-call');
var anObject = require('../internals/an-object');
var aCallable = require('../internals/a-callable');

Expand All @@ -12,14 +13,17 @@ var TypeError = global.TypeError;
$({ target: 'Map', proto: true, real: true, forced: IS_PURE }, {
update: function update(key, callback /* , thunk */) {
var map = anObject(this);
var get = aCallable(map.get);
var has = aCallable(map.has);
var set = aCallable(map.set);
var length = arguments.length;
aCallable(callback);
var isPresentInMap = map.has(key);
var isPresentInMap = call(has, map, key);
if (!isPresentInMap && length < 3) {
throw TypeError('Updating absent value');
}
var value = isPresentInMap ? map.get(key) : aCallable(length > 2 ? arguments[2] : undefined)(key, map);
map.set(key, callback(value, key, map));
var value = isPresentInMap ? call(get, map, key) : aCallable(length > 2 ? arguments[2] : undefined)(key, map);
call(set, map, key, callback(value, key, map));
return map;
}
});
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.set.difference.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var speciesConstructor = require('../internals/species-constructor');
Expand All @@ -15,7 +16,7 @@ $({ target: 'Set', proto: true, real: true, forced: IS_PURE }, {
var newSet = new (speciesConstructor(set, getBuiltIn('Set')))(set);
var remover = aCallable(newSet['delete']);
iterate(iterable, function (value) {
remover.call(newSet, value);
call(remover, newSet, value);
});
return newSet;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.set.filter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var bind = require('../internals/function-bind-context');
Expand All @@ -19,7 +20,7 @@ $({ target: 'Set', proto: true, real: true, forced: IS_PURE }, {
var newSet = new (speciesConstructor(set, getBuiltIn('Set')))();
var adder = aCallable(newSet.add);
iterate(iterator, function (value) {
if (boundFunction(value, value, set)) adder.call(newSet, value);
if (boundFunction(value, value, set)) call(adder, newSet, value);
}, { IS_ITERATOR: true });
return newSet;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.set.intersection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var speciesConstructor = require('../internals/species-constructor');
Expand All @@ -16,7 +17,7 @@ $({ target: 'Set', proto: true, real: true, forced: IS_PURE }, {
var hasCheck = aCallable(set.has);
var adder = aCallable(newSet.add);
iterate(iterable, function (value) {
if (hasCheck.call(set, value)) adder.call(newSet, value);
if (call(hasCheck, set, value)) call(adder, newSet, value);
});
return newSet;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.set.is-disjoint-from.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var iterate = require('../internals/iterate');
Expand All @@ -12,7 +13,7 @@ $({ target: 'Set', proto: true, real: true, forced: IS_PURE }, {
var set = anObject(this);
var hasCheck = aCallable(set.has);
return !iterate(iterable, function (value, stop) {
if (hasCheck.call(set, value) === true) return stop();
if (call(hasCheck, set, value) === true) return stop();
}, { INTERRUPTED: true }).stopped;
}
});
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.set.is-subset-of.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var getBuiltIn = require('../internals/get-built-in');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var isCallable = require('../internals/is-callable');
var anObject = require('../internals/an-object');
Expand All @@ -20,7 +21,7 @@ $({ target: 'Set', proto: true, real: true, forced: IS_PURE }, {
hasCheck = aCallable(otherSet.has);
}
return !iterate(iterator, function (value, stop) {
if (hasCheck.call(otherSet, value) === false) return stop();
if (call(hasCheck, otherSet, value) === false) return stop();
}, { IS_ITERATOR: true, INTERRUPTED: true }).stopped;
}
});
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.set.is-superset-of.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var $ = require('../internals/export');
var IS_PURE = require('../internals/is-pure');
var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var iterate = require('../internals/iterate');
Expand All @@ -12,7 +13,7 @@ $({ target: 'Set', proto: true, real: true, forced: IS_PURE }, {
var set = anObject(this);
var hasCheck = aCallable(set.has);
return !iterate(iterable, function (value, stop) {
if (hasCheck.call(set, value) === false) return stop();
if (call(hasCheck, set, value) === false) return stop();
}, { INTERRUPTED: true }).stopped;
}
});
Loading

0 comments on commit 54aba11

Please sign in to comment.