1

I am trying to use math.js in Postman.

Already saw Tip#5 in their website. So, in one request I have

postman.setGlobalVariable("mathjs", () => {
  \\ The full math.js library
});

Specifically, this is the code of math.js that I copied, in case the version matters.

Then in a request that is supposed to use the library I evaluate the global variable

eval(globals.mathjs)();

I don't use JavaScript often, so maybe it is something basic that I am missing. In the first request a global variable mahjs is defined, which value is a lambda that calls the code of the library. Then, in the second request, that lambda function is called. Please, correct me if my understanding so far is not correct.

Question: How does one call afterwards functions that were defined by the library?

I have tried: math.multiply(x,y);, Math.multiply(x,y);, multiply(x,y);. None of them are valid. The function multiply seems to be defined by the library and is used as math.multiply(array, matrix) .


Comparison with the reuse that I have already made work.

In one request

postman.setGlobalVariable("utils", () => {
  myfunction = function (x){
    return x+1;
  };
});

and in the request that uses it

eval(globals.utils)();
x = 1;
console.log(myfunction(x));

This works.

plop
  • 283
  • 2
  • 10
  • What's the problem you're trying to solve? – Danny Dainton Nov 02 '20 at 21:37
  • @DannyDainton Ultimately, calling functions that are defined in `math.js` in some tests in Postman. – plop Nov 02 '20 at 21:41
  • I get that part but what data points are you trying to create from math.js? Seems a little over the top if all you're using it for is to multiply numbers. Can you update the question to expand on how you're going to be using the data in the requests, there might be a different route that you could take here. – Danny Dainton Nov 02 '20 at 21:47
  • @DannyDainton No, well I will do more complicated linear algebra. The code above is to show the part that I don't understand (and the part that I did manage to do) with examples as simple as possible. If I learn how to call the functions inside `math.js`, I should be able to write the rest on my own. – plop Nov 02 '20 at 21:49
  • 1
    This method from Kevin Swiber, a Postman Lead Solution Engineer, might be a better way to bring in CDN modules and use them in your collections - https://explore.postman.com/templates/7170/browserify-cdn-modules – Danny Dainton Nov 02 '20 at 22:13
  • @DannyDainton Sounds interesting. I don't understand most of what they are saying but it looks like the 'browserification' of `math.js` looks [like this](https://wzrd.in/standalone/math.js@latest), which seems to have very little of what the library has. – plop Nov 02 '20 at 23:35

1 Answers1

2

This solves your problem:

const mathjsUrl = "https://cdnjs.cloudflare.com/ajax/libs/mathjs/7.5.1/math.min.js";

pm.sendRequest(mathjsUrl, (err, response) => {
    const mathjs = response.text();

    (new Function(mathjs))();
        let result = math.multiply(4,3);;
        console.log(result);
});
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
  • 1
    It does, it does. Nice. It looks like to use the library what was needed was to create the Function object from the library text, instead of putting it in a lambda and calling it. I will try this and see if I can modify it to paste the text directly, instead of requesting it from cloudflare. I think that in my case this will be running in computers that are not connected to the Internet. – plop Nov 03 '20 at 21:07