Skip to content

Commit

Permalink
Readme updates, updated defaultPlugin, added immutablyCopyValue tool,…
Browse files Browse the repository at this point in the history
… small cody style fixes
  • Loading branch information
Lukasz-pluszczewski committed Sep 18, 2017
1 parent 30a617e commit e902941
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 31 deletions.
4 changes: 0 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ All pull requests are appreciated as long as the code is well tested, documented
You may grab a thing from todo (see below) or write plugins for your use-case.
## Todo
* make readme great again:
* document config (not a big thing ;) )
* add basic usage tutorial
* add plugins tutorial
* add tests to default plugin
* write action definitions validation (by default only checking for 'type' field, but make it plugin enabled)
* add more plugins:
Expand Down
5 changes: 3 additions & 2 deletions docs/defaultPlugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ And it's just the beginning! :)
- **initialValue** *any* value that is going to be used in initial state in *targetPath*
- **defaultValue** *any* value that is going to be used when not provided in the action
- **result**: *string|array<{sourcePath: string, targetPath: string, initialValue: any, defaultValue, any, result: string}>* type of the result; if 'list' then initialValue will be emtpy string by default; if 'entity' default initial value will be null;
- **sourcePath** *string* path from action the data is going to be taken from, can be nested e.g. 'payload.someField.someSubField'
- **targetPath** *string* path in reducer's state, the data is going to be saved in, can be nested e.g. 'someField.someSubField'
- **sourcePath** *string|function(action)* path from action the data is going to be taken from, can be nested e.g. 'payload.someField.someSubField', or function that returns that path
- **targetPath** *string|function(action)* path in reducer's state, the data is going to be saved in, can be nested e.g. 'someField.someSubField' or function that returns that path
- **initialValue** *any* value that is going to be used in initial state in *targetPath*
- **defaultValue** *any* value that is going to be used when not provided in the action
- **result** *string* when initialValue not provided, it will be based on *result*; if 'list' the initialValue is going to be empty array; if 'entity' the initialValue is going to be null
- **value** *function(action, currentValue)|any* function that gets an action object and current state value (based on targetPath) and returns value to be saved, or hardcoded value to be set
2 changes: 1 addition & 1 deletion docs/gettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Change this code:
import { createStore, combineReducers } from 'redux';
import myCustomReducers from 'myCustomReducers.js';

