npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

map-push

v3.0.0

Published

Push new items to Array entries of a Map

Downloads

5

Readme

map-push

GitHub Codecov GitHub Workflow Status

Pushes new items to Array entries of a Map

Introduction

Sometimes you have a Map with entries of type Array and you want to push new items to those entries.
You also want to create a new Array initialized with the item you want to push if an entry for a particular key does not exist yet.

To do that, I always end up doing something along the lines of:


const myMap = new Map();
const anItem = toPush;
const aKey = "some-key";

if (myMap.Has(aKey)) {

  const arrayEntry = myMap.get(aKey);
	
  arrayEntry.push(anItem);

}
else {

  myMap.set(aKey, [anItem]);

}

Which is boring, long and does not handle edge cases.
So I thought: maybe I can write an utility function for that. Enter map-push.

map-push exposes is a function called mapPush that encapsulates the code above and handles a few edge cases.
What edge cases does mapPush handles, you say ? Well...

  • mapPush will check if the entry is in fact an Array before pushing, and if not will fallback to use Map.set().
  • mapPush will check if the Map to change is actually a Map and is not null or undefined.
  • mapPush will create a new Array only if the Map does not have entries for the key given.

So, how do you use mapPush ?

Usage

mapPush has the following signature: mapPush(map, key, value)
You have to pass a Map, the key of an entry (that might not exists) and a value.
In return you will get new length of the Array (like Array.push()).

The NPM package is hybrid, that means that you get both the ES Modules and the CommonJS version.

With CommonJS

const { mapPush } = require('map-push/cjs');

Note: Import from /cjs folder !

With ES Modules

import { mapPush } from 'map-push'

What about installation ? You have two options...

Installation

You can get mapPush on NPM by running npm install map-push.
Or, as this is a small utility function, I suggest to simply copy/paste it and skipping NPM.