Skip to content

Commit

Permalink
Add calculate() function that evaluates a string equation (#69)
Browse files Browse the repository at this point in the history
# 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 🔍

<img width="915" alt="Screen Shot 2023-11-30 at 01 16 32"
src="https://github.com/oxeanbits/parsec/assets/7637806/310850f9-6f03-4035-ba5a-e90456a546c6">

# Checks ☑️

- [x] Add calculate() tests
- [x] Add equations-parser changes
- [x] Update README.md
  • Loading branch information
Victorcorcos authored Dec 6, 2023
1 parent 801d4cc commit 3701864
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified ext/libnativemath/libnativemath.bundle
Binary file not shown.

0 comments on commit 3701864

Please sign in to comment.