const store = createStore(combineReducer(myCustomRedcuers));
const store = createStore(combineReducer(myCustomReducers));
```

to this:
Expand Down
16 changes: 12 additions & 4 deletions src/defaultPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ const createDefaultPlugin = ({ createActionType, immutableSet }, config) => ({
}
) => {
// setting default value if one has not been provided and defaultValue is defined
if (!(defaultValue instanceof hasNotBeenDefined) && _.isUndefined(_.get(action, sourcePath))) {
accu[targetPath] = defaultValue;
if (
!(defaultValue instanceof hasNotBeenDefined)
&& _.isUndefined(_.get(action, sourcePath))
&& value instanceof hasNotBeenDefined
) {
accu[targetPath] = _.isFunction(defaultValue) ? defaultValue(action) : defaultValue;

// setting value based on sourcePath
} else if (value instanceof hasNotBeenDefined) {
accu[targetPath] = _.get(action, sourcePath);
accu[targetPath] = _.get(action, _.isFunction(sourcePath) ? sourcePath(action) : sourcePath);

// setting value calculated by a 'value' function
} else if (_.isFunction(value)) {
Expand All @@ -62,7 +66,11 @@ const createDefaultPlugin = ({ createActionType, immutableSet }, config) => ({
const resultName = actionDefinition.resultName || actionName;

// setting default value if one has not been provided and defaultValue is defined
if (!_.has(actionDefinition, 'defaultValue') && _.isUndefined(action.payload)) {
if (
_.has(actionDefinition, 'defaultValue')
&& _.isUndefined(action.payload)
&& _.has()
) {
resultsAssignments = { [resultName]: actionDefinition.defaultValue };
} else if (!_.has(actionDefinition, 'value')) {
resultsAssignments = { [resultName]: action.payload };
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import _ from 'lodash';
import { combineReducers } from 'redux';
import { immutableSet, createActionType, chainReducers, mergePlugins, checkConflicts } from './tools';
import {
immutableSet,
createActionType,
chainReducers,
mergePlugins,
checkConflicts,
immutablyCopyValue,
} from './tools';
import createDefaultPlugin from './defaultPlugin';

const defaultConfig = {
Expand All @@ -24,6 +31,7 @@ export const tools = {
chainReducers,
mergePlugins,
checkConflicts,
immutablyCopyValue,
};

const createReduxBreezeInstance = (actionDefinitions, userConfig = defaultConfig, ...plugins) => {
Expand Down
12 changes: 12 additions & 0 deletions src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,15 @@ export const immutableSet = (object, path, value = null, delimiter = '.') => {
[pathSplit[0]]: immutableSet(childObject, _.tail(pathSplit).join(delimiter), value, delimiter),
};
};

/**
* Copy value from fromPath in fromObject and create new object from toObject with that value saved in toPath
* @param {object} fromObject object which you get the value from
* @param {string} fromPath path in fromObject where you get tha value from
* @param {object} toObject object where you save the value to
* @param {string} toPath path in toObject where you save the value
* @return {object} new object created from values in toObject
*/
export const immutablyCopyValue = (fromObject, fromPath, toObject, toPath) => {
return immutableSet(toObject, toPath, _.get(fromObject, fromPath));
};
32 changes: 13 additions & 19 deletions test/reduxBreeze.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ const testPlugin = ({ createActionType, immutableSet }) => ({
*/
actionAdapter: {
test(definition, actionName) {
return (params) => {
return {
params,
somethingMore: 'someMoreValue',
};
};
return params => ({
params,
somethingMore: 'someMoreValue',
});
},
},

Expand Down Expand Up @@ -57,9 +55,8 @@ const testPlugin = ({ createActionType, immutableSet }) => ({
});



describe('reduxBreeze', () => {
let nativeConsoleWarn = console.warn;
const nativeConsoleWarn = console.warn;
before(() => {
console.warn = () => {};
});
Expand Down Expand Up @@ -197,9 +194,7 @@ describe('reduxBreeze', () => {
const plugin1 = () => plugin1Value;
const plugin2 = () => plugin2Value;

const mapActionTypes = (actionType, pluginName, adapterType) => {
return pluginName === 'plugin2' ? `plugin2${actionType}` : actionType;
};
const mapActionTypes = (actionType, pluginName, adapterType) => pluginName === 'plugin2' ? `plugin2${actionType}` : actionType;

const reduxBreezeInstance = createReduxBreezeInstance({}, { useDefaultPlugin: false, mapActionTypes }, plugin1, plugin2);
expect(reduxBreezeInstance.getMergedPlugin()).to.nested.include({
Expand All @@ -224,7 +219,7 @@ describe('reduxBreeze', () => {
someOtherAction: {
type: 'test',
},
}
},
};
const expectedInitialState = {
exampleReducer: {
Expand All @@ -234,7 +229,7 @@ describe('reduxBreeze', () => {
exampleReducer2: {
someOtherAction: null,
field: 1,
}
},
};

const reduxBreezeInstance = createReduxBreezeInstance(actionDefinitions, { useDefaultPlugin: false }, testPlugin);
Expand All @@ -249,11 +244,10 @@ describe('reduxBreeze', () => {
exampleReducer2: {
someOtherAction: 'SOMETHING',
field: 2,
}
},
});
});
it('should merge generated reducers with custom ones with combineReducer function with argument', () => {

const actionDefinitions = {
exampleReducer: {
someAction: {
Expand All @@ -264,7 +258,7 @@ describe('reduxBreeze', () => {
someOtherAction: {
type: 'test',
},
}
},
};
const customInitialState = {
customField: 1,
Expand All @@ -278,7 +272,7 @@ describe('reduxBreeze', () => {
exampleReducer2: {
someOtherAction: null,
field: 1,
}
},
};

const customReducer = (state = customInitialState, action) => {
Expand Down Expand Up @@ -306,7 +300,7 @@ describe('reduxBreeze', () => {
exampleReducer2: {
someOtherAction: 'SOMETHING',
field: 2,
}
},
});
});
it('should throw an error when provided with action that cannot be handled by applied plugins', () => {
Expand All @@ -320,7 +314,7 @@ describe('reduxBreeze', () => {
unhandledAction: {
type: 'unhandled',
},
}
},
};

const reduxBreezeInstance = createReduxBreezeInstance(actionDefinitions, { useDefaultPlugin: false }, testPlugin);
Expand Down

0 comments on commit e902941

Please sign in to comment.