Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add custom operator with logic ? #52

Closed
TBSten opened this issue Mar 11, 2022 · 4 comments · Fixed by #53
Closed

how to add custom operator with logic ? #52

TBSten opened this issue Mar 11, 2022 · 4 comments · Fixed by #53
Labels

Comments

@TBSten
Copy link

TBSten commented Mar 11, 2022

I want to add custom operator with logic .

For example ,

const expr = `` ;
cosnt ast = parse(expr) ;
evaluate(ast,{});

In this example, I want it to return 1024, which is 2 to the 10th power, like the "^" operator in Excel.

I tried creating an original plugin like below .

const myPlugin = {
  name:"my plugin",
  init(){
    jsep.addBinaryOp("^",10);
  }
}
jsep.plugins.register(myPlugin)

However, I don't know where to write the essential logic of "finding exponentiation".

Please tell me how to add a custom operator with logic

@TBSten
Copy link
Author

TBSten commented Mar 11, 2022

Was self resolved.

I was always looking at require("jse-eval").jsep instead of require("jse-eval").

I read the README and realized that I should use addEvaluator.

@TBSten TBSten closed this as completed Mar 11, 2022
@6utt3rfly
Copy link
Owner

Sorry for the confusion @TBSten , but glad you figured it out! I'll try to update the readme with some better examples for extending functionality and your use case would be a great example 🙂

6utt3rfly added a commit that referenced this issue Mar 13, 2022
Can now call `JseEval.registerPlugin` (same as `JseEval.jsep.plugins.register(...)`). This method will now look at the plugin argument(s) and check for `init()` to call `jsep.plugins.register`, as well as look for `initEval()` to call that method so it can extend JseEval as needed.

Updated ReadMe to add more extension examples

Fixes #52
6utt3rfly added a commit that referenced this issue Mar 13, 2022
Can now call `JseEval.registerPlugin` (same as `JseEval.jsep.plugins.register(...)`). This method will now look at the plugin argument(s) and check for `init()` to call `jsep.plugins.register`, as well as look for `initEval()` to call that method so it can extend JseEval as needed.

Added one-line parse + evaluate, `evalExpr(expressionString, context)` and `evalExprAsync()`. Made `context` optional for expressions like `1 + 2` which don't require a context.

Updated ReadMe to add more extension examples

Fixes #52
github-actions bot pushed a commit that referenced this issue Mar 13, 2022
# [1.3.0](v1.2.0...v1.3.0) (2022-03-13)

### Features

* add `registerPlugin` method ([86939ac](86939ac)), closes [#52](#52)
@github-actions
Copy link

🎉 This issue has been resolved in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@TBSten
Copy link
Author

TBSten commented Mar 13, 2022

Thank you for your response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants