New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

as-mutable

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

as-mutable

Ergonomic way to mutate data without touching the original source

0.1.1
latest
Source
npm
Version published
Maintainers
1
Created
Source

as-mutable

as-mutable offers an ergonomic way to mutate data without touching the original source. It's a handy helper at circumstance, say when you work with redux, where you need to mutate native js data struct (object/array) while keeping the original source immutable.

Usage

import { asMutable, getValue } from 'as-mutable'

const mutableCopy = asMutable(source)
// do whatever you want to the `mutableCopy`,
// mutate it! it's ok as long as you keep its reference.

// own prop:
mutableCopy.foo = 'bar'
// deep nested object
mutableCopy.zoo.lo = 'gy'
// deep nested array
mutableCopy.things.push({ useful: true })
// new prop
mutableCopy.usefulThings = mutableCopy.things.filter(item => item.useful)

// when you're done, call `getValue()` to get the result.
const result = getValue(mutableCopy)

Caveats

  • In order to work, as-mutable requires present of ES Proxy in the runtime.
  • It's designed for plain js object and array, object with custom prototype should be fine most of the time, supposing you don't do crazy things.
  • No support for ES Map/Set/WeekMap. These data structs are meant to be mutable at source, don't see the point to keep them immutable.

Keywords

immutable

FAQs

Package last updated on 10 Sep 2018

Did you know?

Socket

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