🚀 Launch Week Day 4:Introducing the Alert Details Page: A Better Way to Explore Alerts.Learn More →
Socket
Book a DemoInstallSign in
Socket

upsert-map

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

upsert-map

A Map with an upsert method and gc support

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

upsert-map

A Map with an upsert method that is like a "get or set" method. Also supports gc'ing empty upserts.

npm install upsert-map

Usage

const UpsertMap = require('upsert-map')

const map = new UpsertMap(() => new Set(), set => !set.size)

map.upsert(10).add(1)
map.upsert(10).add(2)
console.log(...m.values()) // new Set(1, 2)
map.get(10).delete(1)
map.get(10).delete(1)
console.log(...m.values()) // <empty>

API

m = new UpsertMap(createValue, isEmpty)

Make a new upsert map.

  • createValue(key) should return a value to be upserted for a key.
  • isEmpty(val) can optionally be added for auto gc of empty values.

val = m.upsert(key)

Returns the value for key if already exists, otherwise it is created and inserted into the map.

val = m.get(key)

Similar to Map.get

bool = m.has(key)

Similar to Map.has

m.delete(key)

Similar to Map.delete

size = m.size

Similar to Map.size

iterator = m.values()

Similar to Map.values()

iterator = m.keys()

Similar to Map.keys()

iterator = m[Symbol.iterator]()

Similar to MapSymbol.iterator

License

MIT

FAQs

Package last updated on 25 Aug 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts