6utt3rfly / jse-eval

Javascript Expression Evaluator
MIT License
22 stars 7 forks source link

how to add custom operator with logic ? #52

Closed TBSten closed 2 years ago

TBSten commented 2 years ago

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 commented 2 years ago

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.

6utt3rfly commented 2 years ago

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 🙂

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 1.3.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

TBSten commented 2 years ago

Thank you for your response!