WebAssembly and JavaScript
loadPUQ exposes the native PUQ conversion command to JavaScript. The
WebAssembly build also powers the live converter on the
Conversion and unit systems page.
Basic examples
After building the web module, import its loader and convert an energy:
import { loadPUQ } from "./bindings/web/index.mjs";
const puq = await loadPUQ();
console.log(puq.convert("35*eV", "J")); // 5.60762e-18*J
Select unit systems for a contextual conversion:
import { loadPUQ } from "./bindings/web/index.mjs";
const puq = await loadPUQ();
console.log(puq.convert("12*statA", "A", {
inputSystem: "ESU", outputSystem: "SI", outputQuantity: "I",
}));
Common workflows
Task |
Call chain |
Result |
|---|---|---|
Initialize PUQ |
|
Runtime object |
Discover systems |
|
|
Convert with SI defaults |
|
Formatted |
Convert with a system context |
|
Formatted |
Handle a rejected conversion |
|
|
const puq = await loadPUQ(); // runtime object
const systems = puq.systems(); // string[]
const result = puq.convert("12*statA", "A", {
inputSystem: "ESU", outputSystem: "SI", outputQuantity: "I",
}); // string
try {
puq.convert("1*m", "s");
} catch (error) {
console.error(error.message, error.details, error.suggestion);
}
See the WebAssembly guide for building the module and serving its JavaScript and WASM files.
Current limitations and differences
Compared with the canonical C++ implementation:
The binding exposes the C++ conversion command, not the mutable
Quantityobject model. Results are formatted strings rather than numerical values with separate unit and uncertainty fields.Conversion retains uncertainties present in the input. General quantity arithmetic, correlation, Monte Carlo sampling, custom-unit registration, and full unit-definition listings are not exposed.
Loaders initialize WebAssembly asynchronously. Unit-system choices are per-call options rather than native C++ context objects.
Binding reference
This is a lookup index, not a sequence to read from top to bottom. Use Common workflows above to choose a task, then find the exact JavaScript name below. Methods are shown with parentheses; properties and option fields are shown without them. The TypeScript declaration file is the complete source for option and result shapes.
loadPUQ(options?)loads the WebAssembly runtime.puq.systems()returns the supported unit-system abbreviations.puq.convert(expression, outputUnits, options?)returns formatted text.options.inputSystem,options.outputSystem, andoptions.outputQuantityare optional strings.Rejected conversions throw an
Errorwithmessage,details, andsuggestionfields.