mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
Merge 82ed84fc76
into 7c8be12d52
This commit is contained in:
commit
de91ca15f9
3 changed files with 117 additions and 0 deletions
|
@ -216,6 +216,7 @@
|
||||||
"AND",
|
"AND",
|
||||||
"ADD",
|
"ADD",
|
||||||
"SUB",
|
"SUB",
|
||||||
|
"Basic Arithmetic",
|
||||||
"Sum",
|
"Sum",
|
||||||
"Subtract",
|
"Subtract",
|
||||||
"Multiply",
|
"Multiply",
|
||||||
|
|
48
src/core/operations/BasicArithmetic.mjs
Normal file
48
src/core/operations/BasicArithmetic.mjs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
/**
|
||||||
|
* @author scottdermott [scottdermott@outlook.com]
|
||||||
|
* @copyright Crown Copyright 2021
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Operation from "../Operation.mjs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic Arithmetic operation
|
||||||
|
*/
|
||||||
|
class BasicArithmetic extends Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BasicArithmetic constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = "Basic Arithmetic";
|
||||||
|
this.module = "Default";
|
||||||
|
this.description = "Evalutes Basic Arithmetic. <br><br>e.g. <code>1+2-1</code> becomes <code>2</code>";
|
||||||
|
this.infoURL = "";
|
||||||
|
this.inputType = "string";
|
||||||
|
this.outputType = "number";
|
||||||
|
this.args = [];
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param {string} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {number}
|
||||||
|
*/
|
||||||
|
run(input, args) {
|
||||||
|
if (parseInt(input, 10).toString().length === input.length) {
|
||||||
|
return parseInt(input, 10);
|
||||||
|
} else if (input.match(/[+-]?([0-9.]+)/g)) {
|
||||||
|
return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || [])
|
||||||
|
.reduce(function (sum, value) {
|
||||||
|
return parseFloat(sum) + parseFloat(value);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return NaN;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default BasicArithmetic;
|
68
tests/operations/tests/BasicArithmetic.mjs
Normal file
68
tests/operations/tests/BasicArithmetic.mjs
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/**
|
||||||
|
* @author scottdermott [scottdermott@outlook.com]
|
||||||
|
* @copyright Crown Copyright 2021
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic Arithmetic Tests
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Basic Arithmetic: nothing",
|
||||||
|
input: "",
|
||||||
|
expectedOutput: "",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Basic Arithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Basic Arithmetic: Addition",
|
||||||
|
input: "1+2+3+4+5+6+7+8+9+0",
|
||||||
|
expectedOutput: 45,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Basic Arithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Basic Arithmetic: Subtraction",
|
||||||
|
input: "100-9-8-7-6-5-4-3-2-1-0",
|
||||||
|
expectedOutput: 55,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Basic Arithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Basic Arithmetic: Add + Sub",
|
||||||
|
input: "1+2+3+4+5+6+7+8+9-9-8-7-6-5-4-3-2-1",
|
||||||
|
expectedOutput: 0,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Basic Arithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Basic Arithmetic: Large number",
|
||||||
|
input: "999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999+999",
|
||||||
|
expectedOutput: 22977,
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "Basic Arithmetic",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
]);
|
Loading…
Add table
Add a link
Reference in a new issue