@corefunc/upsert-map
v1.0.0
Published
JavaScript MergeMap
Downloads
4
Readme
CoreFunc UpsertMap
Addition of a method that will add a value to a map if the map does not already have something at key, and will also update an existing value at key.
ECMAScript proposal Map.prototype.upsert
If you use this project don't forget to give a ⭐ star ⭐ to it on GitHub!
Usage
CommonJS import.
const { UpsertMap } = require("@corefunc/upsert-map");
ECMAScript Module.
import { UpsertMap } from "@corefunc/upsert-map";
AMD, UMD, browser script tag.
<script src="https://unpkg.com/@corefunc/upsert-map"></script>
CDN (unpkg https://unpkg.com/)
<script src="https://unpkg.com/@corefunc/upsert-map" type="module"></script>
Deno (Pika https://pika.dev/)
import { UpsertMap } from "https://cdn.pika.dev/@corefunc/upsert-map";
Examples
import { UpsertMap } from "@corefunc/upsert-map";
const map = new UpsertMap();
const key = "YOUR_KEY";
// You can skip update function and use only insert function
map.upsert(key, undefined, () => new Set());
// Use both update function and insert function
map.upsert(
key,
(currentValue) => {
currentValue.add(1);
return currentValue;
},
() => new Set()
);
// Use only update function
map.upsert(key, (currentValue) => {
currentValue.add(2);
return currentValue;
});
map.get(key) === Set(1, 2)