Skip to content

alber70g/inflate-object-spread

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inflate Object Spread

Greenkeeper badge

The library exposes one function inflate(obj: Object, newObj: Object) => Object.

Example:

const oldState = { foo: { bar: 0 } };
const increment = (state) => inflate(state, { 'foo.bar': state.foo.bar + 1 });
const newState = increment(oldState); // { foo: { bar: 1 } }
increment(newState); // { foo: { bar: 2 } }

Travis CI Codecov npm

Installation & Usage

You can import the mixin and use it in the app like so:

import inflate from 'inflate-object-spread';
// umd
// const inflate = inflateObjectSpread;
// pre es6
// var inflate = inflateObjectSpread.inflate;

Deep object with spread

Set an the properties of a deep path, while retaining the other properties. This means that you can send an object, and it's properties will be set instead of the new object replaces the existing one

setName: (state, { name, email }) =>
  inflate(state, { '...login': { name, email })

before

{ "login": { "prop": "value"  } }

after

{ "login": { "prop": "value", "name": "name", "email": "email" } }

Author notes

Further improvements:

  • array manipulation with index (possible api's)
    • { 'app.counters.0.value': 10 }
    • { 'app.counters[0].value': 10 }
    • { 'app.counters.[0].value': 10 }

About

Inflate objects that are written with keys as string paths: `{ 'foo.bar': 'baz' }` into `{ foo: { bar: 'baz' } }`

Resources

License

Stars

Watchers

Forks

Packages

No packages published