react-redux-quest
Version:
API and generic utilities for react and redux eco-system
52 lines (36 loc) • 2.36 kB
JavaScript
exports.__esModule = true;
exports.createRegExReducer = exports.getTypes = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _invariant = require('invariant');
var _invariant2 = _interopRequireDefault(_invariant);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var getTypes = exports.getTypes = function getTypes() {
var types = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
var typePrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
(0, _invariant2.default)(types || typePrefix, 'Please specify array of 3 action descriptors \'types\'\n for request, success and failure in the same order\n\n Or specify \'typePrefix\' to create them');
var isValidTypes = types && Array.isArray(types) && types.length === 3 && types.every(function (type) {
return typeof type === 'string' || (typeof type === 'undefined' ? 'undefined' : _typeof(type)) === 'symbol';
});
var isValidTypePrefix = typePrefix && typeof typePrefix === 'string';
(0, _invariant2.default)(isValidTypes || isValidTypePrefix, 'Check yor have array of \'3\' action descriptors specified in \'types\'\n and all of them either string or symbol type\n\n Or you specified \'typePrefix\' of \'string\' type\n ');
if (isValidTypes) {
return types;
} else if (isValidTypePrefix) {
return [typePrefix + '_REQUEST', typePrefix + '_SUCCESS', typePrefix + '_FAIL'];
}
return null;
};
var createRegExReducer = exports.createRegExReducer = function createRegExReducer(initialState) {
return function (caseHandlers) {
return function reducer() {
var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
var action = arguments[1];
var partialCaseTypes = Object.keys(caseHandlers);
var relativCaseType = partialCaseTypes.find(function (caseType) {
return action.type.includes(caseType);
});
return relativCaseType && caseHandlers[relativCaseType](state, action) || state;
};
};
};
;