patchable

0.1.3 • Public • Published

Patchable

Functional state made easy.

What?

Patchable provides a simple JS API to handle immutable values.

It is a simple wrapper on top of the amazing library Immer (If you need it's full power, please use it directly!)

In addition to Immer wrappers, Patchable also provides a super simple reference cell with getter, setter and mutator functions.

API

immutable

Creates an immutable value.

const val = immutable([4, 7, 9, 10]);
 
val[1] = 6; // fails (silently)
val[1]; // ==> 7

patched

Takes a value and a function that patches the value, and generates a patched value.

const val = immutable([4, 7, 9, 10]);
 
const val2 = patched(val, (v) => (v[1] = 6));
 
val[1]; // ==> 6

patching

Curried version of patched.

const val = immutable([4, 7, 9, 10]);
 
const valPatcher = patching(val);
 
const val2 = valPatcher(v) => (v[1] = 6));
 
val[1]; // ==> 6

patcher

Takes a patch function and returns a function which you can call with a value to be updated.

const incrementorOfFirstValue = patcher((v) => (v[1] += 1));
 
const val = immutable([4, 7, 9, 10]);
 
const val2 = incrementorOfFirstValue(val);
 
val[1]; // ==> 8

patchRef

Creates a "reference cell" and returns 3 functions to read, write and update the cell value.

const initialValue = { name: "John", age: 21, balance: 250 };
const [getter, setter, mutator] = patchRef(initialValue);
 
getter(); // ==> { name: "John", age: 21, balance: 250 };
 
getter()["balance"]; // ==> 250
 
setter({
  name: "Jane",
  age: 25,
});
 
getter(); // ==> { name: "Jane", age: 25 }
 
mutator((prev) => {
  prev.age = prev.age + 1;
});
 
getter(); // ==> { name: "Jane", age: 26 }

Package Sidebar

Install

npm i patchable

Weekly Downloads

13

Version

0.1.3

License

MIT

Unpacked Size

4.83 kB

Total Files

4

Last publish

Collaborators

  • murat