redux-conditional
Version:
Make sharing reducers easy by conditionally applying actions to shared Redux reducers.
240 lines (185 loc) • 7.32 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["ReduxConditional"] = factory();
else
root["ReduxConditional"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
module.exports = __webpack_require__(1);
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
exports.simpleConditional = exports.conditionalDefaultSubKey = exports.conditionalReducerByKey = exports.conditionalReducer = undefined;
var _simpleConditional = __webpack_require__(2);
var unknownAction = { type: Symbol('REDUX CONDITIONAL UNKNOWN TYPE') };
var conditionalReducer = function conditionalReducer(conditionFn, reducer) {
return function (state, action) {
return reducer(state, conditionFn(state, action) ? action : unknownAction);
};
};
var normalizeTargets = function normalizeTargets(targets, conditionFnMaker) {
if (Array.isArray(targets)) {
return targets.map(function (key) {
return { key: key, conditionFn: conditionFnMaker(key) };
});
}
return Object.keys(targets).map(function (key) {
return { key: key, conditionFn: conditionFnMaker(targets[key]) };
});
};
// simpleConditional().default expects the "data" can be accessed by action[conditionalKey][conditionalDefaultSubKey]
var conditionalReducerByKey = function conditionalReducerByKey(targets) {
var _ref = arguments.length <= 1 || arguments[1] === undefined ? (0, _simpleConditional.simpleConditional)()['default'] : arguments[1];
var conditionMaker = _ref.conditionMaker;
var normalized = normalizeTargets(targets, conditionMaker);
return function (reducer) {
return function (state, action) {
var nextState = {};
var changed = false;
normalized.forEach(function (target) {
var subState = state;
if (state) {
subState = state[target.key];
}
nextState[target.key] = conditionalReducer(target.conditionFn, reducer)(subState, action);
changed = changed || nextState[target.key] !== subState;
});
return changed ? nextState : state;
};
};
};
exports.conditionalReducer = conditionalReducer;
exports.conditionalReducerByKey = conditionalReducerByKey;
exports.conditionalDefaultSubKey = _simpleConditional.conditionalDefaultSubKey;
exports.simpleConditional = _simpleConditional.simpleConditional;
/***/ },
/* 2 */
/***/ function(module, exports) {
'use strict';
exports.__esModule = true;
var conditionalDefaultSubKey = '__redux_conditional_default_subkey__'; // Symbol('SUB KEY IN CONDITIONAL KEY');
var conditionalKeyReader = function conditionalKeyReader(paths) {
var subKey = arguments.length <= 1 || arguments[1] === undefined ? conditionalDefaultSubKey : arguments[1];
return function (action) {
var fullPath = [];
if (Array.isArray(paths)) {
fullPath = paths;
}
var val = action;
try {
fullPath.reduce(function (prev, cur) {
return val = prev[cur];
}, action);
} catch (err) {
// just don't report error
} finally {
return val && val[subKey];
}
};
};
var conditionalKeyWriter = function conditionalKeyWriter(paths) {
var subKey = arguments.length <= 1 || arguments[1] === undefined ? conditionalDefaultSubKey : arguments[1];
return function (data) {
return function (action) {
var target = action;
if (Array.isArray(paths)) {
target = paths.reduce(function (prev, cur) {
if (prev[cur] === undefined || prev[cur] === null) {
prev[cur] = {};
}
return prev[cur];
}, action);
}
target[subKey] = data;
return action;
};
};
};
var simpleConditionMaker = function simpleConditionMaker(reader) {
return function (data) {
return function (state, action) {
return reader(action) === data;
};
};
};
var simpleConditionalActionCreator = function simpleConditionalActionCreator(paths) {
return function (actionCreator) {
return function (data) {
return function () {
var dataWithKey = data;
if (typeof data !== 'object') {
var _dataWithKey;
dataWithKey = (_dataWithKey = {}, _dataWithKey[conditionalDefaultSubKey] = data, _dataWithKey);
}
var initValue = typeof actionCreator === 'function' ? actionCreator.apply(undefined, arguments) : Object.assign({}, actionCreator);
return Object.keys(dataWithKey).concat(Object.getOwnPropertySymbols(dataWithKey)).reduce(function (action, subKey) {
return conditionalKeyWriter(paths, subKey)(dataWithKey[subKey])(action);
}, initValue);
};
};
};
};
var simpleConditional = function simpleConditional() {
var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var paths = _ref.paths;
var switches = _ref.switches;
// don't use "actionCreator"
var conditions = { actionCreator: simpleConditionalActionCreator(paths) };
var subKeys = (switches || []).concat([conditionalDefaultSubKey]);
subKeys.forEach(function (subKey) {
var keyWriter = conditionalKeyWriter(paths, subKey);
var keyReader = conditionalKeyReader(paths, subKey);
var exportKey = subKey === conditionalDefaultSubKey ? 'default' : subKey;
conditions[exportKey] = {
conditionMaker: simpleConditionMaker(keyReader),
conditionalKeyWriter: keyWriter,
conditionalKeyReader: keyReader
};
});
return conditions;
};
exports.conditionalDefaultSubKey = conditionalDefaultSubKey;
exports.simpleConditional = simpleConditional;
/***/ }
/******/ ])
});
;