Same Old
Simple immutability for JavaScript objects/arrays/functions using Proxies. This is based on the excellent article here.
Libraries like Immutable.js and others are awesome, but I wanted something simple and lightweight (minified and gzipped it's less than 1k) to just give me immutability.
Install
Just run npm install same-old
.
If you want to work on it, you can clone the repo and run the tests with npm run test
.
Usage
const sameOld = ; /*Objects*/const immutable = ; immutablecannot = 'change it'; // ThrowsObject; // Throws const deep = ; deeponnested = 'change'; // Throws /*Arrays*/const immutableArray = ; immutableArray; // ThrowsimmutableArray; // Throwsconsole; // [5, 4, 3] const derived = immutableArray; // [5, 4, 3, 2] derived; // Throwsderived; // Throwsconsole; // [5, 4, 3, 2] /*Functions*/const immutableFunc = ;const returnedArray = ;returnedArray; // Throws
Support
This uses Proxy
and WeakMap
so basically don't even bother on IE or anything old.