2.1.0 • Published 6 years ago
redux-rubik-reducer v2.1.0
redux-rubik-reducer (R3)
An npm package to get the redux compatible reducer function and action creator for Rubik's cube state handling.
Installation
npm install --save redux-rubik-reducer
Example usage
Basic usage
//Import methods and constants
import {
reducer,
createAction,
initialState,
ActionType
} from "redux-rubik-reducer";
//Initialize state
let state = initialState;
//Create action passing ActionType as parameter
let action = createAction(ActionType.ROTATE_RIGHT);
//Get new state
state = reducer(state, action);
Advanced usage
//Import methods and constants
import {
reducer,
createAction,
actionHandlers,
initialState,
ActionType,
CellType
} from "redux-rubik-reducer";
//Create custom CellType
CellType.Custom = "YOUR_CUSTOM_CELL_TYPE_CONSTANT";
//Create custom state
let customState = [
[
[CellType.BLUE, CellType.RED, CellType.GREEN],
[CellType.YELLOW, CellType.RED, CellType.ORANGE],
[CellType.BLUE, CellType.BLUE, CellType.ORANGE]
],
[
[CellType.YELLOW, CellType.WHITE, CellType.WHITE],
[CellType.RED, CellType.YELLOW, CellType.GREEN],
[CellType.WHITE, CellType.BLUE, CellType.ORANGE]
],
[
[CellType.RED, CellType.ORANGE, CellType.BLUE],
[CellType.WHITE, CellType.ORANGE, CellType.YELLOW],
[CellType.WHITE, CellType.WHITE, CellType.GREEN]
],
[
[CellType.GREEN, CellType.ORANGE, CellType.YELLOW],
[CellType.BLUE, CellType.WHITE, CellType.GREEN],
[CellType.WHITE, CellType.YELLOW, CellType.YELLOW]
],
[
[CellType.RED, CellType.BLUE, CellType.RED],
[CellType.RED, CellType.GREEN, CellType.WHITE],
[CellType.ORANGE, CellType.GREEN, CellType.GREEN]
],
[
[CellType.BLUE, CellType.GREEN, CellType.ORANGE],
[CellType.YELLOW, CellType.BLUE, CellType.RED],
[CellType.YELLOW, CellType.ORANGE, CellType.RED]
]
];
//Add custom ActionType
ActionType.CUSTOM_ACTION = "YOUR_CUSTOM_ACTION_TYPE_CONSTANT";
//Create custom action
let action = createAction(ActionType.CUSTOM_ACTION);
//Use actionHandlers
let changedCustomState = actionHandlers.rotateRight(customState);
//Create custom action handlers
/**
* @param {string[][][]} state
* @returns {string[][][]}
*/
actionHandlers.customHandler = state => {
/*Your code*/
};
//Use it
actionHandlers.customHandler(changedCustomState);
//Customize reducer function
/**
* @param {string[][][]} state
* @param {Action} action
* @returns {string[][][]}
*/
const customReducer = (state, action) => {
//Handle your custom action(s) first
switch (action.type) {
case ActionType.CUSTOM_ACTION:
return actionHandlers.customHandler(state);
//If not custom action handle it with reducer function
default:
return reducer(state, action);
}
};
Contributing
Any contribution you make is greatly appreciated. :)
License
This project is licensed under the terms of the MIT license.