qalculator icon indicating copy to clipboard operation
qalculator copied to clipboard

Modularizing libqalculate-wasm and publishing on npm

Open lrq3000 opened this issue 2 years ago • 5 comments

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!

lrq3000 avatar Nov 08 '23 19:11 lrq3000

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?

stephtr avatar Nov 08 '23 22:11 stephtr

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!

lrq3000 avatar Nov 09 '23 08:11 lrq3000

I hope I'll have time for it over the weekend :)

stephtr avatar Nov 10 '23 16:11 stephtr

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!

lrq3000 avatar Nov 10 '23 16:11 lrq3000

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

lrq3000 avatar Nov 14 '23 17:11 lrq3000

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.

stephtr avatar Sep 20 '24 10:09 stephtr

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/).

stephtr avatar Sep 29 '24 01:09 stephtr

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)

lrq3000 avatar Oct 11 '24 22:10 lrq3000

Yes, exactly.

stephtr avatar Oct 12 '24 10:10 stephtr