# m-o

> Functions that work on both Maps and Objects: has, get, set, delete, entries, keys, values, etc.

Latest version **2.2.0** (published 2018-01-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install m-o
pnpm add m-o
yarn add m-o
bun add m-o
```

## 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 | 2.2.0 |
| Published | 2018-01-22 |
| First published | 2017-11-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=7.0.0 |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | John Lamansky |
| Maintainers | lamansky |
| Keywords | Map, object, has, get, set, delete, entries, keys, values, construct |

## Links

- npm: https://www.npmjs.com/package/m-o
- Repository: https://github.com/lamansky/m-o
- Issues: https://github.com/lamansky/m-o/issues
- npm.io page: https://npm.io/package/m-o

## Dependencies (4)

- [is-object](https://npm.io/package/is-object.md) ^1.0.1
- [new-object](https://npm.io/package/new-object.md) ^3.0.0
- [if-else-throw](https://npm.io/package/if-else-throw.md) ^1.0.0
- [get-own-property](https://npm.io/package/get-own-property.md) ^1.0.0

## 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

- 2.2.0 (latest) — 2018-01-22
- 2.1.1 — 2017-12-14
- 2.1.0 — 2017-12-09
- 2.0.0 — 2017-12-05
- 1.0.0 — 2017-11-28

## README

# m-o

A collection of functions that work on both Maps and Objects, letting you write code that can handle either, if that’s your M.O.

The functions are: `has`, `hasIn`, `get`, `getIn`, `set`, `edit`, `delete`, `construct`, `reconstruct`, `entries`, `entriesArray`, `keys`, `keysArray`, `values`, and `valuesArray`.

## Installation

Requires [Node.js](https://nodejs.org/) 7.0.0 or above.

```bash
npm install m-o --save
```

The module exports an object with multiple methods.

## Usage

Nota Bene: Maps can accept keys of any type, but Objects cannot. If you expect your code to work with both, then you should only use strings/numbers/symbols as keys.

```javascript
const mo = require('m-o')

const map = new Map()
const obj = {}

// Set
mo.set(map, 'hello', 'world')
mo.set(obj, 'hello', 'world')

// Has
mo.has(map, 'hello') // true
mo.has(obj, 'hello') // true

// Get
mo.get(map, 'hello') // 'world'
mo.get(obj, 'hello') // 'world'

// vs. the normal way:
map.get('hello') // 'world'
obj.hello // 'world'

// Edit
mo.edit(map, 'hello', value => value + '!')
mo.edit(obj, 'hello', value => value + '!')
mo.get(map, 'hello') // 'world!'
mo.get(obj, 'hello') // 'world!'

// Delete
mo.delete(map, 'hello') // true
mo.delete(obj, 'hello') // true
mo.delete(map, 'nonexistent') // false
mo.delete(obj, 'nonexistent') // false

// Entries
mo.entries(map) // MapIterator
mo.entries(obj) // ArrayIterator

// Construct a Map or an Object, depending on the class provided
const entries = [['hello', 'world']]
mo.construct(Object, entries) // {hello: 'world'}
mo.construct(Map, entries) // Map
class XMap extends Map {}
mo.construct(XMap, entries) // XMap

// Construct a new Map or Object having the same class as an existing object
mo.reconstruct(obj, entries) // {hello: 'world'}
mo.reconstruct(map, entries) // Map
mo.reconstruct(new XMap(), entries) // XMap
```

### Entries, Keys, and Values

The `Map` prototype has `entries()`, `keys()`, and `values()` methods which return iterators, whereas the global functions `Object.entries()`, `Object.keys()`, and `Object.values()` return arrays. The `m-o` module lets you remain consistent regardless of which type you’re dealing with. Use the `entries()`, `keys()`, and `values()` methods if you want iterators, and use `entriesArray()`, `keysArray()`, and `valuesArray()` if you want arrays.

### Accessing Prototype Properties

By default, `has` and `get` treat an object like a dictionary and therefore only access its own properties. If you want to access object prototype properties as well, use the `hasIn` or `getIn` methods instead. (With Maps, behavior is the same regardless of which set of methods you use.)

```javascript
typeof obj.toString // 'function'
typeof mo.get(obj, 'toString') // 'undefined'
typeof mo.getIn(obj, 'toString') // 'function'

mo.has(obj, 'toString') // false
mo.hasIn(obj, 'toString') // true
```

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