Comparing version 0.0.6 to 0.0.7
@@ -33,4 +33,3 @@ # Part 2 - Nesting | ||
import { arrayInsertReducer, arrayDeleteReducer } from 'remerge/lib/arrayReducers' | ||
import { objectInsertReducer, objectDeleteReducer } from 'remerge/lib/objectReducers' | ||
import { objectUpdateReducer } from 'remerge/lib/updateReducers' | ||
import { objectInsertReducer, objectDeleteReducer, objectUpdateReducer } from 'remerge/lib/objectReducers' | ||
@@ -37,0 +36,0 @@ const reducer = merge({ |
# Debugging Remerge | ||
>This section discusses how to debug when working with Remerge. | ||
For debugging purposes, the `merge` function also accepts a second optional argument. If set to `true`, the reducer will log out the path that actions take through the state tree. For example: | ||
@@ -51,2 +53,4 @@ | ||
[remerge] Executing action at leaf node: delete | ||
``` | ||
``` | ||
Continue to [Part 6 - Working with third party reducers](./6-third-party-reducers.md). |
@@ -7,5 +7,4 @@ # Convenience Reducers | ||
import { arrayInsertReducer, arrayDeleteReducer } from 'remerge/lib/arrayReducers' | ||
import { objectInsertReducer, objectDeleteReducer } from 'remerge/lib/objectReducers' | ||
import { objectInsertReducer, objectDeleteReducer, objectUpdateReducer } from 'remerge/lib/objectReducers' | ||
import { mapInsertReducer, mapDeleteReducer } from 'remerge/lib/mapReducers' | ||
import { objectUpdateReducer } from 'remerge/lib/updateReducers' | ||
``` | ||
@@ -70,8 +69,2 @@ | ||
### Map Reducers | ||
Map reducers - `mapInsertReducer` and `mapDeleteReducer` - are used exactly the same as their object reducer counterparts. | ||
### Update Reducers | ||
#### objectUpdateReducer | ||
@@ -86,2 +79,6 @@ | ||
} | ||
``` | ||
``` | ||
### Map Reducers | ||
Map reducers - `mapInsertReducer` and `mapDeleteReducer` - are used exactly the same as their object reducer counterparts. |
@@ -15,6 +15,10 @@ 'use strict'; | ||
var _lodash = require('lodash'); | ||
var _lodash = require('lodash.isfunction'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _lodash3 = require('lodash.clone'); | ||
var _lodash4 = _interopRequireDefault(_lodash3); | ||
var _utils = require('./utils'); | ||
@@ -25,2 +29,3 @@ | ||
var collectionRegex = /^\$(.+)/; | ||
var legacyRegex = /^__(.+)__$/; | ||
@@ -41,2 +46,12 @@ var merge = function merge(map) { | ||
var _getLegacyKey = function _getLegacyKey(key) { | ||
// this regex tests if the key is of the form __abcd__ | ||
var captureLegacy = legacyRegex.exec(key); | ||
if (captureLegacy) { | ||
return captureLegacy[1]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
var _preprocess = function _preprocess(map) { | ||
@@ -54,7 +69,8 @@ var newMap = {}; | ||
var isFunction = _lodash2.default.isFunction(map[key]); | ||
var valueIsFunction = (0, _lodash2.default)(map[key]); | ||
newMap[key.replace(_getAccessorKey(key), "")] = { | ||
isLeaf: isFunction, | ||
isLeaf: valueIsFunction, | ||
accessorKeyName: _getAccessorKey(key), | ||
child: isFunction ? map[key] : _preprocess(map[key]) | ||
legacyKeyName: _getLegacyKey(key), | ||
child: valueIsFunction ? map[key] : _preprocess(map[key]) | ||
}; | ||
@@ -69,10 +85,19 @@ } | ||
return undefined; | ||
} else if (_lodash2.default.isFunction(map)) { | ||
} else if ((0, _lodash2.default)(map)) { | ||
return undefined; | ||
} else if (map['_'] !== undefined) { | ||
return map['_']; | ||
} | ||
var newMap = {}; | ||
var newMap = map['_'] || {}; | ||
var changed = map['_'] !== undefined; | ||
for (var key in map) { | ||
if (key === '_') { | ||
continue; | ||
} | ||
if (_getLegacyKey(key)) { | ||
newMap[_getLegacyKey(key)] = map[key](undefined, {}); | ||
changed = true; | ||
} | ||
// to avoid adding keys to collections | ||
if (!_getAccessorKey(key)) { | ||
@@ -82,6 +107,8 @@ var result = _initial(map[key]); | ||
newMap[key] = result; | ||
changed = true; | ||
} | ||
} | ||
} | ||
return Object.keys(newMap).length > 0 ? newMap : null; | ||
return changed ? newMap : null; | ||
}; | ||
@@ -91,3 +118,3 @@ | ||
var currentPath = action.type.split('.', 1)[0]; | ||
var newState = _lodash2.default.clone(state); | ||
var newState = (0, _lodash4.default)(state); | ||
var foundPath = false; | ||
@@ -102,10 +129,16 @@ | ||
var path = _step.value; | ||
var _map$path = _map[path]; | ||
var accessorKeyName = _map$path.accessorKeyName; | ||
var isLeaf = _map$path.isLeaf; | ||
var child = _map$path.child; | ||
var legacyKeyName = _map$path.legacyKeyName; | ||
if (path === currentPath) { | ||
if (legacyKeyName) { | ||
foundPath = true; | ||
var _map$path = _map[path]; | ||
var accessorKeyName = _map$path.accessorKeyName; | ||
var isLeaf = _map$path.isLeaf; | ||
var child = _map$path.child; | ||
(0, _utils.consoleSuccess)('Executing legacy reducer ' + legacyKeyName, debugMode); | ||
var smallerState = (0, _utils.getCollectionElement)(newState, legacyKeyName); | ||
(0, _utils.setCollectionElement)(newState, legacyKeyName, child(smallerState, action)); | ||
} else if (path === currentPath) { | ||
foundPath = true; | ||
@@ -122,3 +155,3 @@ if (isLeaf) { | ||
(0, _utils.consoleSuccess)('Navigating collection node: ' + currentPath, debugMode); | ||
var newCollection = _lodash2.default.clone((0, _utils.getCollectionElement)(newState, path)); | ||
var newCollection = (0, _lodash4.default)((0, _utils.getCollectionElement)(newState, path)); | ||
var smallerMap = child.$.child; | ||
@@ -125,0 +158,0 @@ var smallerState = (0, _utils.getCollectionElement)(newCollection, collectionKey); |
@@ -8,3 +8,3 @@ 'use strict'; | ||
var _lodash = require('lodash'); | ||
var _lodash = require('lodash.clone'); | ||
@@ -19,3 +19,3 @@ var _lodash2 = _interopRequireDefault(_lodash); | ||
var newState = _lodash2.default.clone(state); | ||
var newState = (0, _lodash2.default)(state); | ||
newState.set(action.insertKey, action.data); | ||
@@ -26,5 +26,5 @@ return newState; | ||
var mapDeleteReducer = exports.mapDeleteReducer = function mapDeleteReducer(state, action) { | ||
var newState = _lodash2.default.clone(state); | ||
var newState = (0, _lodash2.default)(state); | ||
newState.delete(action.deleteKey); | ||
return newState; | ||
}; |
@@ -31,2 +31,10 @@ "use strict"; | ||
return newState; | ||
}; | ||
/** | ||
* Reducer that updates an object | ||
* @param {any} action.data The element to update | ||
*/ | ||
var objectUpdateReducer = exports.objectUpdateReducer = function objectUpdateReducer(state, action) { | ||
return _extends({}, state, action.data); | ||
}; |
{ | ||
"name": "remerge", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "State simplified.", | ||
@@ -8,3 +8,4 @@ "main": "lib/index.js", | ||
"test": "ava --tap -v | tap-nyan", | ||
"prepublish": "rimraf lib && babel src --out-dir lib" | ||
"prepublish": "rimraf lib && babel src --out-dir lib", | ||
"build:script": "webpack" | ||
}, | ||
@@ -36,2 +37,3 @@ "repository": { | ||
"babel-core": "^6.5.2", | ||
"babel-loader": "^6.2.4", | ||
"babel-preset-es2015": "^6.5.0", | ||
@@ -42,6 +44,8 @@ "babel-preset-stage-2": "^6.5.0", | ||
"rimraf": "^2.5.2", | ||
"tap-nyan": "0.0.2" | ||
"tap-nyan": "0.0.2", | ||
"webpack": "^1.12.14" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.5.1" | ||
"lodash.clone": "^4.3.1", | ||
"lodash.isfunction": "^3.0.8" | ||
}, | ||
@@ -48,0 +52,0 @@ "ava": { |
@@ -21,2 +21,6 @@ # Remerge | ||
[Part 6 - Working with third party reducers](docs/6-third-party-reducers.md) | ||
[Part 7 - Standalone usage](docs/7-standalone-usage.md) | ||
#### Examples | ||
@@ -26,4 +30,10 @@ | ||
[React Router Redux basic example, refactored with Remerge](examples/react-router-redux) | ||
This example shows how Remerge can be used in conjunction with libraries that expose reducers to hook into Redux state, such as React Router Redux and Redux Form. | ||
## Getting started | ||
>Remerge is also distributed as a standalone script. See [Part 7 - Standalone usage](docs/7-standalone-usage.md) for more detail. | ||
**Install** | ||
@@ -196,2 +206,4 @@ | ||
**Update**: The `examples` folder has been updated to include [an example of using Remerge together with React Router Redux](examples/react-router-redux). | ||
## Tests | ||
@@ -198,0 +210,0 @@ |
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
68877
20
368
212
2
11
+ Addedlodash.clone@^4.3.1
+ Addedlodash.isfunction@^3.0.8
+ Addedlodash.clone@4.5.0(transitive)
+ Addedlodash.isfunction@3.0.9(transitive)
- Removedlodash@^4.5.1
- Removedlodash@4.17.21(transitive)