From 3701864b947448754e3b7e3978cdd798fb319936 Mon Sep 17 00:00:00 2001 From: Victor Cordeiro Costa Date: Wed, 6 Dec 2023 14:54:49 -0300 Subject: [PATCH] Add calculate() function that evaluates a string equation (#69) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description ✍️ Upgrade `equations-parser` submodule in order to add the `calculate(string)` function, which evaluates the `string` input as an equation and calculates it's result. # Overview 🔍 Screen Shot 2023-11-30 at 01 16 32 # Checks ☑️ - [x] Add calculate() tests - [x] Add equations-parser changes - [x] Update README.md --- README.md | 16 ++++++++++++++++ ext/libnativemath/libnativemath.bundle | Bin 665616 -> 665616 bytes 2 files changed, 16 insertions(+) diff --git a/README.md b/README.md index 059af8a..eefd8c5 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,22 @@ parser.eval_equation('hoursdiff("2018-01-01", "2018-01-02")') # resu parser.eval_equation('hoursdiff("2019-02-01T08:00", "2019-02-01T12:00")') # result => 4 parser.eval_equation('hoursdiff("2019-02-01T08:20", "2019-02-01T12:00")') # result => 3.67 parser.eval_equation('hoursdiff("2018-01-01", "2018-01-01")') # result => 0 + +# Calculate a string as an equation +parser.eval_equation('calculate("2+2+2*4)"') # result => "12" +parser.eval_equation('calculate("(2+2)*4)"') # result => "16" +parser.eval_equation('calculate("2^4)"') # result => "16" +parser.eval_equation('calculate("sqrt(9))"') # result => "3" +parser.eval_equation('calculate("abs(-50))"') # result => "50" +parser.eval_equation('calculate("round(1.123))"') # result => "1" +parser.eval_equation('calculate("add_days(\"2019-01-01\", 3))"') # result => "2019-01-04" +parser.eval_equation('calculate("daysdiff(\"2019-01-01\", \"2019-01-02\"))"') # result => "1" +parser.eval_equation('calculate("hoursdiff(\"2019-01-01\", \"2019-01-02\"))"') # result => "24" +parser.eval_equation('calculate("3 > 2 ? \"higher\" : \"lower\")"') # result => "higher" +parser.eval_equation('calculate("3 < 2 ? \"higher\" : \"lower\")"') # result => "lower" +parser.eval_equation('calculate("concat(\"One \", concat(\"Two\", \" Three\")))"') # result => "One Two Three" +parser.eval_equation('calculate("\"One\" // \" \" // \"Two\" // \" \" // \"Three\")"') # result => "One Two Three" +parser.eval_equation('calculate("number(calculate(\"1 + 1\")) + 1)"') # result => "3" ``` ### The following functions can be used diff --git a/ext/libnativemath/libnativemath.bundle b/ext/libnativemath/libnativemath.bundle index c80d08b1a0c34669a4b93ac6c023862653466dd8..a40898eeff71a6166ebb81cea5b2ee98fe056589 100755 GIT binary patch delta 41 wcmbPmL1V%NjfNJ+7N!>F7M2#)7Pc+yAO16^mgcp8{?87?9NRzt=akg|0CF=Ds{jB1 delta 41 wcmbPmL1V%NjfNJ+7N!>F7M2#)7Pc+yAO17XWy@&){GT0&IktcP&nc?`0B)fYZU6uP