redecorate
Simple module for reducing immutable nested properties in Redux applications.
Last updated a year ago by wildhoney .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install redecorate 
SYNC missed versions from official npm registry.

Redecorate

Simple module for reducing immutable nested properties in Redux applications.

forthebadge forthebadge forthebadge

Getting Started

import {apply} from 'redecorate';

// ...

const model = apply(state)('name.places', cursor => {
    return [ ...cursor, { name: 'Malta' } ];
});

Redecorate allows you to easily handle nested state in your Redux reducers – even after specialising your reducers using combineReducers.

Helpers

Common patterns are found throughout Redux's reducers, which is why Redecorate provides a handful of helper functions to make the reducing process simpler and more readable for fellow developers.

In the following examples we'll assume we have the following state:

const state = {
    name: {
        first: 'James',
        surname: 'Bradfield';
    },
    age: 46,
    songs: ['A Design For Life', 'Motorcycle Emptiness', 'Generation Terrorists'],
    albums: [
        { name: 'The Holy Bible', released: 1994 },
        { name: 'Everything Must Go', released: 1996 },
        { name: 'This Is My Truth, Tell Me Yours', released: 1998 }
    ]
};

We can use the set helper to modify properties on the age literal:

apply(state)('age', set(47));

Furthermore we're able to able to use the set function for adding properties to the name object:

apply(state)('name.middle', set('Dean'));

Adding an item to the songs is as easy as using the add function — you can pass multiple arguments to add many items onto the array:

apply(state)('songs', add('If You Tolerate This Your Children Will Be Next'));

Similarly you can use the remove function to remove items from the aforementioned array — again you can use the nature of the add and remove multivariate functions to supply more than one item:

apply(state)('songs', remove('Motorcycle Emptiness', 'Generation Terrorists'));

Removing items from an array of objects is slightly more difficult:

apply(state)('albums', remove({ released: 1994 }));

You can also pass multiple arguments to the remove function — however if you want to remove items that are either { released: 1994 } or { name: 'Everything Must Go' } then simply pass multiple arguments to the remove function:

apply(state)('albums', remove({ name: 'Everything Must Go' }, { released: 1994 }));

:bulb: Have an idea for Redecorate's helper functions?

Current Tags

  • 0.2.1                                ...           latest (a year ago)

7 Versions

  • 0.2.1                                ...           a year ago
  • 0.2.0                                ...           a year ago
  • 0.1.4                                ...           2 years ago
  • 0.1.3                                ...           4 years ago
  • 0.1.2                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (0)
None
Dev Dependencies (7)
Dependents (2)

Copyright 2014 - 2017 © taobao.org |