https://github.com/babiabeo/map-emplace
Polyfill of `emplace` method for JavaScript
https://github.com/babiabeo/map-emplace
javascript map proposal tc39 typescript weakmap
Last synced: 6 months ago
JSON representation
Polyfill of `emplace` method for JavaScript
- Host: GitHub
- URL: https://github.com/babiabeo/map-emplace
- Owner: babiabeo
- License: mit
- Archived: true
- Created: 2024-04-28T14:43:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-05-01T09:50:21.000Z (over 1 year ago)
- Last Synced: 2025-02-24T11:49:36.788Z (8 months ago)
- Topics: javascript, map, proposal, tc39, typescript, weakmap
- Language: TypeScript
- Homepage: https://jsr.io/@polyfill/map-emplace
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# map-emplace
[](https://github.com/babiabeo/map-emplace/actions/workflows/ci.yml)
[](https://codecov.io/gh/babiabeo/map-emplace)
[](https://jsr.io/@polyfill/map-emplace)
[](https://jsr.io/@polyfill/map-emplace)
> [!IMPORTANT]
> This package has been moved from
> [`@babia/map-emplace`](https://jsr.io/@babia/map-emplace) to
> [`@polyfill/map-emplace`](https://jsr.io/@polyfill/map-emplace)
Polyfill of `Map.prototype.emplace` and `WeakMap.prototype.emplace` for
JavaScript based on
[`tc39/proposal-upsert`](https://github.com/tc39/proposal-upsert) specification.
## Install
### Bun
```sh
bunx jsr add @polyfill/map-emplace
```
### Deno
```sh
deno add @polyfill/map-emplace
```
or use import specifier:
```ts
import { emplaceMap } from "jsr:@polyfill/map-emplace";
```
### Node
```sh
# npm
npx jsr add @polyfill/map-emplace
# yarn
yarn dlx jsr add @polyfill/map-emplace
# pnpm
pnpm dlx jsr add @polyfill/map-emplace
```
## Why?
As mentioned in the proposal:
> Adding and updating values of a `Map` are tasks that developers often perform
> in conjunction. There are currently no `Map` prototype methods for either of
> those two things, let alone a method that does both. The workarounds involve
> multiple lookups and developer inconvenience while avoiding encouraging code
> that is surprising or is potentially error prone.
Therefore, `emplace` method has been proposed as a solution for this problem.
## Examples
```ts
emplaceMap(map, "foo", {
// If map does not include "foo", sets "foo" to 1
insert() {
return 1;
},
// Otherwise, updates its value.
update(oldValue) {
return oldValue + 2;
},
});
```
### Insert if absent
You might want to set a new value to a key if it does not exist in map, and then
use that value in code:
```ts
if (!map.has("foo")) {
map.set("foo", "bar");
}
const foo = map.get("foo");
// do something with "foo"...
```
With `emplace`:
```ts
const foo = emplaceMap(map, "foo", {
insert() {
return "bar";
},
});
// do something with "foo"...
```
### Update if present
You might want to update the value only if the key is exist:
```ts
if (map.has("foo")) {
map.set("foo", "foobar");
}
```
With `emplace`:
```ts
emplaceMap(map, "foo", {
update() {
return "foobar";
},
});
```
## Specification
- [`Map.prototype.emplace`](https://tc39.es/proposal-upsert/#sec-map.prototype.emplace)
- [`WeakMap.prototype.emplace`](https://tc39.es/proposal-upsert/#sec-weakmap.prototype.emplace)
## See also
- [tc39/proposal-upsert](https://github.com/tc39/proposal-upsert)
- [Polyfill in core-js](https://github.com/zloirock/core-js?tab=readme-ov-file#mapprototypeemplace)