UNPKG

reducers-creator

Version:

Reducers Creator is an awesome tiny javascript package that allows you to easily and intuitively create reducer functions. Mainly used to create redux reducers.

58 lines (43 loc) 2.95 kB
"use strict"; function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } /** * Copyright (c) 2020-present Ruben Arushanyan (https://github.com/ruben-arushanyan) * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ var _require = require('./utils'), isFunction = _require.isFunction, isObject = _require.isObject; var reducerTreeParser = function reducerTreeParser(reducer_tree) { var reducer_name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; var handlers = {}; var findHandlers = function findHandlers(tree_branch) { var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; if (isObject(tree_branch)) { for (var _i = 0, _Object$entries = Object.entries(tree_branch); _i < _Object$entries.length; _i++) { var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), key = _Object$entries$_i[0], value = _Object$entries$_i[1]; var type = prefix ? "".concat(prefix, "/").concat(key) : key; if (isFunction(value)) { handlers[type] = value; if (reducer_name) { handlers["".concat(reducer_name, "/").concat(type)] = value; } } else { findHandlers(value, type); } } } }; findHandlers(reducer_tree); return handlers; }; module.exports = reducerTreeParser;