GithubHelp home page GithubHelp logo

key-master's Introduction

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

key-master's People

Contributors

tehshrike avatar jurca avatar artskydj avatar m59peacemaker avatar saibotsivad avatar

Stargazers

Stacey Reiman avatar  avatar Jayson Harshbarger avatar

Watchers

James Cloos avatar  avatar  avatar

key-master's Issues

TypeScript support

Hello,

would you be willing to support TypeScript ecosystem? There are various options, however, I think it would be best for future maintainability to port the code to TypeScript instead of adding separate type declaration files.

I am willing to do all the work myself and send a pull request.

Would that be OK with you, or would you rather go for a different option?

Pass the key to the factory function

Probably breaking changes:

  • Use the getter as an async getter/factory instead of a constructor no don't do this
  • Pass the string of the key into the getter/factory function

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.