Skip to main content
Home
This package has been archived, and as such it is read-only.

Built and signed on GitHub Actions

Polyfill of `emplace` method for JavaScript

This package works with Cloudflare Workers, Node.js, Deno, BunIt is unknown whether this package works with Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
It is unknown whether this package works with Browsers
JSR Score
100%
Published
a year ago (0.1.0)

Polyfill of Map.prototype.emplace and WeakMap.prototype.emplace for JavaScript which have been proposed in https://github.com/tc39/proposal-upsert.

Examples

emplaceMap

const map = new Map([["foo", 9]]);

emplaceMap(map, "foo", {
    insert() {
        return 7;
    },
    update(oldValue) {
        return oldValue * 3;
    },
});

emplaceMap(map, "bar", {
    insert() {
        return 7;
    },
    update(oldValue) {
        return oldValue * 3;
    },
});

console.log(map); // Map(2) { "foo": 27, "bar": 7 }

emplaceWeakMap

const map = new WeakMap<WeakKey, number>();
const o1 = {};
const o2 = function () {};

map.set(o1, 21);

emplaceWeakMap(map, o1, {
    insert() {
        return 100;
    },
    update(oldValue) {
        return oldValue + 50;
    },
});

emplaceWeakMap(map, o2, {
    insert() {
        return 100;
    },
    update(oldValue) {
        return oldValue + 50;
    },
});

console.log(map.get(o1)); // 71
console.log(map.get(o2)); // 100
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@polyfill/map-emplace

Import symbol

import * as map_emplace from "@polyfill/map-emplace";
or

Import directly with a jsr specifier

import * as map_emplace from "jsr:@polyfill/map-emplace";

Add Package

pnpm i jsr:@polyfill/map-emplace
or (using pnpm 10.8 or older)
pnpm dlx jsr add @polyfill/map-emplace

Import symbol

import * as map_emplace from "@polyfill/map-emplace";

Add Package

yarn add jsr:@polyfill/map-emplace
or (using Yarn 4.8 or older)
yarn dlx jsr add @polyfill/map-emplace

Import symbol

import * as map_emplace from "@polyfill/map-emplace";

Add Package

vlt install jsr:@polyfill/map-emplace

Import symbol

import * as map_emplace from "@polyfill/map-emplace";

Add Package

npx jsr add @polyfill/map-emplace

Import symbol

import * as map_emplace from "@polyfill/map-emplace";

Add Package

bunx jsr add @polyfill/map-emplace

Import symbol

import * as map_emplace from "@polyfill/map-emplace";