moggy

0.1.1 • Public • Published
Transmogrify

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]

Package Sidebar

Install

npm i moggy

Weekly Downloads

1

Version

0.1.1

License

GPL-3.0

Last publish

Collaborators

  • wildhoney