inflate-object-spread
v0.0.4
Published
Inflate objects that are written with keys as string paths: `{ 'foo.bar': 'baz' }` into `{ foo: { bar: 'baz' } }`
Downloads
5
Maintainers
Readme
Inflate Object Spread
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 } }
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 }