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

@omenlog/super-map

v1.0.6

Published

Map like structure with super powers

Downloads

2

Readme

SuperMap

Build

SuperMap is a zero dependency ADT like JS MAP but with super powers 😎, is implemented with Typescript so it's completely typed.

This package is 100% compatible with the Map structure built in the language by default, indeed the original one is used under the hood and the new features are implemented using Monkey Patching.

Installation

npm install @omenlog/super-map

SuperMap is a factory function so for create a new super map we call the function without use the new operator, besides that we can pass an array of key value pairs during the initialization in the same way like the JS Map.

import { SuperMap } from '@omenlog/super-map';

const map = SuperMap(); // empty superMap

const mapWithValues = SuperMap([["key", 1]]); // superMap with one entry 

Fallback values on get

get method support a second argument that is used as fallback if the key accessed is undefined,in that case the fallback value is assigned to the current key and returned.

const map = SuperMap<string, number>();
const defaultValue = 10;

console.log(map.get("key", defaultValue))); // 10
console.log(map.get("key")); // 10

console.log(map.get("not-defined-key"));  // undefined

Update values

SuperMap has an update method that allow update values very easily

const map = SuperMap<string, number>();

map.set("key", 1);

map.update("key", 2);

console.log(map.get("key")); // 2 value update

Also if we want to apply the update operation based on the existing value, a function can be passed as second argument, in that case the updater function will be called with the previous value to produce a new one.

const map = SuperMap<string, number>();

map.set("key", 1);

map.update("key", v => v + 10);

console.log(map.get("key")); // 11

//value update from the previous one

map

Every super map is a functor then is possible apply a mapper function over every one of its values to obtain a new super map after the transformation.

const sm = SuperMap([["key", 1], ["another-key", 2]]);

const newMap = sm.map(v => v * 2);

console.log(newMap.get("key")); // 2
console.log(newMap.get("another-key")); // 4

The mapper function receive 3 arguments the value its key and the supermap being transformed, one details about this function is that it shouldn't return null or undefined, in that case we will have a type error.

filter

We can filter super map values based on some predicate to obtain a new map.

const map = SuperMap([["one-key", 1], ["two-key", 2]]);

const newSuperMap = map.filter(v => v % 2 === 0);

console.log(newSuperMap.size); // 1

console.log(newSuperMap.get("one-key")); // undefined

console.log(newSuperMap.get("two-key")); // 2

The function passed to filter must return a boolean and it receive the same 3 argumentslike the map method.

reduce

Every super map implement the reducer protocol, so they can be reduced very similar to how we can do it with JS Arrays

const map = SuperMap([["one-key", 1], ["two-key", 2]]);

const sum = map.reduce((acc,value) => acc + value); // is possible  pass an initial value as second argument

console.log(sum); // 3

reduceAsync

Sometimes we want to reduce our map applying some kind of async function, SuperMap support that use case through reduceAsync function. This function require that our reducer return a Promise, after the reduction is done we obtain a Promise with the final value.

const map = SuperMap([["key", 1], ["another-key", 2]]);

const asyncAverage = await map.reduceAsync((acc, val) => {
    return new Promise((resolve) => setTimeout(() => resolve(acc + val), 0));
}, 0);

console.log(asyncAverage) // 3;