From fa87fc8325d2c10bc271d81e6c5f292723dc5507 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 12 Oct 2018 11:30:41 +0100 Subject: [PATCH] replace operations on Dish with apply --- src/node/SyncDish.mjs | 15 ++++++++++----- test/tests/nodeApi/nodeApi.mjs | 20 +++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/node/SyncDish.mjs b/src/node/SyncDish.mjs index b95e3e1f..b1667198 100644 --- a/src/node/SyncDish.mjs +++ b/src/node/SyncDish.mjs @@ -9,7 +9,6 @@ import Utils from "../core/Utils"; import Dish from "../core/Dish"; import BigNumber from "bignumber.js"; import log from "loglevel"; -import * as ops from "./index"; /** * Subclass of Dish where `get` and `_translate` are synchronous. @@ -32,11 +31,17 @@ class SyncDish extends Dish { } super(inputOrDish, type); + } - // Add operations to make it composable - for (const op in ops) { - this[op] = () => ops[op](this.value); - } + /** + * Apply the inputted operation to the dish. + * + * @param {WrappedOperation} operation the operation to perform + * @param {*} args - any arguments for the operation + * @returns {Dish} a new dish with the result of the operation. + */ + apply(operation, args=null) { + return operation(this.value, args); } /** diff --git a/test/tests/nodeApi/nodeApi.mjs b/test/tests/nodeApi/nodeApi.mjs index 849b5297..2d1d57aa 100644 --- a/test/tests/nodeApi/nodeApi.mjs +++ b/test/tests/nodeApi/nodeApi.mjs @@ -17,7 +17,7 @@ import OperationError from "../../../src/core/errors/OperationError"; import SyncDish from "../../../src/node/SyncDish"; import fs from "fs"; -import { toBase32, Dish } from "../../../src/node/index"; +import { toBase32, Dish, SHA3 } from "../../../src/node/index"; import TestRegister from "../../TestRegister"; TestRegister.addApiTests([ @@ -324,18 +324,18 @@ TestRegister.addApiTests([ assert.strictEqual(dish.type, 0); }), - it("Composable Dish: constructed dish should have operation prototype functions", () => { + it("Composable Dish: constructed dish should have apply prototype functions", () => { const dish = new Dish(); - assert.ok(dish.translateDateTimeFormat); - assert.ok(dish.stripHTTPHeaders); + assert.ok(dish.apply); assert.throws(() => dish.someInvalidFunction()); }), it("Composable Dish: composed function returns another dish", () => { - const result = new Dish("some input").toBase32(); + const result = new Dish("some input").apply(toBase32); assert.ok(result instanceof SyncDish); }), + it("Composable dish: infers type from input if needed", () => { const dish = new Dish("string input"); assert.strictEqual(dish.type, 1); @@ -357,6 +357,16 @@ TestRegister.addApiTests([ fs.unlinkSync("test.txt"); }), + it("Composable Dish: apply should allow set of arguments for operation", () => { + const result = new Dish("input").apply(SHA3, {size: "256"}); + assert.strictEqual(result.toString(), "7640cc9b7e3662b2250a43d1757e318bb29fb4860276ac4373b67b1650d6d3e3"); + }), + + it("Composable Dish: apply functions can be chained", () => { + const result = new Dish("input").apply(toBase32).apply(SHA3, {size: "224"}); + assert.strictEqual(result.toString(), "493e8136b759370a415ef2cf2f7a69690441ff86592aba082bc2e2e0"); + }), + it("Excluded operations: throw a sensible error when you try and call one", () => { try { chef.fork();