Skip to content

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.

License

Notifications You must be signed in to change notification settings

corefunc/upsert-map

Repository files navigation

CoreFunc

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

Buy Me A Coffee Follow New Releases

NPM Version NPM Downloads GitHub Stars TypeScript Typings Travis CI LGTM


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)

See also

My other projects

About

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.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published