Skip to content

Commit

Permalink
Added readme,fixed failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz-pluszczewski committed Jan 22, 2018
1 parent 037e587 commit 9303488
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 13 deletions.
29 changes: 29 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,32 @@
createActionType('myFancyName', 'success'); // MY_FANCY_NAME_SUCCESS
createActionType('myFancyName', '', 'blah'); // BLAH_MY_FANCY_NAME
```

#### `connect(mapStateToProps, mapDispatchToProps, mergeProps, options)`
- `import { connect } from 'redux-breeze'`
- wrapper for react-redux connect that transform mapStateToProps plain object to standard mapStateToProps function
- arguments:
- **mapStateToProps**: *function|object* standard function or object of paths in state or functions (see example below)
- ... all other arguments exactly the same as in [connect](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options)
- example:
```javascript
// standard connect
connect(
state => ({
foo: state.foo.value,
bar: state.foo.value,
noValue: state.noValue && state.noValue.value,
baz: state.baz.value,
})
)(MyComponent);

// redux-breeze connect
connect(
{
foo: 'state.foo.value',
bar: 'bar.value', // you can omit 'state.' from the begining of path
noValue: 'noValue.value' // no error thrown, if noValue is undefined this will return undefined as well
baz: state => state.baz.value, // you can also use selectors by passing a function that accepts state as argument
}
)(MyComponent);
```
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
#### 2.0.0
- Added "connect" function
- All tools are now named exports

### 2.0.1
- Added docs for "connect" function

### 2.0.2
- Fixed failing tests
55 changes: 48 additions & 7 deletions lib/reduxBreeze.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/reduxBreeze.js.map

Large diffs are not rendered by default.

43 changes: 40 additions & 3 deletions lib/reduxBreeze.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'lodash';
import { combineReducers } from 'redux';
import { set } from 'perfect-immutable';
import { connect } from 'react-redux';

var babelHelpers = {};

Expand Down Expand Up @@ -216,6 +217,42 @@ var chainReducers = function chainReducers(rawReducers) {
};
};

/**
* Converts object of paths to traditional mapStateToProps function
* @param {object|function} mapState object of paths or traditional mapStateToProps function
* @return {function} mapStateToProps function
*/
var getNewMapState = function getNewMapState(mapState) {
if (_.isPlainObject(mapState)) {
return function (state) {
return _.mapValues(mapState, function (value, key) {
if (_.isString(value)) {
return _.get(state, value.replace(/^state\./, ''));
}
if (_.isFunction(value)) {
return value(state);
}
throw new Error('When using plain object in "connect", values must be either strings (paths to values in state) or functions (selectors). Check value in ' + key + ' field');
});
};
}
return mapState;
};

/**
* Works like react-redux connect but allows you to use object of paths as first argument
* @param {object|function} mapState objects of paths or traditional mapStateToProps function
* @param {array} rest rest of connect arguments
* @return {function} connect HOC
*/
var connect$1 = function connect$$1(mapState) {
for (var _len = arguments.length, rest = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
rest[_key - 1] = arguments[_key];
}

return connect.apply(undefined, [getNewMapState(mapState)].concat(rest));
};

/**
* Checks if there are conflicts in plugins in given adapterType (conflict = two plugins handling same actionType)
* @param {array} plugins list of plugins
Expand Down Expand Up @@ -422,15 +459,15 @@ var defaultConfig = {
}
};

// still exported for backwards compatibility and testing purposes
var tools = {
createActionType: createActionType,
chainReducers: chainReducers,
connect: connect$1,
mergePlugins: mergePlugins,
checkConflicts: checkConflicts
};

var defaultPlugin = createDefaultPlugin;

var createReduxBreezeInstance = function createReduxBreezeInstance(actionDefinitions) {
for (var _len = arguments.length, plugins = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
plugins[_key - 2] = arguments[_key];
Expand Down Expand Up @@ -516,6 +553,6 @@ var createReduxBreezeInstance = function createReduxBreezeInstance(actionDefinit
};
};

export { tools, defaultPlugin };
export { createActionType, chainReducers, connect$1 as connect, mergePlugins, checkConflicts, createDefaultPlugin as defaultPlugin, tools };
export default createReduxBreezeInstance;
//# sourceMappingURL=reduxBreeze.mjs.map
2 changes: 1 addition & 1 deletion lib/reduxBreeze.mjs.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "redux-breeze",
"version": "2.0.0",
"version": "2.0.2",
"description": "",
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,6 +48,7 @@
"eslint-watch": "3.1.2",
"mocha": "3.5.0",
"nyc": "11.4.1",
"react": "^16.2.0",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"rimraf": "2.6.1",
Expand All @@ -62,6 +63,7 @@
},
"peerDependencies": {
"react-redux": "^5.x.x",
"react": "^16.x.x",
"redux": "^3.x.x"
}
}

0 comments on commit 9303488

Please sign in to comment.