{"id":20132377,"url":"https://github.com/stephtr/libqalculate-wasm","last_synced_at":"2025-04-09T16:53:06.670Z","repository":{"id":261315437,"uuid":"864667911","full_name":"stephtr/libqalculate-wasm","owner":"stephtr","description":"libqalculate-wasm is a WebAssembly (WASM) port of the libqalculate library, a powerful calculation library.","archived":false,"fork":false,"pushed_at":"2024-12-19T17:54:44.000Z","size":47,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-23T19:03:08.153Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stephtr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-28T20:45:14.000Z","updated_at":"2024-12-19T17:54:48.000Z","dependencies_parsed_at":"2024-11-05T22:21:31.915Z","dependency_job_id":"25e711e4-d1bd-4787-8c5d-68968e6d55f9","html_url":"https://github.com/stephtr/libqalculate-wasm","commit_stats":null,"previous_names":["stephtr/libqalculate-wasm"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephtr%2Flibqalculate-wasm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephtr%2Flibqalculate-wasm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephtr%2Flibqalculate-wasm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stephtr%2Flibqalculate-wasm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stephtr","download_url":"https://codeload.github.com/stephtr/libqalculate-wasm/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247974600,"owners_count":21026743,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T20:53:23.238Z","updated_at":"2025-04-09T16:53:06.649Z","avatar_url":"https://github.com/stephtr.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# libqalculate-wasm\n\nlibqalculate-wasm is a WebAssembly (WASM) port of the [libqalculate](https://github.com/Qalculate/libqalculate/) library, a powerful calculation library.\n\nWARNING: This is a first, rough preview version. The API _will_ change.\n\n## Installation\n\nTo install libqalculate-wasm, you can use npm:\n\n```bash\nnpm install libqalculate-wasm\n```\n\n## Usage\n\nHere is a simple example of how to use libqalculate-wasm in your project:\n\n```javascript\nimport loadLibqalculate from \"libqalculate-wasm\";\n\nconst libqalculate = await loadLibqalculate();\nconst result = libqalculate.calculate(\n  \"2 + 2\",\n  300 /* timeout */,\n  0 /* option flags */\n);\n\ndocument.getElementById(\"result\").innerHTML = result.output;\n```\n\n`loadLibqalculate` loads the ~5 MB large WebAssembly file, which is included in the node_modules folder but must be served on the webserver.\nOne way to achieve that is to copy it into your public folder during package installation, by adding the following to your `package.json` file:\n\n```JSON\n{\n  ...\n  \"scripts\": {\n    \"dev\": ...,\n    ...,\n    \"postinstall\": \"cp ./node_modules/libqalculate-wasm/libqalculate.wasm ./public/libqalculate.wasm\"\n  },\n  ...\n}\n```\n\nDepending on the framework/bundler in use, you might have to specify the location where the wasm file can be loaded, by adapting the `loadOptions` of `loadLibqalculate`:\n\n```javascript\nconst libqalculate = await loadLibqalculate({\n  locateFile: (path: string, prefix: string) =\u003e `/${path}`,\n});\n```\n\n(example in case of the libqalculate.wasm file being served from the root directory)\n\n## API\n\nWARNING: This is a first, rough preview version. The API _will_ change (see the [Changelog](CHANGELOG.md)).\n\nFirst, libqalculate has to be loaded by using `loadLibqalculate`, the default export of this library.\n\n```TS\ninterface LoadingOptions {\n  locateFile(prefix: string, path: string): string;\n}\n\nexport default function loadLibqalculate(loadingOptions?: LoadingOptions): Promise\u003cMainModule\u003e;\n```\n\nThe `MainModule` provides several functions for interaction with libqalculate:\n\n```TS\n/**\n * Runs a calculation\n * @argument calculation: Input as string, for example `\"1 m + 5 mm\"`\n * @argument timeout: Calculation timeout in milliseconds; 0 for no timeout\n * @argument optionFlags: optional options, will be changed/removed soon; for now just enter 0\n * @returns the calculation result\n */\ncalculate(calculation: string, timeout: number, optionFlags: number): CalculationResult;\n\ninterface CalculationResult {\n  /** A HTML string containing the parsed \u0026 formatted calculation input */\n  input: string;\n  /** A HTML string containing the formatted calculation result */\n  output: string;\n  /** Warnings or error messages which were thrown during calculation */\n  messages: string[];\n  /**\n   *  In case the calculation input contained a `plot` command, the resulting plot data\n   *  `commands` contains the plot commands intended for gnuplot, including curve IDs and labels\n   *  `data` contains the actual points for each curve ID.\n   *      The points are stored in a string, one point per line, x \u0026 y coordinates separated by a space\n  */\n  plotData: { data: Record\u003cstring, string\u003e; commands: string } | undefined;\n}\n\n\n/**\n * Provide libqalculate with new exchange rates.\n * @param currencyData The exchange rates; for example: [{ name: 'USD', value: 1.2345 }] =\u003e 1 USD = 1.2345 EUR\n * @param baseCurrency The currency relative to which the exchange rates are given; currently only `EUR` is supported.\n * @param showWarning Whether to show warnings about outdated exchange rates\n * @returns whether the update was successful\n */\nupdateCurrencyValues(currencyData: Array\u003c{ name: string, value: string }\u003e, baseCurrency: 'EUR', showWarning: boolean): boolean;\n\n\n/**\n * Returns all variables/constants known to libqalculate\n */\ngetVariables(): Array\u003c{\n/** name of the variable, for example \"π\" */\nname: string;\n/** description, for example \"Archimedes' Constant (pi)\" */\ndescription: string;\n/** aliases of the variable, for example [\"π\", \"pi\"] */\naliases: string[];\n}\u003e;\n\n\n/**\n * Set options with command strings like `angle 2`, `unit on` or `limit implicit multiplication off`\n * For a list of possible options, see https://qalculate.github.io/manual/qalc.html#SETTINGS\n */\nset_option(str: string): boolean;\n```\n\n## Usage example with React \u0026 compatibility with SSR\n\n```TSX\n'use client';\n\nimport { use, useState } from 'react';\nimport loadLibqalculate from 'libqalculate-wasm';\n\n// Since it's interactive, let's not load libqalculate during SSR.\nconst libqalculatePromise = typeof window !== 'undefined' ? loadLibqalculate({\n    // important: libqalculate.wasm has to be copied to the public folder\n    locateFile: (path: string) =\u003e `/${path}`, // Absolute URL\n}) : Promise.resolve(null);\n\nexport function Calculator() {\n    const [input, setInput] = useState('');\n    const libqalculate = use(libqalculatePromise);\n\n    const calculation = input ? libqalculate?.calculate(input, 0, 0) : null;\n    const plotDataset = calculation?.plotData ? processPlotData(calculation.plotData) : null;\n    return (\n        \u003cdiv\u003e\n            \u003cinput title=\"Calculation\" className=\"border-gray-600 border-2\" value={input} onChange={(e) =\u003e setInput(e.currentTarget.value)} /\u003e\n            {calculation \u0026\u0026 \u003cdiv\u003e\u003cspan dangerouslySetInnerHTML={{ __html: calculation.input }} /\u003e = \u003cspan dangerouslySetInnerHTML={{ __html: calculation.output }} /\u003e\u003c/div\u003e}\n        \u003c/div\u003e\n    );\n}\n```\n\n## Development\n\nEmscripten is being used for compiling [libqalculate.cc](src/libqalculate.cc) (the glue between libqalculate and JavaScript). A docker image fulfilling the dependencies and including the precompiled libraries is available at [GitHub's container registry](ghcr.io/stephtr/libqalculate-wasm), automatically built from this repository's [Dockerfile](Dockerfile).\n\nFor compilation, just run `./build-wasm.sh`. It automatically spins up the docker container and generates the libqalculate.wasm/.js files.\n\n## License\n\nThis project is licensed under the GPL-3.0 License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephtr%2Flibqalculate-wasm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstephtr%2Flibqalculate-wasm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephtr%2Flibqalculate-wasm/lists"}