mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 08:16:17 -04:00
+ Adding Basic Arithmetic Operation
This commit is contained in:
parent
ae1b12c120
commit
e4607e97fc
2 changed files with 47 additions and 0 deletions
|
@ -160,6 +160,7 @@
|
||||||
"AND",
|
"AND",
|
||||||
"ADD",
|
"ADD",
|
||||||
"SUB",
|
"SUB",
|
||||||
|
"Basic Arithmetic",
|
||||||
"Sum",
|
"Sum",
|
||||||
"Subtract",
|
"Subtract",
|
||||||
"Multiply",
|
"Multiply",
|
||||||
|
|
46
src/core/operations/BasicArithmetic.mjs
Normal file
46
src/core/operations/BasicArithmetic.mjs
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/**
|
||||||
|
* @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 = "BasicArithmetic";
|
||||||
|
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 {
|
||||||
|
return (input.replace(/\s/g, "").match(/[+-]?([0-9.]+)/g) || [])
|
||||||
|
.reduce(function (sum, value) {
|
||||||
|
return parseFloat(sum) + parseFloat(value);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default BasicArithmetic;
|
Loading…
Add table
Add a link
Reference in a new issue