瀏覽代碼

export help function on chef object

d98762625 7 年之前
父節點
當前提交
e04f66f599
共有 3 個文件被更改,包括 64 次插入32 次删除
  1. 36 31
      src/node/apiUtils.mjs
  2. 9 1
      src/node/config/scripts/generateNodeIndex.mjs
  3. 19 0
      test/tests/nodeApi/nodeApi.mjs

+ 36 - 31
src/node/apiUtils.mjs

@@ -77,7 +77,7 @@ function transformArgs(originalArgs, newArgs) {
  * @returns {Function} The operation's run function, wrapped in
  * some type conversion logic
  */
-export function wrap(opClass) {
+export function wrap(OpClass) {
     /**
      * Wrapped operation run function
      * @param {*} input
@@ -86,7 +86,7 @@ export function wrap(opClass) {
      * @throws {OperationError} if the operation throws one.
      */
     return (input, args=null) => {
-        const operation = new opClass();
+        const operation = new OpClass();
 
         let dish;
         if (input instanceof SyncDish) {
@@ -107,6 +107,26 @@ export function wrap(opClass) {
     };
 }
 
+
+/**
+ * SomeName => someName
+ * @param {String} name - string to be altered
+ * @returns {String} decapitalised
+ */
+export function decapitalise(name) {
+    // Don't decapitalise names that start with 2+ caps
+    if (/^[A-Z0-9]{2,}/g.test(name)) {
+        return name;
+    }
+    // reserved. Don't change for now.
+    if (name === "Return") {
+        return name;
+    }
+
+    return `${name.charAt(0).toLowerCase()}${name.substr(1)}`;
+}
+
+
 /**
  * Extract properties from an operation by instantiating it and
  * returning some of its properties for reference.
@@ -116,7 +136,7 @@ export function wrap(opClass) {
 function extractOperationInfo(Operation) {
     const operation = new Operation();
     return {
-        name: operation.name,
+        name: decapitalise(operation.name).replace(/ /g, ""),
         module: operation.module,
         description: operation.description,
         inputType: operation.inputType,
@@ -133,37 +153,22 @@ function extractOperationInfo(Operation) {
 
 /**
  * @namespace Api
- * @param {Object} operations - an object filled with operations.
+ * @param {Operation[]} operations - an object filled with operations.
  * @param {String} searchTerm - the name of the operation to get help for.
  * Case and whitespace are ignored in search.
- * @returns {Object} listing properties of function
+ * @returns {Function} taking search term and outputting description.
  */
-export function help(operations, searchTerm) {
-    if (typeof searchTerm === "string") {
-        const operation = operations[Object.keys(operations).find(o =>
-            o.toLowerCase() === searchTerm.replace(/ /g, "").toLowerCase())];
-        if (operation) {
-            return extractOperationInfo(operation);
+export function help(operations) {
+    return function(searchTerm) {
+        if (typeof searchTerm === "string") {
+            const operation = operations
+                .find(o => o.name.toLowerCase() === searchTerm.replace(/ /g, "").toLowerCase());
+            if (operation) {
+                return extractOperationInfo(operation);
+            }
+            return null;
         }
-    }
-    return null;
+        return null;
+    };
 }
 
-
-/**
- * SomeName => someName
- * @param {String} name - string to be altered
- * @returns {String} decapitalised
- */
-export function decapitalise(name) {
-    // Don't decapitalise names that start with 2+ caps
-    if (/^[A-Z0-9]{2,}/g.test(name)) {
-        return name;
-    }
-    // reserved. Don't change for now.
-    if (name === "Return") {
-        return name;
-    }
-
-    return `${name.charAt(0).toLowerCase()}${name.substr(1)}`;
-}

+ 9 - 1
src/node/config/scripts/generateNodeIndex.mjs

@@ -39,7 +39,7 @@ let code = `/**
 
 
 import "babel-polyfill";
-import { wrap } from "./apiUtils";
+import { wrap, help } from "./apiUtils";
 import {
 `;
 
@@ -79,6 +79,14 @@ code += `    };
 }
 
 const chef = generateChef();
+chef.help = help([\n`;
+
+includedOperations.forEach((op) => {
+    code += `    core_${op},\n`;
+});
+
+code +=`]);
+
 `;
 
 includedOperations.forEach((op) => {

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

@@ -108,4 +108,23 @@ TestRegister.addApiTests([
         const result = chef.fromBase32(chef.toBase32("32"));
         assert.equal(3 + result, 35);
     }),
+
+    it("chef.help: should exist", () => {
+        assert(chef.help);
+    }),
+
+    it("chef.help: should describe a operation", () => {
+        const result = chef.help("tripleDESDecrypt");
+        assert.strictEqual(result.name, "tripleDESDecrypt");
+        assert.strictEqual(result.module, "Ciphers");
+        assert.strictEqual(result.inputType, "string");
+        assert.strictEqual(result.outputType, "string");
+        assert.strictEqual(result.description, "Triple DES applies DES three times to each block to increase key size.<br><br><b>Key:</b> Triple DES uses a key length of 24 bytes (192 bits).<br>DES uses a key length of 8 bytes (64 bits).<br><br><b>IV:</b> The Initialization Vector should be 8 bytes long. If not entered, it will default to 8 null bytes.<br><br><b>Padding:</b> In CBC and ECB mode, PKCS#7 padding will be used.");
+        assert.strictEqual(result.args.length, 5);
+    }),
+
+    it("chef.help: null for invalid operation", () => {
+        const result = chef.help("some invalid function name");
+        assert.strictEqual(result, null);
+    }),
 ]);