UNPKG

function-tree

Version:
87 lines (86 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _primitives = require("./primitives"); var _errors = require("./errors"); function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function getFunctionName(fn) { if (fn.displayName) return fn.displayName; if (fn.name) return fn.name; var ret = fn.toString(); var startNameMatch; if (ret.indexOf('async function') === 0) startNameMatch = 'async function ';else if (ret.indexOf('function') === 0) startNameMatch = 'function '; ret = ret.substr(startNameMatch ? startNameMatch.length : 0); ret = ret.substr(0, ret.indexOf('(')); return ret; } function isPaths(item) { return item && !Array.isArray(item) && _typeof(item) === 'object' && !(item instanceof _primitives.Primitive); } function stringifyInvalidTreeItem(items, invalidItem) { return "\n[\n".concat(items.map(function (item) { if (item === invalidItem) { return " ".concat(_typeof(invalidItem), ", <-- PROBLEM"); } if (typeof item === 'function') { return " ".concat(getFunctionName(item), ","); } if (item instanceof _primitives.Primitive) { return " [ ".concat(item.type.toUpperCase(), " ],"); } if (Array.isArray(item)) { return " [ SEQUENCE ],"; } return " { PATHS },"; }).join('\n'), "\n]\n "); } function analyze(name, functions, item, _isParallel) { if (item instanceof _primitives.Primitive) { var instance = item.toJSON(); return Object.assign(instance, { items: analyze(name, functions, instance.items, item instanceof _primitives.Parallel).items }); } else if (Array.isArray(item)) { return new _primitives.Sequence(item.reduce(function (allItems, subItem, index) { if (subItem instanceof _primitives.Primitive) { var _instance = subItem.toJSON(); return allItems.concat(Object.assign(_instance, { items: analyze(name, functions, _instance.items, subItem instanceof _primitives.Parallel).items })); } else if (typeof subItem === 'function') { var funcDetails = { name: subItem.displayName || getFunctionName(subItem), functionIndex: functions.push(subItem) - 1, "function": subItem }; var nextItem = item[index + 1]; if (isPaths(nextItem)) { funcDetails.outputs = {}; Object.keys(nextItem).forEach(function (key) { if (subItem.outputs && !~subItem.outputs.indexOf(key)) { throw new _errors.FunctionTreeError("Outputs object doesn't match list of possible outputs defined for function."); } funcDetails.outputs[key] = analyze(name, functions, typeof nextItem[key] === 'function' ? [nextItem[key]] : nextItem[key]); }); } return allItems.concat(funcDetails); } else if (isPaths(subItem)) { return allItems; } else if (Array.isArray(subItem)) { var items = analyze(name, functions, subItem); return allItems.concat(items); } else { throw new _errors.FunctionTreeError("Unexpected entry in \"".concat(name, "\". ").concat(stringifyInvalidTreeItem(item, subItem))); } }, [])).toJSON(); } else { throw new _errors.FunctionTreeError('Unexpected entry in tree'); } } var _default = exports["default"] = function _default(name, tree) { var functions = []; return analyze(name, functions, typeof tree === 'function' ? [tree] : tree); }; //# sourceMappingURL=staticTree.js.map