From c238862efe0c062fdb23e9d85076ccbf9cfef42e Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 27 Apr 2018 16:45:02 +0100 Subject: [PATCH] in/out typing working. Added translateTo function --- src/core/Dish.mjs | 1 + src/node/apiUtils.mjs | 27 ++++++++++++++++++++------- src/node/index.mjs | 3 ++- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index f3c348cb..39a8a7f5 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -39,6 +39,7 @@ class Dish { */ static typeEnum(typeStr) { switch (typeStr.toLowerCase()) { + case "array": case "bytearray": case "byte array": return Dish.BYTE_ARRAY; diff --git a/src/node/apiUtils.mjs b/src/node/apiUtils.mjs index f8457247..a4154be2 100644 --- a/src/node/apiUtils.mjs +++ b/src/node/apiUtils.mjs @@ -35,13 +35,12 @@ export function wrap(Operation) { */ return async (input, args=null) => { const operation = new Operation(); - const dish = new Dish(input); + const dish = new Dish(); - try { - dish.findType(); - } catch (e) { - log.debug(e); - } + // Stolen from Recipe. Only works there as raw input is one + // of these types. consider a mapping for all use cases like below. + const type = input instanceof ArrayBuffer ? Dish.ARRAY_BUFFER : Dish.STRING; + dish.set(input, type); if (!args) { args = operation.args.map(extractArg); @@ -51,11 +50,25 @@ export function wrap(Operation) { args = [args]; } } - const transformedInput = await dish.get(Dish.typeEnum(operation.inputType)); + const transformedInput = await dish.get(operation.inputType); return operation.run(transformedInput, args); }; } +/** + * First draft + * @param input + * @param type + */ +export async function translateTo(input, type) { + const dish = new Dish(); + + const initialType = Dish.typeEnum(input.constructor.name); + + dish.set(input, initialType); + return await dish.get(type); +} + /** * * @param searchTerm diff --git a/src/node/index.mjs b/src/node/index.mjs index 1125685a..cc127f2f 100644 --- a/src/node/index.mjs +++ b/src/node/index.mjs @@ -7,7 +7,7 @@ */ import "babel-polyfill"; -import {wrap, help, decapitalise} from "./apiUtils"; +import {wrap, help, decapitalise, translateTo} from "./apiUtils"; import * as operations from "../core/operations/index"; // Define global environment functions @@ -29,6 +29,7 @@ Object.keys(operations).forEach(op => chef[decapitalise(op)] = wrap(operations[op])); chef.help = help.bind(null, operations); +chef.translateTo = translateTo; export default chef; export {chef};