# key-master

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

Latest version **4.1.0** (published 2019-08-01) · WTFPL license · 0 weekly downloads

## Install

```sh
npm install key-master
pnpm add key-master
yarn add key-master
bun add key-master
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2019-08-01 |
| First published | 2015-05-12 |
| Weekly downloads | 0 |
| License | WTFPL |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | TehShrike |
| Maintainers | tehshrike |
| Keywords | map, hashmap |

## Links

- npm: https://www.npmjs.com/package/key-master
- Repository: https://github.com/TehShrike/key-master
- Issues: https://github.com/TehShrike/key-master/issues
- npm.io page: https://npm.io/package/key-master

## Alternatives

- [@gemini-wallet/core](https://npm.io/package/@gemini-wallet/core.md) — 515.6K weekly downloads
- [utility](https://npm.io/package/utility.md) — 416.6K weekly downloads
- [@primno/dpapi](https://npm.io/package/@primno/dpapi.md) — 7.2K weekly downloads
- [pi-readseek](https://npm.io/package/pi-readseek.md) — 3.7K weekly downloads
- [@emilia-protocol/verify](https://npm.io/package/@emilia-protocol/verify.md) — 1.1K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2019-08-01
- 4.0.0 — 2018-09-17
- 3.1.0 — 2017-08-17
- 3.0.0 — 2017-07-15
- 2.0.2 — 2017-02-11
- 2.0.1 — 2016-09-12
- 2.0.0 — 2016-08-27
- 1.2.0 — 2016-08-26
- 1.1.0 — 2015-05-13
- 1.0.0 — 2015-05-12

## README

# key-master

Replaces "maintain a map of constructed objects" boilerplate.

[![Build Status](https://travis-ci.org/TehShrike/key-master.svg)](https://travis-ci.org/TehShrike/key-master)
![TypeScript supported](https://img.shields.io/npm/types/key-master.svg)

Seen this pattern before?

<!-- js
require('ts-node/register')
var keyMaster = require('.')
function actuallyDoStuff() {

}
-->

```js
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.

```js
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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:

```js
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](http://wtfpl2.com)

---
_Source: https://npm.io/package/key-master · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
