remerge
Advanced tools
Comparing version 0.0.4 to 0.0.5
108
lib/index.js
@@ -23,15 +23,13 @@ 'use strict'; | ||
var collectionRegex = /^\$(.+)/; | ||
var merge = function merge(map) { | ||
var debugMode = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1]; | ||
var _getAccessorKey = function _getAccessorKey(key) { | ||
// this regex tests if the key is of the form abc[123], with opening and closing square brackets | ||
var containsAccessor = /\w+\[\w+\]$/.test(key); | ||
if (containsAccessor) { | ||
// this regex captures the content inside the square brackets | ||
var captureAccessor = /.+\[(\w+)\]/.exec(key); | ||
if (captureAccessor) { | ||
return captureAccessor[1]; | ||
} else { | ||
return null; | ||
} | ||
// this regex tests if the key is of the form $abcd1234 | ||
var captureAccessor = collectionRegex.exec(key); | ||
if (captureAccessor) { | ||
return captureAccessor[1]; | ||
} else { | ||
@@ -42,9 +40,2 @@ return null; | ||
var _removeAccessorKey = function _removeAccessorKey(key) { | ||
if (_getAccessorKey(key)) { | ||
return key.replace(_getAccessorKey(key), ""); | ||
} | ||
return key; | ||
}; | ||
var _preprocess = function _preprocess(map) { | ||
@@ -54,5 +45,12 @@ var newMap = {}; | ||
for (var key in map) { | ||
if (key === '_') { | ||
continue; | ||
} | ||
if (newMap.$) { | ||
(0, _utils.consoleWarning)('More than one collection accessor ' + newMap.$.accessorKeyName); | ||
continue; | ||
} | ||
var isFunction = _lodash2.default.isFunction(map[key]); | ||
newMap[_removeAccessorKey(key)] = { | ||
key: /([^\[]+)/.exec(key)[1], | ||
newMap[key.replace(_getAccessorKey(key), "")] = { | ||
isLeaf: isFunction, | ||
@@ -90,4 +88,4 @@ accessorKeyName: _getAccessorKey(key), | ||
var currentPath = action.type.split('.', 1)[0]; | ||
var newState = _lodash2.default.clone(state); | ||
var foundPath = false; | ||
@@ -103,4 +101,4 @@ var _iteratorNormalCompletion = true; | ||
if (path === currentPath) { | ||
foundPath = true; | ||
var _map$path = _map[path]; | ||
var key = _map$path.key; | ||
var accessorKeyName = _map$path.accessorKeyName; | ||
@@ -112,38 +110,31 @@ var isLeaf = _map$path.isLeaf; | ||
if (isLeaf) { | ||
(0, _utils.consoleSuccess)('Executing action at leaf node: ' + currentPath.bold, debugMode); | ||
return child(newState, action); | ||
} else if (accessorKeyName) { | ||
} else { | ||
var collectionKeyName = child.$ && child.$.accessorKeyName; | ||
var collectionKey = action && action[collectionKeyName]; | ||
var smallerMap = child; | ||
var smallerState = undefined; | ||
// child is a collection and we should enter because accessor key name is given | ||
if (collectionKeyName && collectionKey !== undefined) { | ||
(0, _utils.consoleSuccess)('Navigating collection node: ' + currentPath.bold, debugMode); | ||
var newCollection = _lodash2.default.clone((0, _utils.getCollectionElement)(newState, path)); | ||
var smallerMap = child.$.child; | ||
var smallerState = (0, _utils.getCollectionElement)(newCollection, collectionKey); | ||
var smallerAction = _extends({}, action, { | ||
type: action.type.split('.').splice(1).join(".") | ||
}); | ||
if ((0, _utils.isMap)(newState[key])) { | ||
smallerState = newState[key].get(action[accessorKeyName]); | ||
var newSmallerState = _process(smallerMap, smallerState, smallerAction); | ||
(0, _utils.setCollectionElement)(newCollection, collectionKey, newSmallerState); | ||
(0, _utils.setCollectionElement)(newState, path, newCollection); | ||
} else { | ||
smallerState = newState[key][action[accessorKeyName]]; | ||
(0, _utils.consoleSuccess)('Navigating element node: ' + currentPath.bold, debugMode); | ||
var smallerMap = child; | ||
var smallerState = (0, _utils.getCollectionElement)(newState, path); | ||
var smallerAction = _extends({}, action, { | ||
type: action.type.split('.').splice(1).join(".") | ||
}); | ||
var newSmallerState = _process(smallerMap, smallerState, smallerAction); | ||
(0, _utils.setCollectionElement)(newState, path, newSmallerState); | ||
} | ||
var smallerAction = _extends({}, action, { | ||
type: action.type.split('.').splice(1).join(".") | ||
}); | ||
var newSmallerState = _process(smallerMap, smallerState, smallerAction); | ||
var collection = _lodash2.default.clone(newState[key]); | ||
if ((0, _utils.isMap)(newState[key])) { | ||
collection.set(action[accessorKeyName], newSmallerState); | ||
} else { | ||
collection[action[accessorKeyName]] = newSmallerState; | ||
} | ||
newState[key] = collection; | ||
} else { | ||
var smallerMap = child; | ||
var smallerState = newState[key]; | ||
var smallerAction = _extends({}, action, { | ||
type: action.type.split('.').splice(1).join(".") | ||
}); | ||
var newSmallerState = _process(smallerMap, smallerState, smallerAction); | ||
newState[key] = newSmallerState; | ||
} | ||
@@ -167,2 +158,6 @@ } | ||
if (!foundPath) { | ||
(0, _utils.consoleError)('Could not find path: ' + currentPath.bold, debugMode); | ||
} | ||
return newState; | ||
@@ -175,2 +170,11 @@ }; | ||
return function (state, action) { | ||
if (action === undefined) { | ||
(0, _utils.consoleMessage)('Setting up initial state tree', debugMode); | ||
} else if (!action.type) { | ||
(0, _utils.consoleError)('Action is missing type', debugMode); | ||
} else { | ||
(0, _utils.consoleMessage)(('Received action with type: ' + action.type.bold).underline, debugMode); | ||
} | ||
if (state === undefined) { | ||
@@ -177,0 +181,0 @@ return initialState; |
@@ -7,4 +7,70 @@ 'use strict'; | ||
exports.isMap = isMap; | ||
exports.debug = debug; | ||
exports.getCollectionElement = getCollectionElement; | ||
exports.setCollectionElement = setCollectionElement; | ||
exports.consoleMessage = consoleMessage; | ||
exports.consoleWarning = consoleWarning; | ||
exports.consoleSuccess = consoleSuccess; | ||
exports.consoleError = consoleError; | ||
var _colors = require('colors'); | ||
var _colors2 = _interopRequireDefault(_colors); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function isMap(obj) { | ||
return Object.getPrototypeOf(obj) === Map.prototype || obj.constructor.name === 'Map'; // Ava has a map shim which doesn't play nicely | ||
} | ||
function debug(map, state, action) { | ||
console.log('----------------------'); | ||
console.log('Running process'); | ||
console.log('current map'); | ||
console.log(map); | ||
console.log('current state'); | ||
console.log(state); | ||
console.log('current action'); | ||
console.log(action); | ||
console.log('----------------------'); | ||
} | ||
function getCollectionElement(collection, key) { | ||
if (isMap(collection)) { | ||
return collection.get(key); | ||
} else { | ||
return collection[key]; | ||
} | ||
} | ||
function setCollectionElement(collection, key, value) { | ||
if (isMap(collection)) { | ||
collection.set(key, value); | ||
} else { | ||
collection[key] = value; | ||
} | ||
} | ||
function consoleMessage(msg, debugMode) { | ||
if (debugMode) { | ||
console.log('[remerge]'.cyan + ' ' + msg); | ||
} | ||
} | ||
function consoleWarning(msg, debugMode) { | ||
if (debugMode) { | ||
console.log('[remerge]'.yellow + ' ' + msg); | ||
} | ||
} | ||
function consoleSuccess(msg, debugMode) { | ||
if (debugMode) { | ||
console.log('[remerge]'.green + ' ' + msg); | ||
} | ||
} | ||
function consoleError(msg, debugMode) { | ||
if (debugMode) { | ||
console.error('[remerge]'.red + ' ' + msg); | ||
} | ||
} |
{ | ||
"name": "remerge", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "State simplified.", | ||
@@ -43,2 +43,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"colors": "^1.1.2", | ||
"lodash": "^4.5.1" | ||
@@ -45,0 +46,0 @@ }, |
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
18339
283
0
2
+ Addedcolors@^1.1.2
+ Addedcolors@1.4.0(transitive)