Modularizing libqalculate-wasm and publishing on npm
Hello again Stephan!
I would like to suggest to modularize libqalculate-wasm into its own repo (it can be linked here with a submodule, so for this repo here it can be just the same as now, no difference in building process of qalculator), and to publish a precompiled version in GitHub Releases and if possible on NPM, so that other projects can use it very easily by reusing a centralized version online.
For example I am building a calculator to avoid dosage miscalculations for babies and newborns (a long time project of mine, but the tech wasn't there before). With libqalculate-wasm, it could theoretically convert transparently most dosages reliably.
I know there are lots of other libraries already available, but why not use libqalculate-wasm now that it exists? ;-)
Thank you for considering my suggestion!
That would be interesting! I'm a bit unsure about the API though. Exposing the whole Calculator object might be a bit overkill and also complex in usage. Have you had a look at the interface I wrote for qalculator (calc.cc)? I guess that would be sufficient for your use case?
Oh i didn't see this interface but yes it would be more than enough for the needs of my app indeed, the calculate function greatly simplifies using libqalculate indeed!
I hope I'll have time for it over the weekend :)
Wow that would be awesome, but no pressure, my use case is not urgent, if it ever happens I would be super happy :D Thank you very much for doing this!
Dear @stephtr , just to let you know that by chance I stumbled on another port of libqalculate to wasm, and interestingly it supports plot(), although it is much less feature rich and out of date compared to your implementation. Maybe merging here some interesting bits there may be interesting?
https://flaviutamas.com/qalculate-wasm/
Source-code: https://github.com/flaviut/qalculate-wasm
Hi & sorry for the delay,
Yes, I'm aware of that and it's a great feature! I'm currently planning a rewrite of qalculator, which should also support plotting.
Yesterday, I again had a look into releasing the package. I now have a working GitHub pipeline, which brings me closer to releasing an npm package.
A first very, very rough preview: https://www.npmjs.com/package/libqalculate-wasm Important to note: There's yet no documentation and the API WILL change.
Usage example
import loadLibqalculate from 'libqalculate-wasm';
loadLibqalculate().then(libqalculate =>
console.log(libqalculate.calculate('1+1', 0 /* timeout */, 0 /* option flags */))
);
Important: You have to serve the libqalculate.wasm file to the browser. For example:
"scripts": {
...,
"postinstall": "cp ./node_modules/libqalculate-wasm/libqalculate.wasm ./public/libqalculate.wasm"
}
Depending on the framework in use, you might also have to adapt the location lookup and change the call to loadLibqalculate accordingly:
loadLibqalculate({
locateFile: function (path: string, prefix: string) {
if (path.endsWith('.wasm')) {
return '/' + path; // Absolute URL
}
return prefix + path;
}
})...
See https://github.com/stephtr/qalculator/blob/v2/components/calculator.tsx for a NextJS example (live at https://v2.qalculator.pages.dev/).
Incredible! Thank you so much @stephtr for making it happen!
The file calculator.tsx returns a 404 error now, was it moved to https://github.com/stephtr/qalculator/blob/v2/components/calculator.tsx ? (permanent URL)
Yes, exactly.