2018-04-20 12:23:20 +01:00
|
|
|
/**
|
2018-06-06 16:37:12 +01:00
|
|
|
* Wrap operations for consumption in Node.
|
2018-04-20 12:23:20 +01:00
|
|
|
*
|
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2018-06-06 16:37:12 +01:00
|
|
|
import SyncDish from "./SyncDish";
|
2018-04-20 12:23:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract default arg value from operation argument
|
|
|
|
* @param {Object} arg - an arg from an operation
|
|
|
|
*/
|
|
|
|
function extractArg(arg) {
|
|
|
|
if (arg.type === "option" || arg.type === "editableOption") {
|
2018-06-15 11:33:13 +01:00
|
|
|
// pick default option if not already chosen
|
|
|
|
return typeof arg.value === "string" ? arg.value : arg.value[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg.type === "toggleString") {
|
|
|
|
// ensure string and option exist when user hasn't defined
|
|
|
|
arg.string = arg.string || "";
|
|
|
|
arg.option = arg.option || arg.toggleValues[0];
|
|
|
|
return arg;
|
2018-04-20 12:23:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return arg.value;
|
|
|
|
}
|
|
|
|
|
2018-05-11 19:25:14 +01:00
|
|
|
/**
|
|
|
|
* transformArgs
|
|
|
|
*
|
|
|
|
* Take the default args array and update with any user-defined
|
2018-06-15 11:33:13 +01:00
|
|
|
* operation arguments. Allows user to define arguments in object style,
|
|
|
|
* with accommodating name matching. Using named args in the API is more
|
2018-05-11 19:25:14 +01:00
|
|
|
* clear to the user.
|
|
|
|
*
|
|
|
|
* Argument name matching is case and space insensitive
|
|
|
|
* @private
|
|
|
|
* @param {Object[]} originalArgs
|
|
|
|
* @param {Object} newArgs
|
|
|
|
*/
|
|
|
|
function transformArgs(originalArgs, newArgs) {
|
|
|
|
const allArgs = Object.assign([], originalArgs);
|
|
|
|
|
|
|
|
if (newArgs) {
|
|
|
|
Object.keys(newArgs).map((key) => {
|
|
|
|
const index = allArgs.findIndex((arg) => {
|
|
|
|
return arg.name.toLowerCase().replace(/ /g, "") ===
|
|
|
|
key.toLowerCase().replace(/ /g, "");
|
|
|
|
});
|
2018-06-15 11:33:13 +01:00
|
|
|
|
2018-05-11 19:25:14 +01:00
|
|
|
if (index > -1) {
|
2018-06-15 11:33:13 +01:00
|
|
|
const argument = allArgs[index];
|
|
|
|
if (["toggleString"].indexOf(argument.type) > -1) {
|
|
|
|
argument.string = newArgs[key].string;
|
|
|
|
argument.option = newArgs[key].option;
|
|
|
|
} else {
|
|
|
|
argument.value = newArgs[key];
|
|
|
|
}
|
2018-05-11 19:25:14 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return allArgs.map(extractArg);
|
|
|
|
}
|
|
|
|
|
2018-04-20 12:23:20 +01:00
|
|
|
/**
|
|
|
|
* Wrap an operation to be consumed by node API.
|
|
|
|
* new Operation().run() becomes operation()
|
|
|
|
* Perform type conversion on input
|
2018-05-11 19:25:14 +01:00
|
|
|
* @private
|
2018-04-20 12:23:20 +01:00
|
|
|
* @param {Operation} Operation
|
|
|
|
* @returns {Function} The operation's run function, wrapped in
|
|
|
|
* some type conversion logic
|
|
|
|
*/
|
2018-06-06 16:37:12 +01:00
|
|
|
export function wrap(opClass) {
|
2018-04-20 12:23:20 +01:00
|
|
|
/**
|
|
|
|
* Wrapped operation run function
|
2018-06-06 16:37:12 +01:00
|
|
|
* @param {*} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {SyncDish} operation's output, on a Dish.
|
|
|
|
* @throws {OperationError} if the operation throws one.
|
2018-04-20 12:23:20 +01:00
|
|
|
*/
|
2018-06-06 16:37:12 +01:00
|
|
|
return (input, args=null) => {
|
|
|
|
const operation = new opClass();
|
2018-05-03 10:20:13 +01:00
|
|
|
|
2018-06-06 16:37:12 +01:00
|
|
|
let dish;
|
|
|
|
if (input instanceof SyncDish) {
|
|
|
|
dish = input;
|
|
|
|
} else {
|
|
|
|
dish = new SyncDish();
|
|
|
|
const type = SyncDish.typeEnum(input.constructor.name);
|
|
|
|
dish.set(input, type);
|
2018-05-03 10:20:13 +01:00
|
|
|
}
|
|
|
|
|
2018-05-11 19:25:14 +01:00
|
|
|
args = transformArgs(operation.args, args);
|
2018-06-06 16:37:12 +01:00
|
|
|
const transformedInput = dish.get(operation.inputType);
|
|
|
|
const result = operation.run(transformedInput, args);
|
|
|
|
return new SyncDish({
|
|
|
|
value: result,
|
|
|
|
type: operation.outputType
|
|
|
|
});
|
2018-04-20 12:23:20 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract properties from an operation by instantiating it and
|
|
|
|
* returning some of its properties for reference.
|
|
|
|
* @param {Operation} Operation - the operation to extract info from
|
|
|
|
* @returns {Object} operation properties
|
|
|
|
*/
|
|
|
|
function extractOperationInfo(Operation) {
|
|
|
|
const operation = new Operation();
|
|
|
|
return {
|
|
|
|
name: operation.name,
|
|
|
|
module: operation.module,
|
|
|
|
description: operation.description,
|
|
|
|
inputType: operation.inputType,
|
|
|
|
outputType: operation.outputType,
|
2018-05-11 19:25:14 +01:00
|
|
|
// Make arg names lowercase, no spaces to encourage non-sentence
|
|
|
|
// caps in repl
|
|
|
|
args: Object.assign([], operation.args).map((s) => {
|
|
|
|
s.name = decapitalise(s.name).replace(/ /g, "");
|
|
|
|
return s;
|
|
|
|
})
|
2018-04-20 12:23:20 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-05-11 11:09:04 +01:00
|
|
|
* @namespace Api
|
2018-04-20 12:23:20 +01:00
|
|
|
* @param {Object} operations - an object filled with operations.
|
|
|
|
* @param {String} searchTerm - the name of the operation to get help for.
|
|
|
|
* Case and whitespace are ignored in search.
|
|
|
|
* @returns {Object} listing properties of function
|
|
|
|
*/
|
|
|
|
export function help(operations, searchTerm) {
|
|
|
|
if (typeof searchTerm === "string") {
|
|
|
|
const operation = operations[Object.keys(operations).find(o =>
|
|
|
|
o.toLowerCase() === searchTerm.replace(/ /g, "").toLowerCase())];
|
|
|
|
if (operation) {
|
|
|
|
return extractOperationInfo(operation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SomeName => someName
|
|
|
|
* @param {String} name - string to be altered
|
|
|
|
* @returns {String} decapitalised
|
|
|
|
*/
|
|
|
|
export function decapitalise(name) {
|
2018-05-24 17:02:54 +01:00
|
|
|
// Don't decapitalise names that start with 2+ caps
|
|
|
|
if (/^[A-Z0-9]{2,}/g.test(name)) {
|
2018-05-11 17:33:14 +01:00
|
|
|
return name;
|
|
|
|
}
|
2018-05-24 17:02:54 +01:00
|
|
|
// reserved. Don't change for now.
|
|
|
|
if (name === "Return") {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-04-20 12:23:20 +01:00
|
|
|
return `${name.charAt(0).toLowerCase()}${name.substr(1)}`;
|
|
|
|
}
|