lean-redux
Advanced tools
Comparing version 0.0.4 to 1.0.0
@@ -10,2 +10,3 @@ "use strict"; | ||
exports.composeReducers = composeReducers; | ||
exports.update = update; | ||
exports.thunk = thunk; | ||
@@ -64,6 +65,37 @@ exports.connectLean = connectLean; | ||
var plain = {}; | ||
function disableLodashPath(path) { | ||
if (typeof path === "string") { | ||
return [path]; | ||
} | ||
return path; | ||
} | ||
function update() { | ||
var scope = void 0, | ||
update = void 0; | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
if (args.length === 2) { | ||
scope = args[0]; | ||
update = args[1]; | ||
} else { | ||
update = args[0]; | ||
} | ||
return { | ||
type: "LEAN_UPDATE", | ||
scope: scope, | ||
update: update, | ||
withDefaults: pass | ||
}; | ||
} | ||
var pass = function pass(o) { | ||
return o; | ||
}; | ||
var plain = {}; | ||
var withSlash = function withSlash(s) { | ||
@@ -104,6 +136,15 @@ return s ? "/" + s : ""; | ||
if (scope) { | ||
scopedState = _extends({}, (0, _getOr2.default)(plain, scope, fullState), { scope: scope }); | ||
scopedState = _extends({}, (0, _getOr2.default)(plain, disableLodashPath(scope), fullState), { scope: scope }); | ||
} | ||
scopedState = mapState(withDefaults(scopedState)); | ||
// Implement React Redux style advanced performance pattern where | ||
// the mapState can create the mapState function itself | ||
var _state = mapState(withDefaults(scopedState), ownProps); | ||
if (typeof _state === "function") { | ||
// map state generated a new mapState function. Save it | ||
mapState = _state; | ||
// and use it to map the state | ||
_state = mapState(withDefaults(scopedState), ownProps); | ||
} | ||
scopedState = _state; | ||
@@ -137,4 +178,4 @@ // Regenerate handlers only when the scope changes | ||
return function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
@@ -171,3 +212,3 @@ | ||
if (scope) { | ||
return (0, _update2.default)(scope, function (s) { | ||
return (0, _update2.default)(disableLodashPath(scope), function (s) { | ||
return (0, _update4.default)(update, withDefaults(s)); | ||
@@ -174,0 +215,0 @@ }, state); |
{ | ||
"name": "lean-redux", | ||
"version": "0.0.4", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"test": "eslint src/ examples/*.js && jest", | ||
"build": "mkdir -p lib && babel src --ignore __test__ --out-dir lib", | ||
"prepublish": "npm run build" | ||
}, | ||
"files": ["lib"], | ||
"files": [ | ||
"lib" | ||
], | ||
"author": "Esa-Matti Suuronen", | ||
@@ -32,2 +34,3 @@ "license": "MIT", | ||
"redux-logger": "^2.7.0", | ||
"reselect": "^2.5.4", | ||
"webpack": "^1.13.2", | ||
@@ -34,0 +37,0 @@ "webpack-dev-server": "^1.16.2" |
# Lean Redux | ||
# Lean Redux [![Build Status](https://secure.travis-ci.org/epeli/lean-redux.png?branch=master)](http://travis-ci.org/epeli/lean-redux) | ||
Redux without the boilerplate. | ||
[Redux](http://redux.js.org/) without the boilerplate for [React](https://facebook.github.io/react/). | ||
@@ -85,4 +85,6 @@ ## Design goals | ||
### `leanConnect(options: Object)` | ||
Functions exported by the `lean-redux` module. | ||
### `connectLean(options: Object)` | ||
Connects a React component to a Redux store. Like `connect()` in React Redux it | ||
@@ -93,3 +95,6 @@ does not modify the component but returns a new one. | ||
- `scope: string|Array` Scope the component to a part of the state. | ||
- `scope: string|Array` Scope the component to a part of the state. Deep | ||
scopes can be defined with arrays. Works like paths in | ||
[Lodash](https://lodash.com/docs/4.16.4#set). If `scope` is passed as a prop | ||
from the parent component it will override the value defined here. | ||
- `defaultProps: Object` Default values for props that do not exist in the state | ||
@@ -99,22 +104,22 @@ - `mapState(state: Object, ownProps: Object): Object` Just like the `mapStateToProps` in React Redux, but the | ||
implementation is to return the props matching `defaultProps`. | ||
- `handlers: Object|Function` Object of event handler to be passed to the | ||
component as props. Each handler can return an `updates` value which transforms | ||
the part of the state scoped to the component. See below for details. Can be | ||
also a function which is executed when the component is mounted. See examples | ||
for details. | ||
- `handlers: Object|createHandlers(): Object` Object of event handler to be | ||
passed to the component as props. Each handler can return a `LeanUpdate` which | ||
transforms the part of the state scoped to the component. See below for | ||
details. Can be also a function which is used to generated the handlers when | ||
the component is mounted. See examples for details. | ||
#### `updates` values | ||
#### `LeanUpdate` | ||
Handlers can return an `updates` value to transform the state. It can update as | ||
Handlers can return a `LeanUpdate` to transform the state. It can update as | ||
many values as wanted, as deeply as needed. It can either be an object, a | ||
function, or a value. | ||
If `updates` is an object, for each key/value, it will apply the updates | ||
If `LeanUpdate` is an object, for each key/value, it will apply the updates | ||
specified in the value to `state[key]`. | ||
If `updates` is a function, it will call the function with object and return the value. | ||
If `LeanUpdate` is a function, it will call the function with object and return the value. | ||
If `updates` is a value, it will return that value. | ||
If `LeanUpdate` is a value, it will return that value. | ||
`updates` is recursive. You can define deep nested updates. Ex. `{foo: {bar: i | ||
`LeanUpdate` is recursive. You can define deep nested updates. Ex. `{foo: {bar: i | ||
=> i*2}}`. | ||
@@ -127,4 +132,48 @@ | ||
The `updates` implementation is from | ||
Updates made with `LeanUpdate`s are always applied in immutable manner. State is | ||
never mutated in place. | ||
The `LeanUpdate` implementation is from | ||
[updeep](https://github.com/substantial/updeep). | ||
### `thunk(callback(update(update: LeanUpdate), getProps(): Object))` | ||
Lean Redux comes with simple thunk implementation for async state updates. It's | ||
heavily inspired by [Redux Thunk](https://github.com/gaearon/redux-thunk). | ||
Just return a thunk from a handler: | ||
```js | ||
import {connectLean, thunk} from "lean-redux"; | ||
MyComponent = connectLean({ | ||
defaultProps: { | ||
status: "waiting", | ||
}, | ||
handlers: { | ||
asyncLoad() { | ||
return thunk((update, getProps) => { | ||
update({status: "starting"}); | ||
setTimeout(() => { | ||
update({status: "done"}); | ||
}, 1000); | ||
}); | ||
} | ||
}, | ||
})(MyComponent); | ||
``` | ||
The `update` function passed to the thunk callback works like `dispatch()` in | ||
Redux Thunk but instead of dispatching actions you send `LeanUpdate`s (see above). | ||
The `getProps` returns the current props of the component. It's a function | ||
instead of direct prop values because the props can change over the time. | ||
### `update([scope: string|Array], update: LeanUpdate)` | ||
Normal Redux action creator for dispatching LeanUpdates manually. Use it if you | ||
want to use the `LeanUpdates` outside of the components. Ex. from store | ||
directly `store.dispatch(update({foo: "bar"}))` or from Redux Thunk callback or | ||
whatever situation you have. | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14183
164
1
176
0
20