xyjax-update-immutabledeprecated

Immutable objects deep-clone update made easy to use

Usage no npm install needed!

<script type="module">
  import xyjaxUpdateImmutable from 'https://cdn.skypack.dev/xyjax-update-immutable';
</script>

README

npm npm bundle size npm NPM

About

xyjax-update-immutable is the standalone and lightweight system which provides functional-programming-like deep-cloned objects copy/merge (which is extremely useful for Redux e.g.). This package was originally developed for Xyjax NPM package.

Methods

There is one method you can import:

1. updateImmutable(path, changes)(sourceObject)

Returns changed deep-copy of given object, source object won't be affected.

Arguments description:

  • path is the string representation of path ('a.b.c' for example) must be changed or created in object copy will be returned
  • changes is the payload will be applied to changed source object copy (it can be primitive, object or calculating field function - see examples below)
  • sourceObject is the object which changed copy will be returned

Usage example

Click here to take a look at RunKit + NPM embed example.

import { updateImmutable } from 'xyjax-update-immutable'

var target_1 = {}
//changes can be primitives...
var target_2 = updateImmutable('a.b', 4)(target_1)
//...objects...
var target_3 = updateImmutable('c', {d: 5})(target_2)
//...or calculating fields (with default parameters in case of field is not found)
var target_4 = updateImmutable('c.d', (x = 0) => { return x * 2 })(target_3)
//any of this approaches work at any nested level (calculating field as example)
var target_5 = updateImmutable('a.b.c.d.e.f.g', (x = 5) => { return x * Math.random() })(target_1)

console.log(target_1) // {}
console.log(target_2) // {a: {b: 4}}
console.log(target_3) // {a: {b: 4}, c: {d: 5}}
console.log(target_4) // {a: {b: 4}, c: {d: 10}}
console.log(target_5) // {a: {b: {c: {d: {e: {f: {g: 3.4901662472336725}}}}}}}