Socket
Socket
Sign inDemoInstall

muta

Package Overview
Dependencies
0
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    muta

Transactions for object mutations


Version published
Weekly downloads
13
Maintainers
1
Install size
21.7 kB
Created
Weekly downloads
 

Readme

Source

muta

Mutate your objects without mutating your objects

Overview:

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

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

Keywords

FAQs

Last updated on 01 Oct 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc