From e3e9f3718c371094e65e359d901413441eb334b0 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Sun, 27 Oct 2024 17:05:46 +0700 Subject: [PATCH] update `Map` upsert proposal to the new API https://github.com/tc39/proposal-upsert/pull/58 --- CHANGELOG.md | 7 ++++ README.md | 34 ++++++++++++------- packages/core-js-compat/src/data.mjs | 10 ++++++ .../src/modules-by-versions.mjs | 4 +++ packages/core-js/full/map/from.js | 2 ++ .../full/map/get-or-insert-computed.js | 6 ++++ packages/core-js/full/map/get-or-insert.js | 6 ++++ packages/core-js/full/map/group-by.js | 2 ++ packages/core-js/full/map/index.js | 4 ++- packages/core-js/full/map/key-by.js | 2 ++ packages/core-js/full/map/of.js | 2 ++ packages/core-js/full/weak-map/from.js | 2 ++ .../full/weak-map/get-or-insert-computed.js | 6 ++++ .../core-js/full/weak-map/get-or-insert.js | 6 ++++ packages/core-js/full/weak-map/index.js | 4 ++- packages/core-js/full/weak-map/of.js | 2 ++ .../esnext.map.get-or-insert-computed.js | 22 ++++++++++++ .../modules/esnext.map.get-or-insert.js | 18 ++++++++++ .../esnext.weak-map.get-or-insert-computed.js | 22 ++++++++++++ .../modules/esnext.weak-map.get-or-insert.js | 18 ++++++++++ packages/core-js/proposals/map-upsert-v4.js | 6 ++++ packages/core-js/stage/2.js | 3 +- tests/compat-data/tests-coverage.mjs | 2 ++ tests/compat/tests.js | 16 ++++++--- tests/entries/unit.mjs | 5 +++ 25 files changed, 190 insertions(+), 21 deletions(-) create mode 100644 packages/core-js/full/map/get-or-insert-computed.js create mode 100644 packages/core-js/full/map/get-or-insert.js create mode 100644 packages/core-js/full/weak-map/get-or-insert-computed.js create mode 100644 packages/core-js/full/weak-map/get-or-insert.js create mode 100644 packages/core-js/modules/esnext.map.get-or-insert-computed.js create mode 100644 packages/core-js/modules/esnext.map.get-or-insert.js create mode 100644 packages/core-js/modules/esnext.weak-map.get-or-insert-computed.js create mode 100644 packages/core-js/modules/esnext.weak-map.get-or-insert.js create mode 100644 packages/core-js/proposals/map-upsert-v4.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 5679ed946614..e134fe98bbc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,13 @@ - `Math.sumPrecise` - Moved to stage 3, [October 2024 TC39 meeting](https://x.com/robpalmer2/status/1843829675036160179) - Added `/actual/` namespace entries, unconditional forced replacement changed to feature detection +- [`Map` upsert stage 2 proposal](https://github.com/tc39/proposal-upsert): + - [Updated to the new API following the October 2024 TC39 meeting](https://github.com/tc39/proposal-upsert/pull/58) + - Added built-ins: + - `Map.prototype.getOrInsert` + - `Map.prototype.getOrInsertComputed` + - `WeakMap.prototype.getOrInsert` + - `WeakMap.prototype.getOrInsertComputed` - [Extractors proposal](https://github.com/tc39/proposal-extractors) moved to stage 2, [October 2024 TC39 meeting](https://github.com/tc39/proposals/commit/11bc489049fc5ce59b21e98a670a84f153a29a80) - Usage of `@@species` pattern removed from `%TypedArray%` and `ArrayBuffer` methods, [tc39/ecma262/3450](https://github.com/tc39/ecma262/pull/3450): - Built-ins: diff --git a/README.md b/README.md index af47f0518017..33214c6efdc7 100644 --- a/README.md +++ b/README.md @@ -171,7 +171,7 @@ structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3]) - [Stage 2 proposals](#stage-2-proposals) - [`AsyncIterator` helpers](#asynciterator-helpers) - [`Iterator.range`](#iteratorrange) - - [`Map.prototype.emplace`](#mapprototypeemplace) + - [`Map` upsert](#map-upsert) - [`Array.isTemplateObject`](#arrayistemplateobject) - [`String.dedent`](#stringdedent) - [`Symbol` predicates](#symbol-predicates) @@ -2755,32 +2755,40 @@ for (const i of Iterator.range(1, 10, { step: 3, inclusive: true })) { console.log(i); // => 1, 4, 7, 10 } ``` -##### [`Map.prototype.emplace`](https://github.com/thumbsupep/proposal-upsert)[⬆](#index) -Modules [`esnext.map.emplace`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.map.emplace.js) and [`esnext.weak-map.emplace`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.weak-map.emplace.js) +##### [`Map` upsert](https://github.com/thumbsupep/proposal-upsert)[⬆](#index) +Modules [`esnext.map.get-or-insert`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.map.get-or-insert.js), [`esnext.map.get-or-insert-computed`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.map.get-or-insert-computed.js), [`esnext.weak-map.get-or-insert`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.weak-map.get-or-insert.js) and [`esnext.weak-map.get-or-insert-computed`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.weak-map.get-or-insert-computed.js) ```ts class Map { - emplace(key: any, { update: (value: any, key: any, handler: object) => updated: any, insert: (key: any, handler: object) => value: any): updated | value; + getOrInsert(key: any, value: any): any; + getOrInsertComputed(key: any, (key: any) => value: any): any; } class WeakMap { - emplace(key: any, { update: (value: any, key: any, handler: object) => updated: any, insert: (key: any, handler: object) => value: any): updated | value; + getOrInsert(key: any, value: any): any; + getOrInsertComputed(key: any, (key: any) => value: any): any; } ``` [*CommonJS entry points:*](#commonjs-api) ``` -core-js/proposals/map-upsert-stage-2 -core-js(-pure)/full/map/emplace -core-js(-pure)/full/weak-map/emplace +core-js/proposals/map-upsert-v4 +core-js(-pure)/full/map/get-or-insert +core-js(-pure)/full/map/get-or-insert-computed +core-js(-pure)/full/weak-map/get-or-insert +core-js(-pure)/full/weak-map/get-or-insert-computed ``` -[*Examples*](https://is.gd/ty5I2v): +[*Examples*](https://tinyurl.com/2a54u5ux): ```js -const map = new Map([['a', 2]]); +const map = new Map([['a', 1]]); -map.emplace('a', { update: it => it ** 2, insert: () => 3 }); // => 4 +map.getOrInsert('a', 2); // => 1 -map.emplace('b', { update: it => it ** 2, insert: () => 3 }); // => 3 +map.getOrInsert('b', 3); // => 3 -console.log(map); // => Map { 'a': 4, 'b': 3 } +map.getOrInsertComputed('a', key => key); // => 1 + +map.getOrInsertComputed('c', key => key); // => 'c' + +console.log(map); // => Map { 'a': 1, 'b': 3, 'c': 'c' } ``` ##### [`Array.isTemplateObject`](https://github.com/tc39/proposal-array-is-template-object)[⬆](#index) Module [`esnext.array.is-template-object`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/esnext.array.is-template-object.js) diff --git a/packages/core-js-compat/src/data.mjs b/packages/core-js-compat/src/data.mjs index 784a8a91c536..215135bc3a96 100644 --- a/packages/core-js-compat/src/data.mjs +++ b/packages/core-js-compat/src/data.mjs @@ -2298,6 +2298,7 @@ export const data = { }, 'esnext.map.delete-all': { }, + // TODO: Remove from `core-js@4` 'esnext.map.emplace': { }, 'esnext.map.every': { @@ -2310,6 +2311,10 @@ export const data = { }, 'esnext.map.from': { }, + 'esnext.map.get-or-insert': { + }, + 'esnext.map.get-or-insert-computed': { + }, // TODO: Remove from `core-js@4` 'esnext.map.group-by': null, 'esnext.map.includes': { @@ -2619,8 +2624,13 @@ export const data = { }, 'esnext.weak-map.of': { }, + // TODO: Remove from `core-js@4` 'esnext.weak-map.emplace': { }, + 'esnext.weak-map.get-or-insert': { + }, + 'esnext.weak-map.get-or-insert-computed': { + }, // TODO: Remove from `core-js@4` 'esnext.weak-map.upsert': { }, diff --git a/packages/core-js-compat/src/modules-by-versions.mjs b/packages/core-js-compat/src/modules-by-versions.mjs index 3db1c15d839e..b552a7a224cd 100644 --- a/packages/core-js-compat/src/modules-by-versions.mjs +++ b/packages/core-js-compat/src/modules-by-versions.mjs @@ -268,5 +268,9 @@ export default { 'es.iterator.take', 'es.iterator.to-array', 'es.promise.try', + 'esnext.map.get-or-insert', + 'esnext.map.get-or-insert-computed', + 'esnext.weak-map.get-or-insert', + 'esnext.weak-map.get-or-insert-computed', ], }; diff --git a/packages/core-js/full/map/from.js b/packages/core-js/full/map/from.js index 649c002db5c5..f19d8fddebbc 100644 --- a/packages/core-js/full/map/from.js +++ b/packages/core-js/full/map/from.js @@ -10,6 +10,8 @@ require('../../modules/esnext.map.every'); require('../../modules/esnext.map.filter'); require('../../modules/esnext.map.find'); require('../../modules/esnext.map.find-key'); +require('../../modules/esnext.map.get-or-insert'); +require('../../modules/esnext.map.get-or-insert-computed'); require('../../modules/esnext.map.includes'); require('../../modules/esnext.map.key-of'); require('../../modules/esnext.map.map-keys'); diff --git a/packages/core-js/full/map/get-or-insert-computed.js b/packages/core-js/full/map/get-or-insert-computed.js new file mode 100644 index 000000000000..82f01b45f7fe --- /dev/null +++ b/packages/core-js/full/map/get-or-insert-computed.js @@ -0,0 +1,6 @@ +'use strict'; +require('../../modules/es.map'); +require('../../modules/esnext.map.get-or-insert-computed'); +var entryUnbind = require('../../internals/entry-unbind'); + +module.exports = entryUnbind('Map', 'getOrInsertComputed'); diff --git a/packages/core-js/full/map/get-or-insert.js b/packages/core-js/full/map/get-or-insert.js new file mode 100644 index 000000000000..a526ad602431 --- /dev/null +++ b/packages/core-js/full/map/get-or-insert.js @@ -0,0 +1,6 @@ +'use strict'; +require('../../modules/es.map'); +require('../../modules/esnext.map.get-or-insert'); +var entryUnbind = require('../../internals/entry-unbind'); + +module.exports = entryUnbind('Map', 'getOrInsert'); diff --git a/packages/core-js/full/map/group-by.js b/packages/core-js/full/map/group-by.js index 76dee9faf3b1..1728363b75dc 100644 --- a/packages/core-js/full/map/group-by.js +++ b/packages/core-js/full/map/group-by.js @@ -6,6 +6,8 @@ require('../../modules/esnext.map.every'); require('../../modules/esnext.map.filter'); require('../../modules/esnext.map.find'); require('../../modules/esnext.map.find-key'); +require('../../modules/esnext.map.get-or-insert'); +require('../../modules/esnext.map.get-or-insert-computed'); require('../../modules/esnext.map.includes'); require('../../modules/esnext.map.key-of'); require('../../modules/esnext.map.map-keys'); diff --git a/packages/core-js/full/map/index.js b/packages/core-js/full/map/index.js index 13bc239eafea..26c5cab4ca25 100644 --- a/packages/core-js/full/map/index.js +++ b/packages/core-js/full/map/index.js @@ -2,6 +2,7 @@ var parent = require('../../actual/map'); require('../../modules/esnext.map.from'); require('../../modules/esnext.map.of'); +require('../../modules/esnext.map.key-by'); require('../../modules/esnext.map.delete-all'); require('../../modules/esnext.map.emplace'); require('../../modules/esnext.map.every'); @@ -9,7 +10,8 @@ require('../../modules/esnext.map.filter'); require('../../modules/esnext.map.find'); require('../../modules/esnext.map.find-key'); require('../../modules/esnext.map.includes'); -require('../../modules/esnext.map.key-by'); +require('../../modules/esnext.map.get-or-insert'); +require('../../modules/esnext.map.get-or-insert-computed'); require('../../modules/esnext.map.key-of'); require('../../modules/esnext.map.map-keys'); require('../../modules/esnext.map.map-values'); diff --git a/packages/core-js/full/map/key-by.js b/packages/core-js/full/map/key-by.js index 13daf9ff1b2a..92c3570bf1d6 100644 --- a/packages/core-js/full/map/key-by.js +++ b/packages/core-js/full/map/key-by.js @@ -8,6 +8,8 @@ require('../../modules/esnext.map.every'); require('../../modules/esnext.map.filter'); require('../../modules/esnext.map.find'); require('../../modules/esnext.map.find-key'); +require('../../modules/esnext.map.get-or-insert'); +require('../../modules/esnext.map.get-or-insert-computed'); require('../../modules/esnext.map.includes'); require('../../modules/esnext.map.key-of'); require('../../modules/esnext.map.map-keys'); diff --git a/packages/core-js/full/map/of.js b/packages/core-js/full/map/of.js index 530b9d895ea9..6a246dc55bef 100644 --- a/packages/core-js/full/map/of.js +++ b/packages/core-js/full/map/of.js @@ -9,6 +9,8 @@ require('../../modules/esnext.map.every'); require('../../modules/esnext.map.filter'); require('../../modules/esnext.map.find'); require('../../modules/esnext.map.find-key'); +require('../../modules/esnext.map.get-or-insert'); +require('../../modules/esnext.map.get-or-insert-computed'); require('../../modules/esnext.map.includes'); require('../../modules/esnext.map.key-of'); require('../../modules/esnext.map.map-keys'); diff --git a/packages/core-js/full/weak-map/from.js b/packages/core-js/full/weak-map/from.js index 39cf8236c1aa..443fd8f0b320 100644 --- a/packages/core-js/full/weak-map/from.js +++ b/packages/core-js/full/weak-map/from.js @@ -6,6 +6,8 @@ require('../../modules/es.weak-map'); require('../../modules/esnext.weak-map.from'); require('../../modules/esnext.weak-map.delete-all'); require('../../modules/esnext.weak-map.emplace'); +require('../../modules/esnext.weak-map.get-or-insert'); +require('../../modules/esnext.weak-map.get-or-insert-computed'); require('../../modules/web.dom-collections.iterator'); var path = require('../../internals/path'); diff --git a/packages/core-js/full/weak-map/get-or-insert-computed.js b/packages/core-js/full/weak-map/get-or-insert-computed.js new file mode 100644 index 000000000000..b8dcd093bd31 --- /dev/null +++ b/packages/core-js/full/weak-map/get-or-insert-computed.js @@ -0,0 +1,6 @@ +'use strict'; +require('../../modules/es.weak-map'); +require('../../modules/esnext.weak-map.get-or-insert-computed'); +var entryUnbind = require('../../internals/entry-unbind'); + +module.exports = entryUnbind('WeakMap', 'getOrInsertComputed'); diff --git a/packages/core-js/full/weak-map/get-or-insert.js b/packages/core-js/full/weak-map/get-or-insert.js new file mode 100644 index 000000000000..6bb549fc698e --- /dev/null +++ b/packages/core-js/full/weak-map/get-or-insert.js @@ -0,0 +1,6 @@ +'use strict'; +require('../../modules/es.weak-map'); +require('../../modules/esnext.weak-map.get-or-insert'); +var entryUnbind = require('../../internals/entry-unbind'); + +module.exports = entryUnbind('WeakMap', 'getOrInsert'); diff --git a/packages/core-js/full/weak-map/index.js b/packages/core-js/full/weak-map/index.js index 5244cd344936..0710b896e85a 100644 --- a/packages/core-js/full/weak-map/index.js +++ b/packages/core-js/full/weak-map/index.js @@ -1,9 +1,11 @@ 'use strict'; var parent = require('../../actual/weak-map'); require('../../modules/es.string.iterator'); -require('../../modules/esnext.weak-map.emplace'); require('../../modules/esnext.weak-map.from'); require('../../modules/esnext.weak-map.of'); +require('../../modules/esnext.weak-map.emplace'); +require('../../modules/esnext.weak-map.get-or-insert'); +require('../../modules/esnext.weak-map.get-or-insert-computed'); require('../../modules/esnext.weak-map.delete-all'); // TODO: remove from `core-js@4` require('../../modules/esnext.weak-map.upsert'); diff --git a/packages/core-js/full/weak-map/of.js b/packages/core-js/full/weak-map/of.js index 611a73b0e941..66d3063318f9 100644 --- a/packages/core-js/full/weak-map/of.js +++ b/packages/core-js/full/weak-map/of.js @@ -5,6 +5,8 @@ require('../../modules/es.weak-map'); require('../../modules/esnext.weak-map.of'); require('../../modules/esnext.weak-map.delete-all'); require('../../modules/esnext.weak-map.emplace'); +require('../../modules/esnext.weak-map.get-or-insert'); +require('../../modules/esnext.weak-map.get-or-insert-computed'); var path = require('../../internals/path'); module.exports = path.WeakMap.of; diff --git a/packages/core-js/modules/esnext.map.get-or-insert-computed.js b/packages/core-js/modules/esnext.map.get-or-insert-computed.js new file mode 100644 index 000000000000..d1580436ab45 --- /dev/null +++ b/packages/core-js/modules/esnext.map.get-or-insert-computed.js @@ -0,0 +1,22 @@ +'use strict'; +var $ = require('../internals/export'); +var aCallable = require('../internals/a-callable'); +var aMap = require('../internals/a-map'); +var MapHelpers = require('../internals/map-helpers'); + +var get = MapHelpers.get; +var has = MapHelpers.has; +var set = MapHelpers.set; + +// `Map.prototype.getOrInsertComputed` method +// https://github.com/tc39/proposal-upsert +$({ target: 'Map', proto: true, real: true, forced: true }, { + getOrInsertComputed: function getOrInsertComputed(key, callbackfn) { + aMap(this); + aCallable(callbackfn); + if (has(this, key)) return get(this, key); + var value = callbackfn(key); + set(this, key, value); + return value; + } +}); diff --git a/packages/core-js/modules/esnext.map.get-or-insert.js b/packages/core-js/modules/esnext.map.get-or-insert.js new file mode 100644 index 000000000000..e26f1505b271 --- /dev/null +++ b/packages/core-js/modules/esnext.map.get-or-insert.js @@ -0,0 +1,18 @@ +'use strict'; +var $ = require('../internals/export'); +var aMap = require('../internals/a-map'); +var MapHelpers = require('../internals/map-helpers'); + +var get = MapHelpers.get; +var has = MapHelpers.has; +var set = MapHelpers.set; + +// `Map.prototype.getOrInsert` method +// https://github.com/tc39/proposal-upsert +$({ target: 'Map', proto: true, real: true, forced: true }, { + getOrInsert: function getOrInsert(key, value) { + if (has(aMap(this), key)) return get(this, key); + set(this, key, value); + return value; + } +}); diff --git a/packages/core-js/modules/esnext.weak-map.get-or-insert-computed.js b/packages/core-js/modules/esnext.weak-map.get-or-insert-computed.js new file mode 100644 index 000000000000..e78b719108ef --- /dev/null +++ b/packages/core-js/modules/esnext.weak-map.get-or-insert-computed.js @@ -0,0 +1,22 @@ +'use strict'; +var $ = require('../internals/export'); +var aCallable = require('../internals/a-callable'); +var aWeakMap = require('../internals/a-weak-map'); +var WeakMapHelpers = require('../internals/weak-map-helpers'); + +var get = WeakMapHelpers.get; +var has = WeakMapHelpers.has; +var set = WeakMapHelpers.set; + +// `WeakMap.prototype.getOrInsertComputed` method +// https://github.com/tc39/proposal-upsert +$({ target: 'WeakMap', proto: true, real: true, forced: true }, { + getOrInsertComputed: function getOrInsertComputed(key, callbackfn) { + aWeakMap(this); + aCallable(callbackfn); + if (has(this, key)) return get(this, key); + var value = callbackfn(key); + set(this, key, value); + return value; + } +}); diff --git a/packages/core-js/modules/esnext.weak-map.get-or-insert.js b/packages/core-js/modules/esnext.weak-map.get-or-insert.js new file mode 100644 index 000000000000..f1e345f1c59e --- /dev/null +++ b/packages/core-js/modules/esnext.weak-map.get-or-insert.js @@ -0,0 +1,18 @@ +'use strict'; +var $ = require('../internals/export'); +var aWeakMap = require('../internals/a-weak-map'); +var WeakMapHelpers = require('../internals/weak-map-helpers'); + +var get = WeakMapHelpers.get; +var has = WeakMapHelpers.has; +var set = WeakMapHelpers.set; + +// `WeakMap.prototype.getOrInsert` method +// https://github.com/tc39/proposal-upsert +$({ target: 'WeakMap', proto: true, real: true, forced: true }, { + getOrInsert: function getOrInsert(key, value) { + if (has(aWeakMap(this), key)) return get(this, key); + set(this, key, value); + return value; + } +}); diff --git a/packages/core-js/proposals/map-upsert-v4.js b/packages/core-js/proposals/map-upsert-v4.js new file mode 100644 index 000000000000..f9ec218965e0 --- /dev/null +++ b/packages/core-js/proposals/map-upsert-v4.js @@ -0,0 +1,6 @@ +'use strict'; +// https://github.com/tc39/proposal-upsert +require('../modules/esnext.map.get-or-insert'); +require('../modules/esnext.map.get-or-insert-computed'); +require('../modules/esnext.weak-map.get-or-insert'); +require('../modules/esnext.weak-map.get-or-insert-computed'); diff --git a/packages/core-js/stage/2.js b/packages/core-js/stage/2.js index 2646486737f0..e842d2845e00 100644 --- a/packages/core-js/stage/2.js +++ b/packages/core-js/stage/2.js @@ -5,7 +5,7 @@ require('../proposals/array-is-template-object'); require('../proposals/async-iterator-helpers'); require('../proposals/extractors'); require('../proposals/iterator-range'); -require('../proposals/map-upsert-stage-2'); +require('../proposals/map-upsert-v4'); require('../proposals/string-dedent'); require('../proposals/symbol-predicates-v2'); // TODO: Obsolete versions, remove from `core-js@4` @@ -14,6 +14,7 @@ require('../proposals/async-explicit-resource-management'); require('../proposals/decorators'); require('../proposals/decorator-metadata'); require('../proposals/iterator-helpers'); +require('../proposals/map-upsert-stage-2'); require('../proposals/set-methods'); require('../proposals/symbol-predicates'); require('../proposals/using-statement'); diff --git a/tests/compat-data/tests-coverage.mjs b/tests/compat-data/tests-coverage.mjs index 75513f8f6ce7..278f5a74fc6c 100644 --- a/tests/compat-data/tests-coverage.mjs +++ b/tests/compat-data/tests-coverage.mjs @@ -29,6 +29,7 @@ const ignore = new Set([ 'esnext.function.un-this', 'esnext.iterator.as-indexed-pairs', 'esnext.iterator.indexed', + 'esnext.map.emplace', 'esnext.map.update-or-insert', 'esnext.map.upsert', 'esnext.math.iaddh', @@ -71,6 +72,7 @@ const ignore = new Set([ 'esnext.typed-array.filter-out', 'esnext.typed-array.group-by', 'esnext.typed-array.to-spliced', + 'esnext.weak-map.emplace', 'esnext.weak-map.upsert', 'web.url-search-params', 'web.url', diff --git a/tests/compat/tests.js b/tests/compat/tests.js index d270c6f4bfb3..b7eb1d20cee9 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1699,9 +1699,6 @@ GLOBAL.tests = { 'esnext.map.delete-all': function () { return Map.prototype.deleteAll; }, - 'esnext.map.emplace': function () { - return Map.prototype.emplace; - }, 'esnext.map.every': function () { return Map.prototype.every; }, @@ -1717,6 +1714,12 @@ GLOBAL.tests = { 'esnext.map.from': function () { return Map.from; }, + 'esnext.map.get-or-insert': function () { + return Map.prototype.getOrInsert; + }, + 'esnext.map.get-or-insert-computed': function () { + return Map.prototype.getOrInsertComputed; + }, 'esnext.map.includes': function () { return Map.prototype.includes; }, @@ -1875,8 +1878,11 @@ GLOBAL.tests = { 'esnext.weak-map.delete-all': function () { return WeakMap.prototype.deleteAll; }, - 'esnext.weak-map.emplace': function () { - return WeakMap.prototype.emplace; + 'esnext.weak-map.get-or-insert': function () { + return WeakMap.prototype.getOrInsert; + }, + 'esnext.weak-map.get-or-insert-computed': function () { + return WeakMap.prototype.getOrInsertComputed; }, 'esnext.weak-map.from': function () { return WeakMap.from; diff --git a/tests/entries/unit.mjs b/tests/entries/unit.mjs index cea60fe2af5d..8bf1809a351a 100644 --- a/tests/entries/unit.mjs +++ b/tests/entries/unit.mjs @@ -779,6 +779,8 @@ for (PATH of ['core-js-pure', 'core-js']) { ok(load(NS, 'map/find')(new Map([[1, 2], [2, 3], [3, 4]]), it => it % 2) === 3); ok(load(NS, 'map/find-key')(new Map([[1, 2], [2, 3], [3, 4]]), it => it % 2) === 2); ok(load(NS, 'map/from')([[1, 2], [3, 4]]) instanceof Map); + ok(load(NS, 'map/get-or-insert')(new Map([[1, 2]]), 1, 3) === 2); + ok(load(NS, 'map/get-or-insert-computed')(new Map([[1, 2]]), 1, key => key) === 2); ok(load(NS, 'map/includes')(new Map([[1, 2]]), 2), true); ok(load(NS, 'map/key-by')([], it => it) instanceof Map); ok(load(NS, 'map/key-of')(new Map([[1, 2]]), 2), 1); @@ -848,6 +850,8 @@ for (PATH of ['core-js-pure', 'core-js']) { ok(load(NS, 'symbol/replace-all')); ok(load(NS, 'weak-map/delete-all')(new WeakMap(), [], {}) === false); ok(load(NS, 'weak-map/emplace')(new WeakMap(), {}, { insert: () => ({ a: 42 }) }).a === 42); + ok(load(NS, 'weak-map/get-or-insert')(new WeakMap([[{}, 2]]), {}, 3) === 3); + ok(load(NS, 'weak-map/get-or-insert-computed')(new WeakMap([[{}, 2]]), {}, () => 3) === 3); ok(load(NS, 'weak-map/upsert')(new WeakMap(), {}, null, () => 42) === 42); ok(load(NS, 'weak-map/from')([[{}, 1], [[], 2]]) instanceof WeakMap); ok(load(NS, 'weak-map/of')([{}, 1], [[], 2]) instanceof WeakMap); @@ -939,6 +943,7 @@ for (PATH of ['core-js-pure', 'core-js']) { load('proposals/map-update-or-insert'); load('proposals/map-upsert'); load('proposals/map-upsert-stage-2'); + load('proposals/map-upsert-v4'); load('proposals/math-extensions'); load('proposals/math-signbit'); load('proposals/math-sum');