index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // to include an npm module/package into clojurescript:
  2. // https://figwheel.org/docs/npm.html
  3. // to reduce the bundle size:
  4. // https://mathjs.org/docs/custom_bundling.html
  5. import { parse, evaluate } from 'mathjs';
  6. function getVariables(expression) {
  7. if( !expression || typeof expression !== 'string' ) {
  8. console.log('not correct input', expression, typeof expression);
  9. return [];
  10. }
  11. try {
  12. const expressionObject = parse(expression);
  13. console.log('now returning', expressionObject);
  14. return expressionObject
  15. .filter(x => x.isSymbolNode)
  16. .map(x => x.name)
  17. .map(x => x.toUpperCase())
  18. .filter(x => /^[A-Z]+[0-9]+$/.test(x));
  19. }
  20. catch(e) {
  21. console.error('error in parsing or filtering for variables', e.message || e);
  22. return [];
  23. }
  24. }
  25. function customEval(...args) {
  26. try {
  27. return evaluate(...args);
  28. }
  29. catch(e) {
  30. console.error('error in evaluating expression', e.message || e);
  31. return {error: e.message};
  32. }
  33. }
  34. window.mathjs = {parse, evaluate: customEval, getVariables};