You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
728 B
JavaScript
23 lines
728 B
JavaScript
"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, []);
|
|
};
|