Payload-Reducer
Something like lodash.merge() (a recursive version of
Object.assign()
) that specially designed for roudex. It especially supports functions to make it possible to transform properties based on them previous states/values.
Installation
npm i @rahazad/payload-reducer
or using yarn
:
yarn add @rahazad/payload-reducer
Usage
import payloadReducer from '@rahazad/payload-reducer'
const srcState = {
a: 'a',
n: 10,
v: {c: 'c'}
}
const payloads = [
{
a: 'A',
v: {c: 'C'}
},
{
n: n => n * 2
},
]
const {newState} = payloadReducer(srcState, ...payloads)
assert.deepStrictEqual(newState, { // import assert from 'assert' // https://nodejs.org/api/assert.html
a: 'A',
n: 20,
v: {c: 'C'}
})
License
MIT © Mir-Ismaili