Skip to content

Something like lodash.merge() (a recursive version of Object.assign()) that specially designed for roudex

License

Notifications You must be signed in to change notification settings

Rahazad/graph-reducer.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Graph-Reducer

Something like lodash.merge() (a recursive version of Object.assign()) that specially designed for roudex.

It additionally supports functions (in payload(s) argument(s)) to make it possible to transform properties based on their previous states/values.

It also deeply compares the source state (source object) with the primary result (newState), during its original process and returns an additional value (noTransform) that shows the result of this comparison.

npm (scoped) install size npm
Dependencies Status devDependencies Status
GitHub GitHub forks GitHub stars

Installation

npm i @rahazad/graph-reducer

or using yarn:

yarn add @rahazad/graph-reducer

Usage

import graphReducer from '@rahazad/graph-reducer'

const srcState = {
    a: 'a',
    n: 10,
    v: {c: 'c'}
}

const payloads = [
    {
        a: 'A',
        v: {c: 'C'}
    },
    {
        n: n => n * 2
    },
]

const {newState} = graphReducer(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