UNPKG

react-redux-quest

Version:

API and generic utilities for react and redux eco-system

43 lines (31 loc) 2.04 kB
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; }; import invariant from 'invariant'; export var getTypes = function getTypes() { var types = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null; var typePrefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; invariant(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'; invariant(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; }; export var 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; }; }; };