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-helper

v1.3.3

Published

A library to use Array-like functionality with Javascript Maps

Downloads

7

Readme

Map Helper

A Library for adding array-like functionality to Javascript Maps.

I love Javascript Maps, but they need some additional functionality to make them more useful. This library is an attempt to provide that functionality.

Install

install via npm

npm install --save map-helper

then in your application

import * as mapHelper from 'map-helper';

Methods

mapMap

takes and a Map and provides a new Map after applying a callback.

mapHelper.mapMap(map,callback)

Arguments

map (Map)

callback (function) [Key, value, index]

Returns

(Map)

Example

let foo = new Map([
    ["stuff",0],
    ["junk",1],
    ["gubbins",2]
]);

let bar = mapHelper.mapMap(foo, (key,value, index) => [key , value + 1]);

// result: Map(3) {"stuff" => 1, "junk" => 2, "gubbins" => 3}

reduceMap

Reduces a map based on a callback and an initial value.

mapHelper.mapMap(map,callback,inital)

Arguments

map (Map)

callback (function) [Key, value, index]

initial (any)

Returns

(any)

Example

let foo = new Map([
    ["stuff",0],
    ["junk",1],
    ["gubbins",2]
]);

let baz = mapHelper.reduceMap(foo, (current, key, value, index) => current + value , 0);

// result: 3

mergeMap

Merges an array of maps, aggregating key collisions as default behavior. Accepts an optional callback to handle collisions

mapHelper.mergeMap(maps,callback)

Arguments

maps (Array)

callback (function) [Key, oldValue, newValue, numberOfCollisions]

Returns

(Map)

Example

let foo = new Map([
    ["junk",1],
    ["gubbins",2]
]);

let anotherMap = new Map([
    ["mary", 1],
    ["gubbins", 4]
]);

let baz = mapHelper.reduceMap(foo, (current, key, value, index) => current + value , 0);

// result: Map(3) {"junk" => 1, "gubbins" => (2) [2, 4], "mary" => 1}

let mergedWithCallback = mapHelper.mergeMaps([foo, anotherMap], (key, oldVal, newVal, collisions) =>{
    return oldVal + newVal;
});

//result: Map(3) {"junk" => 1, "gubbins" => 6, "mary" => 1}

invertMap

Inverts the keys and values of a map, aggregating values with key collisions as default behaviour. accepts an options callback to handle key collisions.

mapHelper.invertMap(map,callback)

Arguments

map (Map)

callback (function) [Key, oldValue, newValue, numberOfCollisions]

Returns

(Map)

Example

let anotherMap = new Map([
    ["peter", 0],
    ["paul", 1],
    ["mary", 1],
    ["gubbins", 4]
]);

let invertedMap = mapHelper.invertMap(anotherMap);

// result: Map(3) {0 => "peter", 1 => (2) ["paul", "mary"], 4 => "gubbins"}

mapFromObject

converts a plain object to a map, using only own properties.

mapHelper.mapFromObject(object)

Arguments

object (Object)

Returns

(Map)

Example

let objectToConvert = {
    key1:"foo",
    key2:"bar",
    key3:"baz"
}

let convertedObject = mapHelper.mapFromObject(objectToConvert);

// result: Map(3) {"key1" => "foo", "key2" => "bar", "key3" => "baz"}