1
0
mirror of synced 2024-12-11 22:45:58 +01:00

replace operations on Dish with apply

This commit is contained in:
d98762625 2018-10-12 11:30:41 +01:00
parent 48f3a3b18f
commit fa87fc8325
2 changed files with 25 additions and 10 deletions

View File

@ -9,7 +9,6 @@ import Utils from "../core/Utils";
import Dish from "../core/Dish"; import Dish from "../core/Dish";
import BigNumber from "bignumber.js"; import BigNumber from "bignumber.js";
import log from "loglevel"; import log from "loglevel";
import * as ops from "./index";
/** /**
* Subclass of Dish where `get` and `_translate` are synchronous. * Subclass of Dish where `get` and `_translate` are synchronous.
@ -32,11 +31,17 @@ class SyncDish extends Dish {
} }
super(inputOrDish, type); 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);
} }
/** /**

View File

@ -17,7 +17,7 @@ import OperationError from "../../../src/core/errors/OperationError";
import SyncDish from "../../../src/node/SyncDish"; import SyncDish from "../../../src/node/SyncDish";
import fs from "fs"; import fs from "fs";
import { toBase32, Dish } from "../../../src/node/index"; import { toBase32, Dish, SHA3 } from "../../../src/node/index";
import TestRegister from "../../TestRegister"; import TestRegister from "../../TestRegister";
TestRegister.addApiTests([ TestRegister.addApiTests([
@ -324,18 +324,18 @@ TestRegister.addApiTests([
assert.strictEqual(dish.type, 0); 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(); const dish = new Dish();
assert.ok(dish.translateDateTimeFormat); assert.ok(dish.apply);
assert.ok(dish.stripHTTPHeaders);
assert.throws(() => dish.someInvalidFunction()); assert.throws(() => dish.someInvalidFunction());
}), }),
it("Composable Dish: composed function returns another dish", () => { 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); assert.ok(result instanceof SyncDish);
}), }),
it("Composable dish: infers type from input if needed", () => { it("Composable dish: infers type from input if needed", () => {
const dish = new Dish("string input"); const dish = new Dish("string input");
assert.strictEqual(dish.type, 1); assert.strictEqual(dish.type, 1);
@ -357,6 +357,16 @@ TestRegister.addApiTests([
fs.unlinkSync("test.txt"); 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", () => { it("Excluded operations: throw a sensible error when you try and call one", () => {
try { try {
chef.fork(); chef.fork();