Skip to content

Added calculate to command entry for numbers - #66

Draft
Brett-Parker wants to merge 4 commits into
dubstar-04:mainfrom
Brett-Parker:calculate-commandline
Draft

Added calculate to command entry for numbers#66
Brett-Parker wants to merge 4 commits into
dubstar-04:mainfrom
Brett-Parker:calculate-commandline

Conversation

@Brett-Parker

Copy link
Copy Markdown
Contributor
Screencast.from.16-02-23.21.16.50.webm

New feature for calculating values as you enter the command.

Works for anything that is a number

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

Base: 14.34% // Head: 14.35% // Increases project coverage by +0.01% 🎉

Coverage data is based on head (b53cfc1) compared to base (6eaebc2).
Patch coverage: 19.35% of modified lines in pull request are covered.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #66      +/-   ##
==========================================
+ Coverage   14.34%   14.35%   +0.01%     
==========================================
  Files          43       43              
  Lines        5555     5585      +30     
  Branches     1111     1119       +8     
==========================================
+ Hits          797      802       +5     
- Misses       4272     4295      +23     
- Partials      486      488       +2     
Impacted Files Coverage Δ
core/lib/commandLine.js 38.36% <19.35%> (-5.05%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Brett-Parker

Copy link
Copy Markdown
Contributor Author

@dubstar-04 complete

@dubstar-04

dubstar-04 commented Feb 17, 2023

Copy link
Copy Markdown
Owner

Cool feature. Need to consider the following:

Order of operations (BIDMAS) :
4+2*5 = 14 but calculates linearly to 30

Negative numbers don't return a result:
-100+50

Comment thread core/lib/commandLine.js

calculate(string) {
// define the regular expression to match the operators
const operators = /^(\d+(\.\d+)?|\((\s*\d+(\.\d+)?\s*[\+\-\*\/]\s*)+\d+(\.\d+)?\))(\s*[\+\-\*\/]\s*(\d+(\.\d+)?|\((\s*\d+(\.\d+)?\s*[\+\-\*\/]\s*)+\d+(\.\d+)?\)))*$/;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be able to distil this down to a much simpler expression.

@dubstar-04

Copy link
Copy Markdown
Owner

it could be possible to simplify calculations.

This is janky but shows an extreme example:

const input = '4+2*5';
const calculation = Function('return ' + input)();
console.log('calculation: ', calculation);

@Brett-Parker

Brett-Parker commented Feb 17, 2023

Copy link
Copy Markdown
Contributor Author

(BIDMAS)

This is standard method of calculating without brackets. This is also how a calculator would return the answer if you typed it in literally. Should handle better though.

Guidance on this;

image

Will look at a better solution.

@Brett-Parker
Brett-Parker marked this pull request as draft February 17, 2023 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants