From 223e2e0b73c8f77ca0e0c9fb71f3c8da1acc1798 Mon Sep 17 00:00:00 2001 From: d98762625 Date: Fri, 17 Aug 2018 17:16:24 +0100 Subject: [PATCH] add Buffer translation to Dish. Cannot work in SyncDish as typeEnums are static. Seeing as they will never be hit in the broswer, shouldnt be an issue --- src/core/Dish.mjs | 19 +++++++++++++++++++ src/node/SyncDish.mjs | 9 ++++++++- src/node/api.mjs | 22 ++++++++++++++++++---- test/tests/nodeApi/ops.mjs | 6 +++--- 4 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/core/Dish.mjs b/src/core/Dish.mjs index 150b70be..da49b6e6 100755 --- a/src/core/Dish.mjs +++ b/src/core/Dish.mjs @@ -61,6 +61,8 @@ class Dish { return Dish.FILE; case "list": return Dish.LIST_FILE; + case "buffer": + return Dish.BUFFER; default: throw "Invalid data type string. No matching enum."; } @@ -93,6 +95,8 @@ class Dish { return "File"; case Dish.LIST_FILE: return "List"; + case Dish.BUFFER: + return "Buffer"; default: throw "Invalid data type enum. No matching type."; } @@ -266,6 +270,8 @@ class Dish { case Dish.LIST_FILE: return this.value instanceof Array && this.value.reduce((acc, curr) => acc && curr instanceof File, true); + case Dish.BUFFER: + return this.value instanceof Buffer; default: return false; } @@ -284,6 +290,7 @@ class Dish { case Dish.BYTE_ARRAY: case Dish.STRING: case Dish.HTML: + case Dish.BUFFER: return this.value.length; case Dish.NUMBER: case Dish.BIG_NUMBER: @@ -357,6 +364,12 @@ class Dish { this.type ); break; + case Dish.BUFFER: + newDish.set( + Buffer.from(this.value), + this.type + ); + break; default: throw new Error("Cannot clone Dish, unknown type"); } @@ -421,6 +434,12 @@ Dish.FILE = 7; * @enum */ Dish.LIST_FILE = 8; +/** +* Dish data type enum for node Buffer. +* @readonly +* @enum +*/ +Dish.BUFFER = 9; export default Dish; diff --git a/src/node/SyncDish.mjs b/src/node/SyncDish.mjs index 536e0f6e..7eed3068 100644 --- a/src/node/SyncDish.mjs +++ b/src/node/SyncDish.mjs @@ -92,6 +92,9 @@ class SyncDish extends Dish { case Dish.JSON: this.value = this.value ? Utils.strToByteArray(JSON.stringify(this.value)) : []; break; + case Dish.BUFFER: + this.value = this.value instanceof Buffer ? this.value.buffer : []; + break; // case Dish.FILE: // this.value = Utils.readFileSync(this.value); // this.value = Array.prototype.slice.call(this.value); @@ -134,6 +137,10 @@ class SyncDish extends Dish { this.value = JSON.parse(byteArrayToStr(this.value)); this.type = Dish.JSON; break; + case Dish.BUFFER: + this.value = Buffer.from(new Uint8Array(this.value)); + this.type = Dish.BUFFER; + break; // case Dish.FILE: // this.value = new File(this.value, "unknown"); // break; @@ -145,7 +152,7 @@ class SyncDish extends Dish { break; } } - } + export default SyncDish; diff --git a/src/node/api.mjs b/src/node/api.mjs index e006076a..87e93f98 100644 --- a/src/node/api.mjs +++ b/src/node/api.mjs @@ -6,6 +6,7 @@ * @license Apache-2.0 */ +import Dish from "../core/Dish"; import SyncDish from "./SyncDish"; import Recipe from "./Recipe"; import OperationConfig from "./config/OperationConfig.json"; @@ -46,11 +47,24 @@ function extractArg(arg) { * * Argument name matching is case and space insensitive * @private - * @param {Object[]} originalArgs - * @param {Object} newArgs + * @param {Object[]} originalArgs - the operation-s args list + * @param {Object} newArgs - any inputted args */ function transformArgs(originalArgs, newArgs) { - const allArgs = Object.assign([], originalArgs); + + // Filter out arg values that are list subheadings - they are surrounded in []. + // See Strings op for example. + const allArgs = Object.assign([], originalArgs).map((a) => { + if (Array.isArray(a.value)) { + a.value = a.value.filter((v) => { + if (typeof v === "string") { + return !v.match(/^\[[\s\S]*\]$/); // Matches anything surrounded in [ ] + } + return true; + }); + } + return a; + }); if (newArgs) { Object.keys(newArgs).map((key) => { @@ -90,7 +104,7 @@ const ensureIsDish = function ensureIsDish(input) { dish = input; } else { dish = new SyncDish(); - const type = SyncDish.typeEnum(input.constructor.name); + const type = Dish.typeEnum(input.constructor.name); dish.set(input, type); } return dish; diff --git a/test/tests/nodeApi/ops.mjs b/test/tests/nodeApi/ops.mjs index 0a969228..4f5fa479 100644 --- a/test/tests/nodeApi/ops.mjs +++ b/test/tests/nodeApi/ops.mjs @@ -158,7 +158,7 @@ Full hash: $2a$10$ODeP1.6fMsb.ENk2ngPUCO7qTGVPyHA9TqDVcyupyed8FjsiF65L6`; it("bit shift left", () => { const result = chef.bitShiftLeft("Keep Your Eyes Peeled"); - assert.strictEqual(result.toString(), ".ÊÊà@²Þêä@.òÊæ@ ÊÊØÊÈ"); + assert.strictEqual(result.toString(), "–ÊÊà@²Þêä@ŠòÊæ@ ÊÊØÊÈ"); }), it("bitShiftRight: number and option", () => { @@ -852,7 +852,7 @@ FROM STATS;`; }), it("strings", () => { - const result = chef.strings("smothering ampersand abreast"); + const result = chef.strings("smothering ampersand abreast", {displayTotal: true}); const expected = `Total found: 1 smothering ampersand abreast @@ -909,7 +909,7 @@ smothering ampersand abreast }), it("to unix timestamp", () => { - assert.strictEqual(chef.toUNIXTimestamp("04/01/2001").toString(), "986083200 (Sun 1 April 2001 00:00:00 UTC)"); + assert.strictEqual(chef.toUNIXTimestamp("04-01-2001").toString(), "986083200 (Sun 1 April 2001 00:00:00 UTC)"); }), it("Translate DateTime format", () => {