🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

map-ext

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

map-ext

map-ext

latest
Source
npmnpm
Version
0.1.5
Version published
Weekly downloads
70
66.67%
Maintainers
1
Weekly downloads
 
Created
Source

A simple addition to (weak) map

Personally I hate it to check if something is in a map, and if not add an object. So, I always write this small snippet, I decided to make a repo out of it.

A simple example;

import {WeakMapExt} from "map-ext";

type EventHandler = (...[]) => any;

const eventStore = new WeakMapExt<object, MapExt<string, Set<EventHandler>>>(() => new MapExt(() => new Set()));

class Events {
    async emit(key: string, ...args: any[]) {
        for (const cb of eventStore.get(this).get(key)) {
            await cb(...args);
        }
    }

    on(key: string, cb: EventHandler) {
        eventStore.get(this).get(key).add(cb);
    }

    off(key: string, cb: EventHandler) {
        eventStore.get(this).get(key).delete(cb);
    }
}

esm - commonjs

Since this package is so small it supports both esm and commonjs

Keywords

map-ext

FAQs

Package last updated on 12 Jun 2023

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