UNPKG

react-elegant-ui

Version:

Elegant UI components, made by BEM best practices for react

135 lines (134 loc) 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.configure = void 0; var __values = void 0 && (void 0).__values || function (o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; // TODO: implement option to select strategy between depth/breadth /** * Constructor of merge function */ var configure = function (_a) { var handlers = _a.handlers; var mergeFn = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } // Initialization var handlersRegistry = []; handlers.forEach(function (handler) { var storage = {}; if (typeof handler === 'function') { // Simply handler handlersRegistry.push({ handler: handler, storage: storage }); } else { // Complexity handler if (handler.onStart !== undefined) { handler.onStart(args, storage); } handlersRegistry.push({ handler: handler.fn, storage: storage, source: handler }); } }); // Main var emptyValueSymbol = {}; var mergedObject = args.reduce(function (acc, obj) { var e_1, _a, e_2, _b; // Skip undefined objects if (obj === undefined) { return acc; } try { // Iterate properties of object for (var _c = __values(Object.keys(obj)), _d = _c.next(); !_d.done; _d = _c.next()) { var key = _d.value; var accProp = acc[key]; var objProp = obj[key]; var _loop_1 = function (handlerEntry) { var value = emptyValueSymbol; var setValue = function (newValue) { value = newValue; }; var handler = handlerEntry.handler, storage = handlerEntry.storage; handler({ key: key, currentValue: accProp, value: objProp, setValue: setValue }, storage); // Skip handler which is not set value if (value === emptyValueSymbol) return "continue"; // Set value value and break acc[key] = value; return "break"; }; try { // Iterate handlers for (var handlersRegistry_1 = (e_2 = void 0, __values(handlersRegistry)), handlersRegistry_1_1 = handlersRegistry_1.next(); !handlersRegistry_1_1.done; handlersRegistry_1_1 = handlersRegistry_1.next()) { var handlerEntry = handlersRegistry_1_1.value; var state_1 = _loop_1(handlerEntry); if (state_1 === "break") break; } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (handlersRegistry_1_1 && !handlersRegistry_1_1.done && (_b = handlersRegistry_1.return)) _b.call(handlersRegistry_1); } finally { if (e_2) throw e_2.error; } } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_1) throw e_1.error; } } return acc; }, {}); // End handlersRegistry.forEach(function (handlerEntry) { var source = handlerEntry.source, storage = handlerEntry.storage; if (source === undefined) return; var onEnd = source.onEnd; if (onEnd !== undefined) { onEnd(mergedObject, storage); } }); return mergedObject; }; return mergeFn; }; exports.configure = configure;