mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 00:06:17 -04:00
Converted the core to ES modules
This commit is contained in:
parent
c1bb93eec1
commit
9b4fc3d3aa
154 changed files with 1901 additions and 2223 deletions
109
src/core/Ingredient.mjs
Executable file
109
src/core/Ingredient.mjs
Executable file
|
@ -0,0 +1,109 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2016
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import Utils from "./Utils";
|
||||
|
||||
/**
|
||||
* The arguments to operations.
|
||||
*/
|
||||
class Ingredient {
|
||||
|
||||
/**
|
||||
* Ingredient constructor
|
||||
*
|
||||
* @param {Object} ingredientConfig
|
||||
*/
|
||||
constructor(ingredientConfig) {
|
||||
this.name = "";
|
||||
this.type = "";
|
||||
this._value = null;
|
||||
|
||||
if (ingredientConfig) {
|
||||
this._parseConfig(ingredientConfig);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads and parses the given config.
|
||||
*
|
||||
* @private
|
||||
* @param {Object} ingredientConfig
|
||||
*/
|
||||
_parseConfig(ingredientConfig) {
|
||||
this.name = ingredientConfig.name;
|
||||
this.type = ingredientConfig.type;
|
||||
this.defaultValue = ingredientConfig.value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of the Ingredient as it should be displayed in a recipe config.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
get config() {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the value of the Ingredient.
|
||||
*
|
||||
* @param {*} value
|
||||
*/
|
||||
set value(value) {
|
||||
this._value = Ingredient.prepare(value, this.type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the value of the Ingredient.
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
get value() {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Most values will be strings when they are entered. This function converts them to the correct
|
||||
* type.
|
||||
*
|
||||
* @param {*} data
|
||||
* @param {string} type - The name of the data type.
|
||||
*/
|
||||
static prepare(data, type) {
|
||||
let number;
|
||||
|
||||
switch (type) {
|
||||
case "binaryString":
|
||||
case "binaryShortString":
|
||||
case "editableOption":
|
||||
return Utils.parseEscapedChars(data);
|
||||
case "byteArray":
|
||||
if (typeof data == "string") {
|
||||
data = data.replace(/\s+/g, "");
|
||||
return Utils.fromHex(data);
|
||||
} else {
|
||||
return data;
|
||||
}
|
||||
case "number":
|
||||
number = parseFloat(data);
|
||||
if (isNaN(number)) {
|
||||
const sample = Utils.truncate(data.toString(), 10);
|
||||
throw "Invalid ingredient value. Not a number: " + sample;
|
||||
}
|
||||
return number;
|
||||
default:
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Ingredient;
|
Loading…
Add table
Add a link
Reference in a new issue