0.0.19 • Published 4 years ago

@mzvonar/setin v0.0.19

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

setIn Build Status Coverage Status npm version

Sets value in object by path and returns copy. Path can be string or array (e.g. 'user', 'profile', 'gender').
If any part of path doesn't exist it is created. Always returns new copy of object.

Installation

npm install @mzvonar/setin

Parameters

setIn(context, path, value, push);
NameDescription
contextObject in which the value will be set
pathMust be Array or String. See usage
valueValue to set in path
pushWhether to push the value to Array. See usage

Usage

import setIn from '@mzvonar/setin';
  
const context = {
    user: {
        profile: {
            gender: 'female'
        }
    }
};
  
const newContext = setIn(context, ['user', 'profile', 'gender'], 'male');

returns:

    {
        user: {
            profile: {
                gender: 'male'
            }
        }
    }
const newContext = setIn(context, ['user', 'profile', 'address', 'country'], 'slovakia');

returns:

    {
        user: {
            profile: {
                gender: 'female',
                address: {
                    country: 'slovakia'
                }
            }
        }
    }

Push

If fourth parameter is true and last item in path is Array value is pushed to the Array

example:

    const context = {
        user: {
            name: 'Mike',
            nicknames: [
                'terminator',
                'maverick'
            ]
        }
    };
      
    const newContext = setIn(context, ['user', 'nickanmes'], 'vincent vega');
    
    console.log(newContext);
    /*
    {
        user: {
            name: 'Mike',
            nicknames: [
                'terminator',
                'maverick',
                'vincent vega'
            ]
        }
    }
    */

mutableSetIn

If you need you can import mutableSetIn, which is exactly the same as setIn, but mutates the original context object without creating copy.

Tests

npm test

0.0.19

4 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago