moggy
v0.1.1
Published
Miniature ~2kb library that brings immutability to existing prototype functions employing the principle of least astonishment.
Downloads
7
Maintainers
Readme
Miniature ~2kb library that brings immutability to existing prototype functions employing the principle of least astonishment.
Getting Started
Moggy inspects the prototype
of the value and creates an object
that takes the functions from the prototype – it's therefore not easy to say which functions Moggy implements, since ECMAScript functions that mutate the value will respond with an array of [sideEffect, returnValue]
, whereas functions that are already immutable in their nature will respond as-is.
import m from 'moggy';
const a = m([1, 2, 3]);
const b = a.push(4); // [sideEffect = [1, 2, 3, 4], returnValue = 4]
const c = a.pop(); // [sideEffect = [1, 2], returnValue = 3]
console.log(a); // [1, 2, 3]
In such cases it's not always obvious which value you may require – that's why Moggy returns both. In the case of push
you're more likely to sideEffect
, however in pop
you're more likely to require the returnValue
.
Using destructuring it's easy to take what you need – ignoring what you don't need.
import m from 'moggy';
const a = m([1, 2, 3]);
const [b] = a.push(4); // [1, 2, 3, 4]
const [, c] = a.pop(); // [1, 2]
console.log(a); // [1, 2, 3]