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

defaultmap

v1.0.3

Published

A map that gives you a default value if a key is not present.

Downloads

7

Readme

Default Map

A map that gives you a default value if a key is not present. Requires an ES6 runtime. Can be subclassed.

var withArray = new DefaultMap([], (map, k) => {
  const xs = []
  map.set(k, xs);
  return xs;
})

withArray.get("someKey").push(1);
withArray.get("someKey") // [1]

// instances of 'MapWithArrays' would perform like `withArray` above
class MapWithArrays extends DefaultMap {
  constructor(members) {
    super(members, (m, k, set) => {
      set([])
    })
  }
}


class Counter extends DefaultMap {
  constructor(members) {
    super(members, () => 0)
  }
  add(k, n) {
    const now = this.get(k) + n;
    this.set(k, now)
    return now;
  }
}

var visits = new Counter;
visits.get("newUser") // 0, not mutating visits

visits.add("newUser", 10)
visits.get("newUser") // 10

You control the default operation by providing a function that returns the default value for a key. It's your responsibility to store it, if you'd like. If your default is a mutable value like an array storing it makes sense. If it's a primitive like a number, it's likely better to save space and simply return it.

API

DefaultMap(members: enumerable (as per Map), default: (m: Map, k: any, setAndReturn: (v: any) => void) => defaultValue)

Simply pass in a function to create the default value. It will be called with the map and a key that was requested but not found. You can mutate the map if you like. Whatever you return will be returned from .get(k).

Since it's a pretty common pattern to want to mutate and return the value, you may call the third argument, setAndReturn. The value passed with be the new value of .get(k), and will be returned. The return value of default will be ignored if setAndReturn is called.

// example of using setAndReturn
var withArray = new DefaultMap([], (_m, _k, set) => set([]))

withArray.get("someKey").push(1);
withArray.get("someKey") // [1]

Install & use

npm i -S defaultmap
var DefaultMap = require("defaultmap");