0.0.3 • Published 3 years ago
immutalize v0.0.3
immutalize
A simple function to make objects immutable using ES6 proxies. YMMV when using it on objects with functions.
Usage
Install
npm install immutalize
yarn add immutalize
Use
import { immutalize, remutalize } from 'immutalize'
const x = { a: 'a', b: { c: 0 }}
const immut_x = immutalize(x)
x.a = 'b' // Will throw an error
x.b.c++ // Will throw an error
const remut_x = remutalize(immut_x)
x.a = 'b' // No error
x.b.c++ // No error
How it works
This library uses a very simple concept. The immutable function recursively and lazily masks the original object in an ES6 proxy that traps any attempt to modify it. The remutable function recusively and lazily destructures the provided object which strips any proxying off of it.
In cases where there are functions on the objects, if they access the this
property, they must be bound (or the this context will be lost).