google/mathsteps

Incorrect docs + bug - `step.newNode` is an object, not a string

cristiano-belloni opened this issue · 4 comments

Hi, I'm using "mathsteps": "^0.1.6" I tried the latest commit from master

In the README examples,

console.log("after change: " + step.newNode); // after change: 6 x

Doesn't print 6 x, because step.newNode is an object, like:

  {
    "description": "ADD_POLYNOMIAL_TERMS",
    "next": {
      "implicit": true,
      "op": "*",
      "fn": "multiply",
      "args": [
        {
          "value": "6",
          "valueType": "number"
        },
        {
          "name": "x"
        }
      ],
      "changeGroup": 1
    }
  }

which doesn't even have a .print() method, like in the equation solver case.

How do we get 6 x from the example?

If I require print from utils: const print = require('mathsteps/lib/util/print') and later do print(step.newNode) it works.
Do you envision adding print to nodes or expose it from the mathsteps object, in the future? As it is now, it's inconsistent and undocumented.

("after change: " + step.newNode); should call toString() on the newNode and all mathjs nodes have a toString() property. I'm curious why that wasn't being called on yours, but I agree that it should clearly state toString() so people know how to print it on its own without concatenating to a string :p

Does that work for you?

Thanks for bringing it up!

And around equations - ideally all nodes would have the same print function but the way the tree is set up right now equations and expressions are different objects. We're working on a new parser/tree object so hopefully this changes soon :)

just fixed by cbb5c7f - I'll close this once I know you've got things working :)

It works.
I didn't know how I tested it this morning, but in some occasions I got [object Object] stringifying the node.
But now I cannot repro anymore, so probably the issue was my side. Will reopen if I see that again.