Skip to content

Govern your state. Utility library for change control in frameworks such as redux

License

Notifications You must be signed in to change notification settings

rrichardsonv/sovereignty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status

Sovereignty

Utility library for managing state changes in frameworks such as redux

Setup

$npm i sovereignty

To run the test file

$npm run test

Examples

At the top of your reducer (Note: SovereignState is for object primitive states it will not work for other data structures and this library is not for you)

import SovereignState from 'sovereignty';

const initialState = {
  data: [],
  currentContactId: '',
  isFetching: false,
}

const storeUtils = new SovereignState(initialState);

Note: The keys in your initial state and the name of the constructor for the values in your initial state will be used for validations. If you need a field without type validations your initial state should have the initial value be null;

SovereignState.prototype.update and SovereignState.prototype.mixedUpdate function similarly to Object.assign (MDN).

When you simply want to update state use update():

handleActions({
  GET_CONTACTS: (state, action) => {
    const newData = action.payload.data;
    
    return storeUtils.update(state, { data: newData });
  },

When you're concerned about falsey values use mixedUpdate():

handleActions({
  GET_ACCOUNT: (state, action) => {
    const { contacts, currentContact } = action.payload.data.contacts;

    return storeUtils.mixedUpdate(
      state,
      { data: contacts },
      currentContact && { currendContactId: currentContact.id },
    );
  },

About

Govern your state. Utility library for change control in frameworks such as redux

Resources

License

Stars

Watchers

Forks

Packages

No packages published