function-tree
Version:
When a function is not enough
81 lines • 3.58 kB
JavaScript
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); }
import { Sequence, Parallel, Primitive } from './primitives';
import { FunctionTreeError } from './errors';
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 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 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 Primitive) {
var instance = item.toJSON();
return Object.assign(instance, {
items: analyze(name, functions, instance.items, item instanceof Parallel).items
});
} else if (Array.isArray(item)) {
return new Sequence(item.reduce(function (allItems, subItem, index) {
if (subItem instanceof Primitive) {
var _instance = subItem.toJSON();
return allItems.concat(Object.assign(_instance, {
items: analyze(name, functions, _instance.items, subItem instanceof 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 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 FunctionTreeError("Unexpected entry in \"".concat(name, "\". ").concat(stringifyInvalidTreeItem(item, subItem)));
}
}, [])).toJSON();
} else {
throw new FunctionTreeError('Unexpected entry in tree');
}
}
export default (function (name, tree) {
var functions = [];
return analyze(name, functions, typeof tree === 'function' ? [tree] : tree);
});
//# sourceMappingURL=staticTree.js.map