From 06964eaa05468e0eb458da918a785e479549991d Mon Sep 17 00:00:00 2001 From: Benjamin Wegener Date: Tue, 16 Jan 2024 20:03:25 +0000 Subject: [PATCH] enh(std) enhance standard library plugin with math functionalities --- lib/plugins/StandardLibrary/Math.js | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lib/plugins/StandardLibrary/Math.js diff --git a/lib/plugins/StandardLibrary/Math.js b/lib/plugins/StandardLibrary/Math.js new file mode 100644 index 0000000..6d7b59c --- /dev/null +++ b/lib/plugins/StandardLibrary/Math.js @@ -0,0 +1,60 @@ +function inc(val, options = [1]) { + let [name, amount = 1, ..._rest] = options; + return { + varName: name, + new: val * 1 + amount * 1, + old: val * 1, + }; +} + +function dec(val, options = [1]) { + let [name, amount = 1, ..._rest] = options; + return { + varName: name, + new: val * 1 - amount * 1, + old: val * 1, + }; +} + +function mult(val, options = [1]) { + let [name, amount = 1, ..._rest] = options; + return { + varName: name, + new: val * 1 * amount * 1, + old: val * 1, + }; +} + +function div(val, options = [1]) { + let [name, amount = 1, ..._rest] = options; + return { + varName: name, + new: ((val * 1) / amount) * 1, + old: val * 1, + }; +} + +function pow(val, options = [1]) { + let [name, amount = 1, ..._rest] = options; + return { + varName: name, + new: Math.pow(val * 1, amount * 1), + old: val * 1, + }; +} + +const BasicArithmetic = new Map([ + ['increment', inc], + ['increase', inc], + ['inc', inc], + ['decrement', dec], + ['decrease', dec], + ['dec', dec], + ['multiply', mult], + ['mult', mult], + ['divide', div], + ['div', div], + ['pow', pow], +]); + +export default new Map([...BasicArithmetic.entries()]);