Comparing version 0.0.1 to 0.0.2
31
index.js
@@ -19,2 +19,6 @@ 'use strict'; | ||
var _shallowCopy = require('shallow-copy'); | ||
var _shallowCopy2 = _interopRequireDefault(_shallowCopy); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -24,3 +28,3 @@ | ||
var getAccessorKey = function getAccessorKey(key) { | ||
var _getAccessorKey = function _getAccessorKey(key) { | ||
// this regex tests if the key is of the form abc[123], with opening and closing square brackets | ||
@@ -41,5 +45,5 @@ var containsAccessor = /\w+\[\w+\]$/.test(key); | ||
var removeAccessorKey = function removeAccessorKey(key) { | ||
if (getAccessorKey(key)) { | ||
return key.replace(getAccessorKey(key), ""); | ||
var _removeAccessorKey = function _removeAccessorKey(key) { | ||
if (_getAccessorKey(key)) { | ||
return key.replace(_getAccessorKey(key), ""); | ||
} | ||
@@ -49,13 +53,12 @@ return key; | ||
var _preprocess = function _preprocess(_originalMap) { | ||
var _map = _lodash2.default.cloneDeep(_originalMap); | ||
var _preprocess = function _preprocess(map) { | ||
var newMap = {}; | ||
for (var key in _originalMap) { | ||
var isFunction = _lodash2.default.isFunction(_map[key]); | ||
newMap[removeAccessorKey(key)] = { | ||
for (var key in map) { | ||
var isFunction = _lodash2.default.isFunction(map[key]); | ||
newMap[_removeAccessorKey(key)] = { | ||
key: /([^\[]+)/.exec(key)[1], | ||
isLeaf: isFunction, | ||
accessorKeyName: getAccessorKey(key), | ||
child: isFunction ? _map[key] : _preprocess(_map[key]) | ||
accessorKeyName: _getAccessorKey(key), | ||
child: isFunction ? map[key] : _preprocess(map[key]) | ||
}; | ||
@@ -71,3 +74,3 @@ } | ||
var newState = _lodash2.default.cloneDeep(state); | ||
var newState = (0, _shallowCopy2.default)(state); | ||
@@ -103,3 +106,5 @@ var _iteratorNormalCompletion = true; | ||
if (accessorKeyName) { | ||
newState[key][action[accessorKeyName]] = newSmallerState; | ||
var collection = (0, _shallowCopy2.default)(newState[key]); | ||
collection[action[accessorKeyName]] = newSmallerState; | ||
newState[key] = collection; | ||
} else { | ||
@@ -106,0 +111,0 @@ newState[key] = newSmallerState; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,20 +6,22 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.deleteArrayReducer = exports.addArrayReducer = undefined; | ||
var _lodash = require('lodash'); | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var addArrayReducer = exports.addArrayReducer = function addArrayReducer(state, action) { | ||
var newState = _lodash2.default.cloneDeep(state); | ||
newState.push(action.data); | ||
return newState; | ||
/** | ||
* Reducer that inserts a value to an array | ||
* @param {any} action.data The element to be added | ||
* @param {number} action.insertIndex The index to be inserted | ||
*/ | ||
var arrayInsertReducer = exports.arrayInsertReducer = function arrayInsertReducer(state, action) { | ||
var index = action.insertIndex == undefined ? state.length : action.insertIndex; | ||
return [].concat(_toConsumableArray(state.slice(0, index)), [action.data], _toConsumableArray(state.slice(index))); | ||
}; | ||
var deleteArrayReducer = exports.deleteArrayReducer = function deleteArrayReducer(state, action) { | ||
var newState = _lodash2.default.cloneDeep(state); | ||
newState.splice([action.deleteId], 1); | ||
return newState; | ||
/** | ||
* Reducer that removes an element from an array | ||
* @param {number} action.deleteIndex The index to be removed | ||
*/ | ||
var arrayDeleteReducer = exports.arrayDeleteReducer = function arrayDeleteReducer(state, action) { | ||
var index = action.deleteIndex; | ||
return [].concat(_toConsumableArray(state.slice(0, index)), _toConsumableArray(state.slice(index + 1))); | ||
}; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,20 +6,24 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.deleteObjectReducer = exports.addObjectReducer = undefined; | ||
var _lodash = require('lodash'); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var addObjectReducer = exports.addObjectReducer = function addObjectReducer(state, action) { | ||
var newState = _lodash2.default.cloneDeep(state); | ||
newState[action.addId] = action.data; | ||
return newState; | ||
/** | ||
* Reducer that inserts a value to an object | ||
* @param {any} action.data The element to be added | ||
* @param {string} action.insertKey The key to use | ||
*/ | ||
var objectInsertReducer = exports.objectInsertReducer = function objectInsertReducer(state, action) { | ||
return _extends({}, state, _defineProperty({}, "" + action.insertKey, action.data)); | ||
}; | ||
var deleteObjectReducer = exports.deleteObjectReducer = function deleteObjectReducer(state, action) { | ||
var newState = _lodash2.default.cloneDeep(state); | ||
delete newState[action.deleteId]; | ||
/** | ||
* Reducer that removes a value from an object | ||
* @param {string} action.deleteKey The key to use | ||
*/ | ||
var objectDeleteReducer = exports.objectDeleteReducer = function objectDeleteReducer(state, action) { | ||
var newState = _extends({}, state); | ||
delete newState[action.deleteKey]; | ||
return newState; | ||
}; |
{ | ||
"name": "remerge", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "State simplified.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "ava --tap | tap-nyan", | ||
"test": "ava --tap -v | tap-nyan", | ||
"prepublish": "babel src --out-dir lib --ignore merge.js && babel src/merge.js -o index.js" | ||
@@ -42,3 +42,4 @@ }, | ||
"dependencies": { | ||
"lodash": "^4.5.1" | ||
"lodash": "^4.5.1", | ||
"shallow-copy": "0.0.1" | ||
}, | ||
@@ -45,0 +46,0 @@ "ava": { |
@@ -63,12 +63,12 @@ # Remerge | ||
```js | ||
import { addArrayReducer, deleteArrayReducer } from 'remerge/lib/arrayReducers' | ||
import { addObjectReducer, deleteObjectReducer } from 'remerge/lib/objectReducers' | ||
import { arrayInsertReducer, arrayDeleteReducer } from 'remerge/lib/arrayReducers' | ||
import { objectInsertReducer, objectDeleteReducer } from 'remerge/lib/objectReducers' | ||
import { updateReducer } from 'remerge/lib/updateReducers' | ||
``` | ||
### arrayReducers | ||
### Array Reducers | ||
#### addArrayReducer | ||
#### Adding array elements | ||
`addArrayReducer` is used to append an object to the state, which is expected to be an array. `addArrayReducer` expects the object to be appended to be indicated with the `data` key: | ||
`arrayInsertReducer` is used to append an object to the state, which is expected to be an array. `arrayInsertReducer` expects the object to be appended to be indicated with the `data` key and optionally, the index `insertIndex`: | ||
@@ -78,2 +78,3 @@ ```js | ||
type: 'items.add', | ||
insertIndex: 0, | ||
data: { | ||
@@ -85,5 +86,5 @@ name: 'model' | ||
#### deleteArrayReducer | ||
#### Deleting array elements | ||
`deleteArrayReducer` is used to remove an object by index from the state, which is expeced to be an array. `deleteArrayReducer` expects the index of the object to be indicated with the `deleteId` key: | ||
`arrayDeleteReducer` is used to remove an object by index from the state, which is expeced to be an array. `arrayDeleteReducer` expects the index of the object to be indicated with the `deleteIndex` key: | ||
@@ -94,11 +95,11 @@ | ||
type: 'items.delete', | ||
deleteId: 1 | ||
deleteIndex: 1 | ||
} | ||
``` | ||
### objectReducers | ||
### Object Reducers | ||
#### addObjectReducer | ||
#### Inserting object keys | ||
`addArrayReducer` is used to add a key-value pair to the state, which is expected to be an object. `addArrayReducer` expects the key to be indicated with the `addId` key, and the value to be indicated with the `data` key: | ||
`objectInsertReducer` is used to add a key-value pair to the state, which is expected to be an object. `objectInsertReducer` expects the key to be indicated with the `insertKey` key, and the value to be indicated with the `data` key: | ||
@@ -108,3 +109,3 @@ ```js | ||
type: 'items.add', | ||
addId: 'apple', | ||
insertKey: 'apple', | ||
data: { name: 'a yummy apple' } | ||
@@ -116,5 +117,5 @@ } | ||
#### deleteObjectReducer | ||
#### Deleting object keys | ||
`deleteArrayReducer` is used to remove a key-value pair from the state, which is expected to be an object. `deleteArrayReducer` expects the key to be indicated with the `deleteId` key: | ||
`objectDeleteReducer` is used to remove a key-value pair from the state, which is expected to be an object. `objectDeleteReducer` expects the key to be indicated with the `deleteKey` key: | ||
@@ -124,7 +125,7 @@ ```js | ||
type: 'models.delete', | ||
deleteId: 'apple' | ||
deleteKey: 'apple' | ||
} | ||
``` | ||
### updateReducers | ||
### Update Reducers | ||
@@ -138,3 +139,2 @@ #### updateReducer | ||
type: 'items[].update', | ||
itemId: 'apple', | ||
data: { name: 'a yummier apple' } | ||
@@ -148,2 +148,2 @@ } | ||
Remerge includes a fairly comprehensive test suite that also doubles as documentation. Run it with `npm test`. | ||
Remerge includes a fairly comprehensive test suite that also doubles as documentation. Run it with `npm test`. |
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
13506
158
141
2
+ Addedshallow-copy@0.0.1
+ Addedshallow-copy@0.0.1(transitive)