1.0.3 • Published 7 years ago

@jkempema/replace-state v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

replace-state

function that allows replacing sections of an object graph

Usage

Replace using a path of properties

import replaceState from 'replace-state';

const state = {
    p1: {
        p2: 'some-value'
    }
};

const newState = replaceState( state, 'p1.p2', 'new-value' );

Replace using an array of properties

import replaceState from 'replace-state';

const state = {
    p1: {
        p2: 'some-value'
    }
};

const newState = replaceState( state, [ 'p1', 'p2' ], 'new-value' );

Replace when the value is an array

import replaceState from 'replace-state';

const state = {
    p1: {
        p2: [ {
            id: 1,
            value: 'some-value'
        }, {
            id: 2,
            value: 'some-other-value'
        } ]
    }
};

const newState = replaceState( state, [ 'p1', ( item ) => item.id === 1 ],  'new-value' );

Replace with a function

import replaceState from 'replace-state';

const state = {
    p1: {
        p2: 'some-value'
    }
};

const newState = replaceState( state, 'p1.p2', ( p2 ) => 'new-value' );
1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago