update-in
Persistent functional object updates on vanilla js data structures (wraps react-addons-update)
Quick Examples
; const val = a: b: 0 c: 2 xs: 1 2; // => {a: {b: 1, c: 2}, xs: [1, 2]} // => {a: {b: 10, c: 2}, xs: [1, 2]} let args; // => {a: {b: 6, c: 2}, xs: [1, 2]} // => {a: {b: 99, c: 2}, xs: [1, 2]} // => {x: 1, y: 2, z: 2} // => {a: {b: 0, c: 99, d: 99}, xs: [1, 2]} // => {a: {b: 0, c: 2}, xs: [1, 2, 3]} // => {a: {b: 0, c: 2}, xs: [1, 2, 99]} // => {a: {b: 0, c: 2}, xs: [0, 1, 2]} // => {a: {b: 0, c: 2}, xs: [1, 20]} // => {a: {b: 0, c: 99, d: 99}, xs: [6,5,2,99,99]} ; // => {b: 1, c: 2}; // => {b: 5, c: 6}; // => {b: 0, c: 2, d: 4}; // => {b: 0, c: 2, d: 4, e: 6} // => Error('assoc expects an even number of arguments') ; // => [3, 2]; // => [3, 4]; // => [1, 2, 3] // => Error('assoc expects an even number of arguments') // => TypeError('assoc expects only integer keys') // => RangeError('assoc expects only numeric keys in the range [0, array.length]') // => RangeError('assoc expects only numeric keys in the range [0, array.length]') const collections = object: foo: 1 bar: 2 baz: 3 array: 1 2 3 4 5 6 7; // => {foo: 1, baz: 3} // => {bar: 2} // => [1, 3, 4, 5, 6, 7] // => [1, 2, 6, 7] // => [1, 3, 5, 7]
These combinators use structure sharing to preserve ===
for unchanged nodes, structure sharing is provided by react-addons-update. As of React 0.14, react-addons-update requires all of React as a peer dependency.
Bigger example
We can implement cursors in very few lines with updateIn
, see the examples subdirectory.