Pārlūkot izejas kodu

add test and change name of property to argOptions

d98762625 6 gadi atpakaļ
vecāks
revīzija
a010bba047
2 mainītis faili ar 55 papildinājumiem un 17 dzēšanām
  1. 35 17
      src/node/api.mjs
  2. 20 0
      test/tests/nodeApi/nodeApi.mjs

+ 35 - 17
src/node/api.mjs

@@ -91,6 +91,7 @@ function transformArgs(originalArgs, newArgs) {
     return allArgs.map(extractArg);
 }
 
+
 /**
  * Ensure an input is a SyncDish object.
  * @param input
@@ -107,6 +108,7 @@ function ensureIsDish(input) {
     }
 }
 
+
 /**
  * prepareOp: transform args, make input the right type.
  * Also convert any Buffers to ArrayBuffers.
@@ -127,6 +129,32 @@ function prepareOp(opInstance, input, args) {
     return {transformedInput, transformedArgs};
 }
 
+
+/**
+ * createArgOptions
+ *
+ * Create an object of options for each option or togglestring argument
+ * in the given operation.
+ *
+ * Argument names are converted to camel case for consistency.
+ *
+ * @param {Operation} op - the operation to extract args from
+ * @returns {{}} - arrays of options for option and toggleString args.
+*/
+function createArgOptions(op) {
+    const result = {};
+    op.args.forEach((a) => {
+        if (a.type === "option") {
+            result[sentenceToCamelCase(a.name)] = removeSubheadingsFromArray(a.value);
+        } else if (a.type === "toggleString") {
+            result[sentenceToCamelCase(a.name)] = removeSubheadingsFromArray(a.toggleValues);
+        }
+    });
+
+    return result;
+}
+
+
 /**
  * Wrap an operation to be consumed by node API.
  * Checks to see if run function is async or not.
@@ -182,30 +210,16 @@ export function wrap(OpClass) {
 
     // used in chef.help
     wrapped.opName = OpClass.name;
-
-    /** */
-    const addArgs = (op) => {
-        const result = {};
-        op.args.forEach((a) => {
-            if (a.type === "option") {
-                result[sentenceToCamelCase(a.name)] = removeSubheadingsFromArray(a.value);
-            } else if (a.type === "toggleString") {
-                result[sentenceToCamelCase(a.name)] = removeSubheadingsFromArray(a.toggleValues);
-            }
-        });
-
-        return result;
-    };
-
-    wrapped.args = addArgs(opInstance);
+    wrapped.argOptions = createArgOptions(opInstance);
 
     return wrapped;
 }
 
+
 /**
- * @namespace Api
  * help: Give information about operations matching the given search term,
  * or inputted operation.
+ *
  * @param {String || wrapped operation} input - the name of the operation to get help for.
  * Case and whitespace are ignored in search.
  * @returns {Object[]} Config of matching operations.
@@ -260,6 +274,7 @@ export function help(input) {
     return null;
 }
 
+
 /**
  * bake [Wrapped] - Perform an array of operations on some input.
  * @param operations array of chef's operations (used in wrapping stage)
@@ -283,7 +298,10 @@ export function bake(operations){
     };
 }
 
+
 /**
+ * explainExcludedFunction
+ *
  * Explain that the given operation is not included in the Node.js version.
  * @param {String} name - name of operation
  */

+ 20 - 0
test/tests/nodeApi/nodeApi.mjs

@@ -383,6 +383,26 @@ TestRegister.addApiTests([
             assert.strictEqual(e.type, "ExcludedOperationError");
             assert.strictEqual(e.message, "Sorry, the RenderImage operation is not available in the Node.js version of CyberChef.");
         }
+    }),
+
+    it("Operation arguments: should be accessible from operation object if op has array arg", () => {
+        assert.ok(chef.toCharcode.argOptions);
+        assert.equal(chef.unzip.argOptions, undefined);
+    }),
+
+    it("Operation arguments: should have key for each array-based argument in operation", () => {
+        assert.ok(chef.convertDistance.argOptions.inputUnits);
+        assert.ok(chef.convertDistance.argOptions.outputUnits);
+
+        assert.ok(chef.bitShiftRight.argOptions.type);
+        // is a number type, so not included.
+        assert.equal(chef.bitShiftRight.argOptions.amount, undefined);
+    }),
+
+    it("Operation arguments: should list all options excluding subheadings", () => {
+        // First element (subheading) removed
+        assert.equal(chef.convertDistance.argOptions.inputUnits[0], "Nanometres (nm)");
+        assert.equal(chef.defangURL.argOptions.process[1], "Only full URLs");
     })
 
 ]);