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)
mutableCopy.foo = 'bar'
mutableCopy.zoo.lo = 'gy'
mutableCopy.things.push({ useful: true })
mutableCopy.usefulThings = mutableCopy.things.filter(item => item.useful)
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.