@corefunc/upsert-map
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i @corefunc/upsert-map

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

15.8 kB

Total Files

10

Last publish

Collaborators

  • r37r0m0d3l