Socket
Socket
Sign inDemoInstall

remerge

Package Overview
Dependencies
2
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.7 to 0.0.8

323

lib/index.js

@@ -7,10 +7,2 @@ 'use strict';

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; }; /**
* The top-level merge function
*
* @param {object} remerge map
* @return {function} reducer function
*
*/
var _lodash = require('lodash.isfunction');

@@ -28,6 +20,15 @@

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); } } /**
* The top-level merge function
*
* @param {object} remerge map
* @return {function} reducer function
*
*/
var collectionRegex = /^\$(.+)/;
var legacyRegex = /^__(.+)__$/;
var remergeLegacyKey = '$legacy';
var merge = function merge(map) {
var merge = function merge(schema) {
var debugMode = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];

@@ -56,24 +57,49 @@

var _preprocess = function _preprocess(map) {
var newMap = {};
var _preprocess = function _preprocess(schema) {
var map = new Map();
function _dive(subSchema) {
var prefix = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
var path = arguments.length <= 2 || arguments[2] === undefined ? [] : arguments[2];
var params = arguments.length <= 3 || arguments[3] === undefined ? [] : arguments[3];
for (var key in map) {
if (key === '_') {
continue;
for (var key in subSchema) {
if (key === '_') {
continue;
}
var child = subSchema[key];
var type = '' + prefix + key;
if ((0, _lodash2.default)(child)) {
var legacyKey = _getLegacyKey(key);
if (legacyKey) {
var node = {
reducer: child,
path: [legacyKey]
};
var nodes = map.get(remergeLegacyKey) || [];
map.set(remergeLegacyKey, [].concat(_toConsumableArray(nodes), [node]));
} else {
var node = {
reducer: child,
path: path,
params: params
};
var nodes = map.get(type) || [];
map.set(type, [].concat(_toConsumableArray(nodes), [node]));
}
} else {
var param = _getAccessorKey(key);
if (param) {
_dive(child, prefix, [].concat(_toConsumableArray(path), [key]), [].concat(_toConsumableArray(params), [param]));
} else {
_dive(child, type + '.', [].concat(_toConsumableArray(path), [key]), params);
}
}
}
if (newMap.$) {
(0, _utils.consoleWarning)('More than one collection accessor ' + newMap.$.accessorKeyName);
continue;
}
var valueIsFunction = (0, _lodash2.default)(map[key]);
newMap[key.replace(_getAccessorKey(key), "")] = {
isLeaf: valueIsFunction,
accessorKeyName: _getAccessorKey(key),
legacyKeyName: _getLegacyKey(key),
child: valueIsFunction ? map[key] : _preprocess(map[key])
};
}
return newMap;
_dive(schema);
return map;
};

@@ -113,6 +139,6 @@

var _process = function _process(_map, state, action) {
var currentPath = action.type.split('.', 1)[0];
var _reduce = function _reduce(state, action, node) {
var newState = (0, _lodash4.default)(state);
var foundPath = false;
var current = newState;
var parent = null;

@@ -124,50 +150,14 @@ var _iteratorNormalCompletion = true;

try {
for (var _iterator = Object.keys(_map)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
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;
for (var _iterator = node.path[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
if (legacyKeyName) {
foundPath = true;
(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;
if (isLeaf) {
(0, _utils.consoleSuccess)('Executing action at leaf node: ' + currentPath, debugMode);
return child(newState, action);
} else {
var collectionKeyName = child.$ && child.$.accessorKeyName;
var collectionKey = action && action[collectionKeyName];
// 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, debugMode);
var newCollection = (0, _lodash4.default)((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(".")
});
var newSmallerState = _process(smallerMap, smallerState, smallerAction);
(0, _utils.setCollectionElement)(newCollection, collectionKey, newSmallerState);
(0, _utils.setCollectionElement)(newState, path, newCollection);
} else {
(0, _utils.consoleSuccess)('Navigating element node: ' + currentPath, 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 _accessorKey = _getAccessorKey(key);
if (_accessorKey) {
parent = current;
current = (0, _lodash4.default)((0, _utils.getCollectionElement)(current, action[_accessorKey]));
(0, _utils.setCollectionElement)(parent, action[_accessorKey], current);
} else {
parent = current;
current = (0, _lodash4.default)((0, _utils.getCollectionElement)(current, key));
(0, _utils.setCollectionElement)(parent, key, current);
}

@@ -190,18 +180,22 @@ }

if (!foundPath) {
(0, _utils.consoleError)('Could not find path: ' + currentPath, debugMode);
current = node.reducer(current, action);
var lastKey = node.path[node.path.length - 1];
var accessorKey = _getAccessorKey(lastKey);
if (accessorKey) {
(0, _utils.setCollectionElement)(parent, action[accessorKey], current);
} else {
(0, _utils.setCollectionElement)(parent, lastKey, current);
}
return newState;
};
var initialState = _initial(map);
var computedMap = _preprocess(map);
var initialState = _initial(schema);
var map = _preprocess(schema);
return function (state, action) {
if (action === undefined) {
(0, _utils.consoleMessage)('Setting up initial state tree', debugMode);
(0, _utils.consoleMessage)(debugMode, 'Setting up initial state tree');
} else if (!action.type) {
(0, _utils.consoleError)('Action is missing type', debugMode);
(0, _utils.consoleError)(debugMode, 'Action is missing type');
}

@@ -213,6 +207,159 @@

(0, _utils.consoleGrouped)('Received action with type: ' + action.type, debugMode);
var newState = _process(computedMap, state, action);
(0, _utils.consoleEndGrouped)(null, debugMode);
var successLogs = [];
var errorLogs = [];
var newState = state;
var nodes = map.get(action.type);
if (nodes) {
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = nodes[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var node = _step2.value;
var valid = node.params.map(function (p) {
return action[p];
}).reduce(function (prev, curr) {
return prev && curr !== undefined;
}, true);
if (valid) {
successLogs.push('Executing action ' + action.type);
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
try {
for (var _iterator3 = node.params[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var param = _step3.value;
successLogs.push('$' + param + ' = ' + action[param]);
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
newState = _reduce(newState, action, node);
break;
} else {
errorLogs.push('Could not execute action ' + action.type + ' with params ' + node.params);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
} else {
var legacyNodes = map.get(remergeLegacyKey);
var _iteratorNormalCompletion4 = true;
var _didIteratorError4 = false;
var _iteratorError4 = undefined;
try {
for (var _iterator4 = legacyNodes[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
var node = _step4.value;
successLogs.push('Executing legacy action ' + action.type);
newState = _reduce(newState, action, node);
}
} catch (err) {
_didIteratorError4 = true;
_iteratorError4 = err;
} finally {
try {
if (!_iteratorNormalCompletion4 && _iterator4.return) {
_iterator4.return();
}
} finally {
if (_didIteratorError4) {
throw _iteratorError4;
}
}
}
}
if (successLogs.length > 0) {
(0, _utils.consoleGrouped)(debugMode, 'Processing action ' + action.type);
var _iteratorNormalCompletion5 = true;
var _didIteratorError5 = false;
var _iteratorError5 = undefined;
try {
for (var _iterator5 = successLogs[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
var log = _step5.value;
(0, _utils.consoleSuccess)(debugMode, log);
}
} catch (err) {
_didIteratorError5 = true;
_iteratorError5 = err;
} finally {
try {
if (!_iteratorNormalCompletion5 && _iterator5.return) {
_iterator5.return();
}
} finally {
if (_didIteratorError5) {
throw _iteratorError5;
}
}
}
(0, _utils.consoleEndGrouped)(debugMode);
} else if (errorLogs.length > 0) {
(0, _utils.consoleGrouped)(debugMode, 'Processing action ' + action.type, false);
var _iteratorNormalCompletion6 = true;
var _didIteratorError6 = false;
var _iteratorError6 = undefined;
try {
for (var _iterator6 = errorLogs[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
var log = _step6.value;
(0, _utils.consoleSuccess)(debugMode, log);
}
} catch (err) {
_didIteratorError6 = true;
_iteratorError6 = err;
} finally {
try {
if (!_iteratorNormalCompletion6 && _iterator6.return) {
_iterator6.return();
}
} finally {
if (_didIteratorError6) {
throw _iteratorError6;
}
}
}
(0, _utils.consoleEndGrouped)(debugMode);
} else {
(0, _utils.consoleGrouped)(debugMode, 'Processing action ' + action.type, false);
(0, _utils.consoleError)(debugMode, 'No available action found!');
(0, _utils.consoleEndGrouped)(debugMode);
}
return newState;

@@ -219,0 +366,0 @@ };

@@ -73,35 +73,47 @@ 'use strict';

function consoleMessage(msg, debugMode) {
function _console(msg, color) {
var func = arguments.length <= 2 || arguments[2] === undefined ? console.log : arguments[2];
if (typeof window === 'undefined') {
func('[remerge] ' + msg);
} else {
func.call(console, '%c[remerge]%c ' + msg, color, '');
}
}
function consoleMessage(debugMode, msg) {
if (debugMode) {
console.log('%c[remerge]%c ' + msg, colors.black, '');
_console(msg, colors.black);
}
}
function consoleWarning(msg, debugMode) {
function consoleWarning(debugMode, msg) {
if (debugMode) {
console.log('%c[remerge]%c ' + msg, colors.yellow, '');
_console(msg, colors.yellow);
}
}
function consoleSuccess(msg, debugMode) {
function consoleSuccess(debugMode, msg) {
if (debugMode) {
console.log('%c[remerge]%c ' + msg, colors.green, '');
_console(msg, colors.green);
}
}
function consoleError(msg, debugMode) {
function consoleError(debugMode, msg) {
if (debugMode) {
console.error('%c[remerge]%c ' + msg, colors.red, '');
_console(msg, colors.red);
}
}
function consoleGrouped(msg, debugMode) {
if (debugMode && console.groupCollapsed) {
console.groupCollapsed('%c[remerge]%c ' + msg, colors.black, '');
function consoleGrouped(debugMode, msg) {
var collapsed = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
if (debugMode && console.group) {
_console(msg, colors.black, collapsed ? console.groupCollapsed : console.group);
} else if (debugMode) {
console.log('%c[remerge]%c ' + msg, colors.black, '');
_console(msg, colors.black);
}
}
function consoleEndGrouped(msg, debugMode) {
function consoleEndGrouped(debugMode) {
if (debugMode && console.groupEnd) {

@@ -108,0 +120,0 @@ console.groupEnd();

{
"name": "remerge",
"version": "0.0.7",
"version": "0.0.8",
"description": "State simplified.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc