"use strict"; const assert = require("assert"); const assureArray = require("assure-array"); function doConversionForLevel(structure, operations, path) { return operations.flatMap((operation) => { assert(operation.type != null); let handlers = structure[operation.type]; assert(handlers != null); // FIXME: Better error reporting when this occurs, this is a bug return assureArray(handlers).flatMap((handler) => { return (typeof handler === "function") ? handler(operation, path) : doConversionForLevel(handler, operation.operations, path.concat([ operation ])); }); }); } module.exports = function treeToOperations(structure, operations) { return doConversionForLevel(structure, operations, []); };