The JavaScript column already includes lodash
, which provides a value named _
with many useful utility functions. lodash
is the most popular JavaScript library in the world, so we included it by default even if you do not use it.
@Jeff_Hager recently requested mathjs
support in the JavaScript column. mathjs
is another top JavaScript library, but including it by default means that every app that uses the JavaScript column would load this library as well, even though very few people need it.
Today I discovered how you can load any JavaScript package within the JavaScript column using skypack.dev and a newer JavaScript feature called async import
.
Example: Load mathjs
to evaluate a complex expression:
// Use await import to load the package:
const mathjs = await import("https://cdn.skypack.dev/mathjs");
// Use any function in the package:
return mathjs.evaluate("sin(45 deg) ^ 2");
Alternatively, you could use JavaScript object destructuring syntax to import evaluate
more specifically:
const { evaluate } = await import("https://cdn.skypack.dev/mathjs");
return evaluate("sin(45 deg) ^ 2");
Instead of evaluating a constant string, pass a parameter to give your app calculation superpowers!
const { evaluate } = await import("https://cdn.skypack.dev/mathjs");
const result = evaluate(p1);
return typeof result === 'number' ? result : result.toString();
You can search for thousands of popular packages on skypack and import them in this way.
Please share any interesting things you do with this power!