1.0.0 • Published 4 years ago

@corefunc/upsert-map v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

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