# muta

> Transactions for object mutations

Latest version **0.5.1-0** (published 2020-10-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install muta
pnpm add muta
yarn add muta
bun add muta
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1-0 |
| Published | 2020-10-01 |
| First published | 2018-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=6 |
| Dependencies | 0 |
| Unpacked size | 20.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Matt Bell |
| Maintainers | mappum |
| Keywords | proxy, mutation, state, commit |

## Links

- npm: https://www.npmjs.com/package/muta
- Repository: https://github.com/mappum/muta
- Homepage: https://github.com/mappum/muta#readme
- Issues: https://github.com/mappum/muta/issues
- npm.io page: https://npm.io/package/muta

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.5.1-0 (latest) — 2020-10-01
- 0.5.0-0 — 2020-10-01
- 0.4.0 — 2018-10-01
- 0.3.1 — 2018-10-01
- 0.3.0 — 2018-09-05
- 0.2.7 — 2018-08-28
- 0.2.6 — 2018-08-28
- 0.2.5 — 2018-08-09
- 0.2.4 — 2018-08-09
- 0.2.3 — 2018-08-09
- 0.2.2 — 2018-08-08
- 0.2.1 — 2018-08-08
- 0.2.0 — 2018-08-08
- 0.0.0 — 2018-08-08

## README

# muta

Mutate your objects without mutating your objects

**Overview:**
- A thin wrapper around JS objects/arrays, using the [`Proxy`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) API
- Protects data (similar to cloning), but without memory or CPU costs
- Mutations made to the wrapper can be either committed to the original object or thrown away
- All operations happen in `O(1)`

`muta` lets you wrap an object and make mutations to it, while only keeping in memory the unmodified original object and the mutation "patch". The wrapper can be accessed as if the mutations had made to the original object, and the mutations can be eventually committed to the original object if desired.

This module provides similar functionality to the [`jsondiffpatch`](https://github.com/benjamine/jsondiffpatch) and [`immer`](https://github.com/mweststrate/immer) modules, although it is more efficient for large objects since we build the patch as the data is mutated rather than iterating through all the keys to compare at the end. In other words, `jsondiffpatch` and `immer` are both O(N), but `muta` is O(1).

## Usage
`npm install muta`

```js
let muta = require('muta')

let originalData = { foo: { bar: 123 } }

// `virtualData` appears to be the same as `originalData`,
let virtualData = muta(originalData)

// but changes you make to it don't affect `originalData`
virtualData.foo.bar += 1
console.log(originalData) // { foo: { bar: 123 } }
console.log(virtualData) // { foo: { bar: 124 } }

// view the mutations with muta.getPatch
console.log(muta.getPatch(virtualData)) // { foo: { [Symbol(assign)]: { bar: 124 } } }

// if you want to apply the changes,
// call muta.commit on the wrapper
muta.commit(virtualData)
console.log(originalData) // { foo: { bar: 124 } }
```

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