mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-16 00:48:31 +01:00
add immutable presentAs method to Dish for node REPL display. add test for exact match help
This commit is contained in:
parent
8f5f3e56cb
commit
8eed2232ee
5 changed files with 55 additions and 11 deletions
|
@ -184,6 +184,22 @@ class Dish {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Dish as the given type, without mutating the original dish.
|
||||||
|
*
|
||||||
|
* If running in a browser, get is asynchronous.
|
||||||
|
*
|
||||||
|
* @Node
|
||||||
|
*
|
||||||
|
* @param {number} type - The data type of value, see Dish enums.
|
||||||
|
* @param {boolean} [notUTF8=false] - Do not treat strings as UTF8.
|
||||||
|
* @returns {Dish | Promise} - (Broswer) A promise | (Node) value of dish in given type
|
||||||
|
*/
|
||||||
|
presentAs(type, notUTF8=false) {
|
||||||
|
const clone = this.clone();
|
||||||
|
return clone.get(type, notUTF8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates that the value is the type that has been specified.
|
* Validates that the value is the type that has been specified.
|
||||||
|
|
|
@ -21,12 +21,17 @@ class File {
|
||||||
*
|
*
|
||||||
* https://w3c.github.io/FileAPI/#file-constructor
|
* https://w3c.github.io/FileAPI/#file-constructor
|
||||||
*
|
*
|
||||||
* @param {String|Array|ArrayBuffer|Buffer} bits - file content
|
* @param {String|Array|ArrayBuffer|Buffer|[File]} bits - file content
|
||||||
* @param {String} name (optional) - file name
|
* @param {String} name (optional) - file name
|
||||||
* @param {Object} stats (optional) - file stats e.g. lastModified
|
* @param {Object} stats (optional) - file stats e.g. lastModified
|
||||||
*/
|
*/
|
||||||
constructor(data, name="", stats={}) {
|
constructor(data, name="", stats={}) {
|
||||||
const buffers = data.map(d => Buffer.from(d));
|
const buffers = data.map((d) => {
|
||||||
|
if (d instanceof File) {
|
||||||
|
return Buffer.from(d.data);
|
||||||
|
}
|
||||||
|
return Buffer.from(d);
|
||||||
|
});
|
||||||
const totalLength = buffers.reduce((p, c) => p + c.length, 0);
|
const totalLength = buffers.reduce((p, c) => p + c.length, 0);
|
||||||
this.data = Buffer.concat(buffers, totalLength);
|
this.data = Buffer.concat(buffers, totalLength);
|
||||||
|
|
||||||
|
|
|
@ -53,14 +53,14 @@ class NodeDish extends Dish {
|
||||||
* Avoid coercion to a String primitive.
|
* Avoid coercion to a String primitive.
|
||||||
*/
|
*/
|
||||||
toString() {
|
toString() {
|
||||||
return this.get(Dish.typeEnum("string"));
|
return this.presentAs(Dish.typeEnum("string"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* What we want to log to the console.
|
* What we want to log to the console.
|
||||||
*/
|
*/
|
||||||
[util.inspect.custom](depth, options) {
|
[util.inspect.custom](depth, options) {
|
||||||
return this.get(Dish.typeEnum("string"));
|
return this.presentAs(Dish.typeEnum("string"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -68,14 +68,14 @@ class NodeDish extends Dish {
|
||||||
* Log only the value to the console in node.
|
* Log only the value to the console in node.
|
||||||
*/
|
*/
|
||||||
inspect() {
|
inspect() {
|
||||||
return this.get(Dish.typeEnum("string"));
|
return this.presentAs(Dish.typeEnum("string"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Avoid coercion to a Number primitive.
|
* Avoid coercion to a Number primitive.
|
||||||
*/
|
*/
|
||||||
valueOf() {
|
valueOf() {
|
||||||
return this.get(Dish.typeEnum("number"));
|
return this.presentAs(Dish.typeEnum("number"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,12 +148,23 @@ TestRegister.addApiTests([
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("chef.help: lists name matches before desc matches", () => {
|
it("chef.help: lists name matches before desc matches", () => {
|
||||||
|
const result = chef.help("Checksum");
|
||||||
|
assert.ok(result[0].name.includes("Checksum"));
|
||||||
|
assert.ok(result[1].name.includes("Checksum"));
|
||||||
|
assert.strictEqual(result[result.length - 1].name.includes("Checksum"), false);
|
||||||
|
assert.ok(result[result.length - 1].description.includes("checksum"));
|
||||||
|
}),
|
||||||
|
|
||||||
|
it("chef.help: exact name match only returns one result", () => {
|
||||||
const result = chef.help("MD5");
|
const result = chef.help("MD5");
|
||||||
assert.ok(result[0].name.includes("MD5"));
|
assert.strictEqual(result.length, 1);
|
||||||
assert.strictEqual(result[1].name.includes("MD5"), false);
|
assert.strictEqual(result[0].name, "MD5");
|
||||||
assert.strictEqual(result[2].name.includes("MD5"), false);
|
}),
|
||||||
assert.ok(result[1].description.includes("MD5"));
|
|
||||||
assert.ok(result[2].description.includes("MD5"));
|
it("chef.help: exact match ignores whitespace", () => {
|
||||||
|
const result = chef.help("tobase64");
|
||||||
|
assert.strictEqual(result.length, 1);
|
||||||
|
assert.strictEqual(result[0].name, "To Base64");
|
||||||
}),
|
}),
|
||||||
|
|
||||||
it("chef.bake: should exist", () => {
|
it("chef.bake: should exist", () => {
|
||||||
|
|
12
tests/operations/Dish.mjs
Normal file
12
tests/operations/Dish.mjs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import TestRegister from "../../lib/TestRegister";
|
||||||
|
import Dish from "../../src/core/Dish";
|
||||||
|
import it from "../node/assertionHandler";
|
||||||
|
import assert from "assert";
|
||||||
|
|
||||||
|
TestRegister.addApiTests([
|
||||||
|
it("Dish - presentAs: should exist", () => {
|
||||||
|
const dish = new Dish();
|
||||||
|
assert(dish.presentAs);
|
||||||
|
}),
|
||||||
|
|
||||||
|
]);
|
Loading…
Reference in a new issue