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

key-master

v4.1.0

Published

hashmap + constructor function = a hashmap you don't have to check before calling get()

Downloads

1,705

Readme

key-master

Replaces "maintain a map of constructed objects" boilerplate.

Build Status TypeScript supported

Seen this pattern before?

const defaultValue = key => [ key ]

const myMap = new Map()

// ...

function doStuff(thing) {
	if (!myMap.has(thing)) {
		myMap.set(thing, defaultValue(thing))
	}

	actuallyDoStuff(myMap.get(thing))
}

I figure I've typed that enough times in my life. Now I'm going to just use this module.

const map = keyMaster(key => defaultValue(key))

map.get('howdy') // => [ 'howdy' ]

actuallyDoStuff(map.get('howdy'))

Usage

  • Install: npm install key-master
  • Use: const keyMaster = require('key-master')

This library uses ES2015 syntax, so if you're deploying to IE11, you'll need to be transpiling your project with Babel or something.

API

const map = keyMaster(defaultValueReturningFunction, [map])

The defaultValueReturningFunction is called whenever the map doesn't already have a value for the given key.

It is passed the key as its first argument.

The map argument is optional. It can be anything implementing .get, .set, .has, and .delete. If not passed in, keyMaster will use a new JavaScript Map by default.

const map = keyMaster(yourFactory)

const map = keyMaster(yourFactory, new WeakMap())

const map = keyMaster(yourFactory, new Map())

const map = keyMaster(yourFactory, { get, set, has, delete })

value = map.get(key)

Returns the value in the map. If there isn't a value for that key, the constructor calls the defaultValueReturningFunction that was passed to the constructor, passing in the key. Whatever the constructor function returns is inserted into the map and returned by get.

map.set(key, value)

Inserts a value into the map, overwriting anything that might be there.

map.delete(key)

Removes a value from the map.

bool = map.has(key)

Returns true if the key exists in the map, false if the key does not exist in the map.

jsMap = map.getUnderlyingDataStructure()

Returns the underlying data structure. If you passed in a map to the constructor, it returns that. Otherwise, it returns the plain-old object that was used as a hashmap.

Using a plain-old-object as a map

If you want to use an object as a map instead of a Map or WeakMap, you can use this function to create a map to pass in:

function makeObjectMap() {
	var obj = Object.create(null)

	return {
		get: function(key) {
			return obj[key]
		},
		set: function(key, value) {
			obj[key] = value
		},
		has: function(key) {
			return Object.prototype.hasOwnProperty.call(obj, key)
		},
		delete: function(key) {
			delete obj[key]
		},
		object: obj
	}
}

License

WTFPL