{"id":19357534,"url":"https://github.com/babiabeo/map-emplace","last_synced_at":"2025-04-23T11:30:24.294Z","repository":{"id":236785024,"uuid":"793140273","full_name":"babiabeo/map-emplace","owner":"babiabeo","description":"Polyfill of `emplace` method for JavaScript","archived":true,"fork":false,"pushed_at":"2024-05-01T09:50:21.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-24T11:49:36.788Z","etag":null,"topics":["javascript","map","proposal","tc39","typescript","weakmap"],"latest_commit_sha":null,"homepage":"https://jsr.io/@polyfill/map-emplace","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/babiabeo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-28T14:43:18.000Z","updated_at":"2024-10-04T14:51:54.000Z","dependencies_parsed_at":"2024-05-01T04:23:47.003Z","dependency_job_id":null,"html_url":"https://github.com/babiabeo/map-emplace","commit_stats":null,"previous_names":["babiabeo/map-emplace"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babiabeo%2Fmap-emplace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babiabeo%2Fmap-emplace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babiabeo%2Fmap-emplace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/babiabeo%2Fmap-emplace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/babiabeo","download_url":"https://codeload.github.com/babiabeo/map-emplace/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250424911,"owners_count":21428466,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","map","proposal","tc39","typescript","weakmap"],"created_at":"2024-11-10T07:07:59.994Z","updated_at":"2025-04-23T11:30:24.062Z","avatar_url":"https://github.com/babiabeo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# map-emplace\n\n[![ci status](https://github.com/babiabeo/map-emplace/actions/workflows/ci.yml/badge.svg)](https://github.com/babiabeo/map-emplace/actions/workflows/ci.yml)\n[![codecov](https://codecov.io/gh/babiabeo/map-emplace/graph/badge.svg?token=K419Z9KMJ9)](https://codecov.io/gh/babiabeo/map-emplace)\n[![JSR](https://jsr.io/badges/@polyfill/map-emplace)](https://jsr.io/@polyfill/map-emplace)\n[![JSR Score](https://jsr.io/badges/@polyfill/map-emplace/score)](https://jsr.io/@polyfill/map-emplace)\n\n\u003e [!IMPORTANT]\n\u003e This package has been moved from\n\u003e [`@babia/map-emplace`](https://jsr.io/@babia/map-emplace) to\n\u003e [`@polyfill/map-emplace`](https://jsr.io/@polyfill/map-emplace)\n\nPolyfill of `Map.prototype.emplace` and `WeakMap.prototype.emplace` for\nJavaScript based on\n[`tc39/proposal-upsert`](https://github.com/tc39/proposal-upsert) specification.\n\n## Install\n\n### Bun\n\n```sh\nbunx jsr add @polyfill/map-emplace\n```\n\n### Deno\n\n```sh\ndeno add @polyfill/map-emplace\n```\n\nor use import specifier:\n\n```ts\nimport { emplaceMap } from \"jsr:@polyfill/map-emplace\";\n```\n\n### Node\n\n```sh\n# npm\nnpx jsr add @polyfill/map-emplace\n\n# yarn\nyarn dlx jsr add @polyfill/map-emplace\n\n# pnpm\npnpm dlx jsr add @polyfill/map-emplace\n```\n\n## Why?\n\nAs mentioned in the proposal:\n\n\u003e Adding and updating values of a `Map` are tasks that developers often perform\n\u003e in conjunction. There are currently no `Map` prototype methods for either of\n\u003e those two things, let alone a method that does both. The workarounds involve\n\u003e multiple lookups and developer inconvenience while avoiding encouraging code\n\u003e that is surprising or is potentially error prone.\n\nTherefore, `emplace` method has been proposed as a solution for this problem.\n\n## Examples\n\n```ts\nemplaceMap(map, \"foo\", {\n    // If map does not include \"foo\", sets \"foo\" to 1\n    insert() {\n        return 1;\n    },\n    // Otherwise, updates its value.\n    update(oldValue) {\n        return oldValue + 2;\n    },\n});\n```\n\n### Insert if absent\n\nYou might want to set a new value to a key if it does not exist in map, and then\nuse that value in code:\n\n```ts\nif (!map.has(\"foo\")) {\n    map.set(\"foo\", \"bar\");\n}\n\nconst foo = map.get(\"foo\");\n\n// do something with \"foo\"...\n```\n\nWith `emplace`:\n\n```ts\nconst foo = emplaceMap(map, \"foo\", {\n    insert() {\n        return \"bar\";\n    },\n});\n\n// do something with \"foo\"...\n```\n\n### Update if present\n\nYou might want to update the value only if the key is exist:\n\n```ts\nif (map.has(\"foo\")) {\n    map.set(\"foo\", \"foobar\");\n}\n```\n\nWith `emplace`:\n\n```ts\nemplaceMap(map, \"foo\", {\n    update() {\n        return \"foobar\";\n    },\n});\n```\n\n## Specification\n\n- [`Map.prototype.emplace`](https://tc39.es/proposal-upsert/#sec-map.prototype.emplace)\n- [`WeakMap.prototype.emplace`](https://tc39.es/proposal-upsert/#sec-weakmap.prototype.emplace)\n\n## See also\n\n- [tc39/proposal-upsert](https://github.com/tc39/proposal-upsert)\n- [Polyfill in core-js](https://github.com/zloirock/core-js?tab=readme-ov-file#mapprototypeemplace)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabiabeo%2Fmap-emplace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbabiabeo%2Fmap-emplace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbabiabeo%2Fmap-emplace/lists"}