@danielsimonjr/mathts-expression 0.2.4 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +20 -0
  2. package/dist/index.d.ts +2111 -2398
  3. package/dist/index.js +452 -170
  4. package/package.json +59 -59
package/dist/index.js CHANGED
@@ -118,10 +118,10 @@ function isBigNumber(x) {
118
118
  return false;
119
119
  }
120
120
  function isComplex(x) {
121
- return x && typeof x === "object" && Object.getPrototypeOf(x).isComplex === true || false;
121
+ return !!(x && typeof x === "object" && Object.getPrototypeOf(x).isComplex === true);
122
122
  }
123
123
  function isFraction(x) {
124
- return x && typeof x === "object" && Object.getPrototypeOf(x).isFraction === true || false;
124
+ return !!(x && typeof x === "object" && Object.getPrototypeOf(x).isFraction === true);
125
125
  }
126
126
  function isUnit(x) {
127
127
  if (!x || typeof x !== "object") return false;
@@ -575,13 +575,16 @@ var properties = [
575
575
  function unwrapParen(_node, parenthesis) {
576
576
  if (!parenthesis || parenthesis !== "auto") return _node;
577
577
  let node = _node;
578
- while (isParenthesisNode(node)) node = node.content;
578
+ while (isParenthesisNode(node)) {
579
+ node = node.content;
580
+ }
579
581
  return node;
580
582
  }
581
583
  function getPrecedence(_node, parenthesis, implicit, parent) {
582
584
  let node = _node;
585
+ const parentNode = parent;
583
586
  if (parenthesis !== "keep") {
584
- node = _node.getContent();
587
+ node = node.getContent();
585
588
  }
586
589
  const identifier = node.getIdentifier();
587
590
  let precedence = null;
@@ -593,8 +596,8 @@ function getPrecedence(_node, parenthesis, implicit, parent) {
593
596
  }
594
597
  if (identifier === "OperatorNode:multiply" && node.implicit && implicit !== "show") {
595
598
  const leftArg = unwrapParen(node.args[0], parenthesis);
596
- if (!(isConstantNode(leftArg) && parent && parent.getIdentifier() === "OperatorNode:divide" && rule2Node(unwrapParen(parent.args[0], parenthesis))) && !(leftArg.getIdentifier() === "OperatorNode:divide" && rule2Node(unwrapParen(leftArg.args[0], parenthesis)) && isConstantNode(unwrapParen(leftArg.args[1], parenthesis)))) {
597
- precedence += 1;
599
+ if (!(isConstantNode(leftArg) && parentNode && parentNode.getIdentifier() === "OperatorNode:divide" && rule2Node(unwrapParen(parentNode.args[0], parenthesis))) && !(leftArg.getIdentifier() === "OperatorNode:divide" && rule2Node(unwrapParen(leftArg.args[0], parenthesis)) && isConstantNode(unwrapParen(leftArg.args[1], parenthesis)))) {
600
+ precedence = (precedence ?? 0) + 1;
598
601
  }
599
602
  }
600
603
  return precedence;
@@ -602,7 +605,7 @@ function getPrecedence(_node, parenthesis, implicit, parent) {
602
605
  function getAssociativity(_node, parenthesis) {
603
606
  let node = _node;
604
607
  if (parenthesis !== "keep") {
605
- node = _node.getContent();
608
+ node = node.getContent();
606
609
  }
607
610
  const identifier = node.getIdentifier();
608
611
  const index = getPrecedence(node, parenthesis, void 0, void 0);
@@ -624,8 +627,10 @@ function getAssociativity(_node, parenthesis) {
624
627
  return null;
625
628
  }
626
629
  function isAssociativeWith(nodeA, nodeB, parenthesis) {
627
- const a = parenthesis !== "keep" ? nodeA.getContent() : nodeA;
628
- const b = parenthesis !== "keep" ? nodeA.getContent() : nodeB;
630
+ const nA = nodeA;
631
+ const nB = nodeB;
632
+ const a = parenthesis !== "keep" ? nA.getContent() : nA;
633
+ const b = parenthesis !== "keep" ? nA.getContent() : nB;
629
634
  const identifierA = a.getIdentifier();
630
635
  const identifierB = b.getIdentifier();
631
636
  const index = getPrecedence(a, parenthesis, void 0, void 0);
@@ -647,7 +652,7 @@ function getOperator(fn) {
647
652
  const identifier = "OperatorNode:" + fn;
648
653
  for (const group of properties) {
649
654
  if (identifier in group) {
650
- return group[identifier].op;
655
+ return group[identifier].op ?? null;
651
656
  }
652
657
  }
653
658
  return null;
@@ -663,8 +668,9 @@ var MathjsError = class _MathjsError extends Error {
663
668
  constructor(message) {
664
669
  super(message);
665
670
  this.name = "MathjsError";
666
- if (Error.captureStackTrace) {
667
- Error.captureStackTrace(this, _MathjsError);
671
+ const { captureStackTrace } = Error;
672
+ if (captureStackTrace) {
673
+ captureStackTrace(this, _MathjsError);
668
674
  }
669
675
  }
670
676
  };
@@ -696,7 +702,7 @@ function assertDependencies(name21, dependencies21, scope) {
696
702
  }
697
703
  }
698
704
  function isOptionalDependency(dependency) {
699
- return dependency && dependency[0] === "?";
705
+ return Boolean(dependency && dependency[0] === "?");
700
706
  }
701
707
  function stripOptionalNotation(dependency) {
702
708
  return dependency && dependency[0] === "?" ? dependency.slice(1) : dependency;
@@ -742,8 +748,9 @@ var IndexError = class _IndexError extends RangeError {
742
748
  this.min = actualMin;
743
749
  this.max = actualMax;
744
750
  this.name = "IndexError";
745
- if (Error.captureStackTrace) {
746
- Error.captureStackTrace(this, _IndexError);
751
+ const { captureStackTrace } = Error;
752
+ if (captureStackTrace) {
753
+ captureStackTrace(this, _IndexError);
747
754
  }
748
755
  }
749
756
  };
@@ -822,9 +829,7 @@ function format(value, options) {
822
829
  case "auto":
823
830
  return toPrecision(value, precision, options).replace(
824
831
  /((\.\d*?)(0+))($|e)/,
825
- function() {
826
- const digits = arguments[2];
827
- const e = arguments[4];
832
+ function(_match, _g1, digits, _g3, e) {
828
833
  return digits !== "." ? digits + e : e;
829
834
  }
830
835
  );
@@ -1093,8 +1098,9 @@ function format2(value, options) {
1093
1098
  case "hex":
1094
1099
  return formatBigNumberToBase(value, 16, wordSize);
1095
1100
  case "auto": {
1096
- const lowerExp = _toNumberOrDefault2(options?.lowerExp, -3);
1097
- const upperExp = _toNumberOrDefault2(options?.upperExp, 5);
1101
+ const optionsObject = typeof options === "object" ? options : void 0;
1102
+ const lowerExp = _toNumberOrDefault2(optionsObject?.lowerExp, -3);
1103
+ const upperExp = _toNumberOrDefault2(optionsObject?.upperExp, 5);
1098
1104
  if (value.isZero()) return "0";
1099
1105
  let str;
1100
1106
  const rounded = value.toSignificantDigits(precision);
@@ -1104,11 +1110,12 @@ function format2(value, options) {
1104
1110
  } else {
1105
1111
  str = toExponential2(value, precision);
1106
1112
  }
1107
- return str.replace(/((\.\d*?)(0+))($|e)/, function() {
1108
- const digits = arguments[2];
1109
- const e = arguments[4];
1110
- return digits !== "." ? digits + e : e;
1111
- });
1113
+ return str.replace(
1114
+ /((\.\d*?)(0+))($|e)/,
1115
+ function(_match, _g1, digits, _g3, e) {
1116
+ return digits !== "." ? digits + e : e;
1117
+ }
1118
+ );
1112
1119
  }
1113
1120
  default:
1114
1121
  throw new Error(
@@ -1160,14 +1167,19 @@ function _format(value, options) {
1160
1167
  return format(value, options);
1161
1168
  }
1162
1169
  if (isBigNumber(value)) {
1163
- return format2(value, options);
1170
+ return format2(
1171
+ value,
1172
+ options
1173
+ );
1164
1174
  }
1165
1175
  if (looksLikeFraction(value)) {
1166
- if (!options || options.fraction !== "decimal") {
1167
- const signedNumerator = typeof value.n === "bigint" ? BigInt(value.s) * value.n : value.s * value.n;
1168
- return `${signedNumerator}/${value.d}`;
1176
+ const v = value;
1177
+ const opts = options;
1178
+ if (!opts || opts.fraction !== "decimal") {
1179
+ const signedNumerator = typeof v.n === "bigint" ? BigInt(v.s) * v.n : v.s * v.n;
1180
+ return `${signedNumerator}/${v.d}`;
1169
1181
  } else {
1170
- return value.toString();
1182
+ return v.toString();
1171
1183
  }
1172
1184
  }
1173
1185
  if (Array.isArray(value)) {
@@ -1177,13 +1189,15 @@ function _format(value, options) {
1177
1189
  return stringify(value);
1178
1190
  }
1179
1191
  if (typeof value === "function") {
1180
- return value.syntax ? String(value.syntax) : "function";
1192
+ const fn = value;
1193
+ return fn.syntax ? String(fn.syntax) : "function";
1181
1194
  }
1182
1195
  if (value && typeof value === "object") {
1183
- if (typeof value.format === "function") {
1184
- return value.format(options);
1185
- } else if (value && value.toString(options) !== {}.toString()) {
1186
- return value.toString(options);
1196
+ const v = value;
1197
+ if (typeof v.format === "function") {
1198
+ return v.format(options);
1199
+ } else if (value && v.toString(options) !== {}.toString()) {
1200
+ return v.toString(options);
1187
1201
  } else {
1188
1202
  const entries = Object.keys(value).map((key) => {
1189
1203
  return stringify(key) + ": " + format3(value[key], options);
@@ -1235,7 +1249,7 @@ function formatArray(array, options) {
1235
1249
  }
1236
1250
  }
1237
1251
  function looksLikeFraction(value) {
1238
- return value && typeof value === "object" && typeof value.s === "bigint" && typeof value.n === "bigint" && typeof value.d === "bigint" || false;
1252
+ return !!value && typeof value === "object" && typeof value.s === "bigint" && typeof value.n === "bigint" && typeof value.d === "bigint" || false;
1239
1253
  }
1240
1254
 
1241
1255
  // src/utils/array.ts
@@ -1365,10 +1379,13 @@ var createParse = /* @__PURE__ */ factory(
1365
1379
  });
1366
1380
  function parseMultiple(expressions, options = {}) {
1367
1381
  const extraNodes = options.nodes !== void 0 ? options.nodes : {};
1368
- return deepMap2(expressions, function(elem) {
1369
- if (typeof elem !== "string") throw new TypeError("String expected");
1370
- return parseStart(elem, extraNodes);
1371
- });
1382
+ return deepMap2(
1383
+ expressions,
1384
+ function(elem) {
1385
+ if (typeof elem !== "string") throw new TypeError("String expected");
1386
+ return parseStart(elem, extraNodes);
1387
+ }
1388
+ );
1372
1389
  }
1373
1390
  const DELIMITERS = {
1374
1391
  ",": true,
@@ -1722,17 +1739,19 @@ var createParse = /* @__PURE__ */ factory(
1722
1739
  value = parseAssignment(state);
1723
1740
  return new AssignmentNode(new SymbolNode(name21), value);
1724
1741
  } else if (isAccessorNode(node)) {
1725
- if (node.optionalChaining) {
1742
+ const accessor = node;
1743
+ if (accessor.optionalChaining) {
1726
1744
  throw createSyntaxError(state, "Cannot assign to optional chain");
1727
1745
  }
1728
1746
  getTokenSkipNewline(state);
1729
1747
  value = parseAssignment(state);
1730
- return new AssignmentNode(node.object, node.index, value);
1748
+ return new AssignmentNode(accessor.object, accessor.index, value);
1731
1749
  } else if (isFunctionNode(node) && isSymbolNode(node.fn)) {
1732
1750
  valid = true;
1733
1751
  args = [];
1734
- name21 = node.name;
1735
- node.args.forEach(function(arg, index) {
1752
+ const fnNode = node;
1753
+ name21 = fnNode.name;
1754
+ fnNode.args.forEach(function(arg, index) {
1736
1755
  if (isSymbolNode(arg)) {
1737
1756
  args[index] = arg.name;
1738
1757
  } else {
@@ -2278,12 +2297,13 @@ var createParse = /* @__PURE__ */ factory(
2278
2297
  }
2279
2298
  closeParams(state);
2280
2299
  getToken(state);
2281
- cols = params[0].items.length;
2300
+ const rowItems = (row2) => row2.items;
2301
+ cols = rowItems(params[0]).length;
2282
2302
  for (let r = 1; r < rows; r++) {
2283
- if (params[r].items.length !== cols) {
2303
+ if (rowItems(params[r]).length !== cols) {
2284
2304
  throw createError(
2285
2305
  state,
2286
- "Column dimensions mismatch (" + params[r].items.length + " !== " + cols + ")"
2306
+ "Column dimensions mismatch (" + rowItems(params[r]).length + " !== " + cols + ")"
2287
2307
  );
2288
2308
  }
2289
2309
  }
@@ -2412,16 +2432,16 @@ function getSafeProperty(object, prop) {
2412
2432
  return object[prop];
2413
2433
  }
2414
2434
  if (typeof object[prop] === "function" && isSafeMethod(object, prop)) {
2415
- throw new Error('Cannot access method "' + prop + '" as a property');
2435
+ throw new Error('Cannot access method "' + String(prop) + '" as a property');
2416
2436
  }
2417
- throw new Error('No access to property "' + prop + '"');
2437
+ throw new Error('No access to property "' + String(prop) + '"');
2418
2438
  }
2419
2439
  function setSafeProperty(object, prop, value) {
2420
2440
  if (isSafeProperty(object, prop)) {
2421
2441
  object[prop] = value;
2422
2442
  return value;
2423
2443
  }
2424
- throw new Error('No access to property "' + prop + '"');
2444
+ throw new Error('No access to property "' + String(prop) + '"');
2425
2445
  }
2426
2446
  function isSafeProperty(object, prop) {
2427
2447
  if (!isPlainObject(object) && !Array.isArray(object)) {
@@ -2440,7 +2460,7 @@ function isSafeProperty(object, prop) {
2440
2460
  }
2441
2461
  function getSafeMethod(object, method) {
2442
2462
  if (!isSafeMethod(object, method)) {
2443
- throw new Error('No access to method "' + method + '"');
2463
+ throw new Error('No access to method "' + String(method) + '"');
2444
2464
  }
2445
2465
  return object[method];
2446
2466
  }
@@ -2463,7 +2483,7 @@ function isSafeMethod(object, method) {
2463
2483
  return true;
2464
2484
  }
2465
2485
  function isPlainObject(object) {
2466
- return typeof object === "object" && object && object.constructor === Object;
2486
+ return typeof object === "object" && !!object && object.constructor === Object;
2467
2487
  }
2468
2488
  var safeNativeProperties = {
2469
2489
  length: true,
@@ -2648,9 +2668,7 @@ var createParserClass = /* @__PURE__ */ factory(
2648
2668
  return evaluate(expr, this.scope);
2649
2669
  };
2650
2670
  Parser.prototype.get = function(name21) {
2651
- if (this.scope.has(name21)) {
2652
- return this.scope.get(name21);
2653
- }
2671
+ return this.scope.has(name21) ? this.scope.get(name21) : void 0;
2654
2672
  };
2655
2673
  Parser.prototype.getAll = function() {
2656
2674
  return toObject(this.scope);
@@ -2717,7 +2735,8 @@ var createParserClass = /* @__PURE__ */ factory(
2717
2735
  { isClass: true }
2718
2736
  );
2719
2737
  function isExpressionFunction(value) {
2720
- return typeof value === "function" && typeof value.syntax === "string" && typeof value.expr === "string";
2738
+ const fn = value;
2739
+ return typeof value === "function" && typeof fn.syntax === "string" && typeof fn.expr === "string";
2721
2740
  }
2722
2741
 
2723
2742
  // src/function/parser.ts
@@ -2726,7 +2745,10 @@ var dependencies3 = ["typed", "Parser"];
2726
2745
  var createParser = /* @__PURE__ */ factory(
2727
2746
  name3,
2728
2747
  dependencies3,
2729
- ({ typed, Parser }) => {
2748
+ ({
2749
+ typed,
2750
+ Parser
2751
+ }) => {
2730
2752
  return typed(name3, {
2731
2753
  "": function() {
2732
2754
  return new Parser();
@@ -2838,49 +2860,50 @@ function compile(node, mathScope) {
2838
2860
  };
2839
2861
  }
2840
2862
  function compileNode(node, math, argNames) {
2841
- if (node.isConstantNode) {
2863
+ const n = node;
2864
+ if (n.isConstantNode) {
2842
2865
  return compileConstantNode(node);
2843
2866
  }
2844
- if (node.isSymbolNode) {
2867
+ if (n.isSymbolNode) {
2845
2868
  return compileSymbolNode(node, math, argNames);
2846
2869
  }
2847
- if (node.isOperatorNode) {
2870
+ if (n.isOperatorNode) {
2848
2871
  return compileOperatorNode(node, math, argNames);
2849
2872
  }
2850
- if (node.isFunctionNode) {
2873
+ if (n.isFunctionNode) {
2851
2874
  return compileFunctionNode(node, math, argNames);
2852
2875
  }
2853
- if (node.isParenthesisNode) {
2854
- return compileNode(node.content, math, argNames);
2876
+ if (n.isParenthesisNode) {
2877
+ return compileNode(n.content, math, argNames);
2855
2878
  }
2856
- if (node.isArrayNode) {
2879
+ if (n.isArrayNode) {
2857
2880
  return compileArrayNode(node, math, argNames);
2858
2881
  }
2859
- if (node.isAssignmentNode) {
2882
+ if (n.isAssignmentNode) {
2860
2883
  return compileAssignmentNode(node, math, argNames);
2861
2884
  }
2862
- if (node.isBlockNode) {
2885
+ if (n.isBlockNode) {
2863
2886
  return compileBlockNode(node, math, argNames);
2864
2887
  }
2865
- if (node.isConditionalNode) {
2888
+ if (n.isConditionalNode) {
2866
2889
  return compileConditionalNode(node, math, argNames);
2867
2890
  }
2868
- if (node.isObjectNode) {
2891
+ if (n.isObjectNode) {
2869
2892
  return compileObjectNode(node, math, argNames);
2870
2893
  }
2871
- if (node.isRangeNode) {
2894
+ if (n.isRangeNode) {
2872
2895
  return compileRangeNode(node, math, argNames);
2873
2896
  }
2874
- if (node.isRelationalNode) {
2897
+ if (n.isRelationalNode) {
2875
2898
  return compileRelationalNode(node, math, argNames);
2876
2899
  }
2877
- if (node.isFunctionAssignmentNode) {
2900
+ if (n.isFunctionAssignmentNode) {
2878
2901
  return compileFunctionAssignmentNode(node, math, argNames);
2879
2902
  }
2880
- if (node.isAccessorNode) {
2903
+ if (n.isAccessorNode) {
2881
2904
  return compileAccessorNode(node, math, argNames);
2882
2905
  }
2883
- if (node.isIndexNode) {
2906
+ if (n.isIndexNode) {
2884
2907
  return compileIndexNode(node, math, argNames);
2885
2908
  }
2886
2909
  throw new Error(`Unknown node type: ${node.type || node.constructor?.name}`);
@@ -2914,12 +2937,13 @@ function compileSymbolNode(node, math, argNames) {
2914
2937
  };
2915
2938
  }
2916
2939
  function compileOperatorNode(node, math, argNames) {
2917
- const fnName = node.fn;
2940
+ const opNode = node;
2941
+ const fnName = opNode.fn;
2918
2942
  if (typeof fnName !== "string" || !(fnName in math)) {
2919
2943
  throw new Error(`Function "${fnName}" missing in provided namespace "math"`);
2920
2944
  }
2921
2945
  const fn = math[fnName];
2922
- const evalArgs = node.args.map((arg) => compileNode(arg, math, argNames));
2946
+ const evalArgs = opNode.args.map((arg) => compileNode(arg, math, argNames));
2923
2947
  if (evalArgs.length === 1) {
2924
2948
  const evalArg0 = evalArgs[0];
2925
2949
  return function evalOperatorNode(scope, args, context) {
@@ -2939,8 +2963,9 @@ function compileOperatorNode(node, math, argNames) {
2939
2963
  };
2940
2964
  }
2941
2965
  function compileFunctionNode(node, math, argNames) {
2942
- const evalArgs = node.args.map((arg) => compileNode(arg, math, argNames));
2943
- const fnNode = node.fn;
2966
+ const fnNodeOuter = node;
2967
+ const evalArgs = fnNodeOuter.args.map((arg) => compileNode(arg, math, argNames));
2968
+ const fnNode = fnNodeOuter.fn;
2944
2969
  if (fnNode && fnNode.isSymbolNode) {
2945
2970
  const name21 = fnNode.name;
2946
2971
  const resolveFn = (scope) => {
@@ -3007,27 +3032,27 @@ function compileFunctionNode(node, math, argNames) {
3007
3032
  };
3008
3033
  }
3009
3034
  function compileArrayNode(node, math, argNames) {
3010
- const evalItems = node.items.map(
3011
- (item) => compileNode(item, math, argNames)
3012
- );
3035
+ const items = node.items;
3036
+ const evalItems = items.map((item) => compileNode(item, math, argNames));
3013
3037
  return function evalArrayNode(scope, args, context) {
3014
3038
  return evalItems.map((e) => e(scope, args, context));
3015
3039
  };
3016
3040
  }
3017
3041
  function compileAssignmentNode(node, math, argNames) {
3018
- const evalValue = compileNode(node.value, math, argNames);
3019
- if (node.object && node.object.isSymbolNode && !node.index) {
3020
- const name22 = node.object.name;
3042
+ const asgNode = node;
3043
+ const evalValue = compileNode(asgNode.value, math, argNames);
3044
+ if (asgNode.object && asgNode.object.isSymbolNode && !asgNode.index) {
3045
+ const name22 = asgNode.object.name;
3021
3046
  return function evalAssignmentNode(scope, args, context) {
3022
3047
  const value = evalValue(scope, args, context);
3023
3048
  scope.set(name22, value);
3024
3049
  return value;
3025
3050
  };
3026
3051
  }
3027
- if (node.object && node.object.isAccessorNode) {
3028
- const evalObject = compileNode(node.object.object, math, argNames);
3029
- if (node.object.index && node.object.index.isObjectProperty()) {
3030
- const prop = node.object.index.getObjectProperty();
3052
+ if (asgNode.object && asgNode.object.isAccessorNode) {
3053
+ const evalObject = compileNode(asgNode.object.object, math, argNames);
3054
+ if (asgNode.object.index && asgNode.object.index.isObjectProperty()) {
3055
+ const prop = asgNode.object.index.getObjectProperty();
3031
3056
  return function evalAssignmentNode(scope, args, context) {
3032
3057
  const obj = evalObject(scope, args, context);
3033
3058
  const value = evalValue(scope, args, context);
@@ -3035,7 +3060,7 @@ function compileAssignmentNode(node, math, argNames) {
3035
3060
  };
3036
3061
  }
3037
3062
  }
3038
- const name21 = node.name || node.object && node.object.name;
3063
+ const name21 = asgNode.name || asgNode.object && asgNode.object.name;
3039
3064
  if (name21) {
3040
3065
  return function evalAssignmentNode(scope, args, context) {
3041
3066
  const value = evalValue(scope, args, context);
@@ -3046,7 +3071,8 @@ function compileAssignmentNode(node, math, argNames) {
3046
3071
  throw new Error("Unsupported assignment target");
3047
3072
  }
3048
3073
  function compileBlockNode(node, math, argNames) {
3049
- const evalBlocks = node.blocks.map((block) => ({
3074
+ const blocks = node.blocks;
3075
+ const evalBlocks = blocks.map((block) => ({
3050
3076
  evaluate: compileNode(block.node, math, argNames),
3051
3077
  visible: block.visible
3052
3078
  }));
@@ -3064,19 +3090,21 @@ function compileBlockNode(node, math, argNames) {
3064
3090
  };
3065
3091
  }
3066
3092
  function compileConditionalNode(node, math, argNames) {
3067
- const evalCondition = compileNode(node.condition, math, argNames);
3068
- const evalTrueExpr = compileNode(node.trueExpr, math, argNames);
3069
- const evalFalseExpr = compileNode(node.falseExpr, math, argNames);
3093
+ const condNode = node;
3094
+ const evalCondition = compileNode(condNode.condition, math, argNames);
3095
+ const evalTrueExpr = compileNode(condNode.trueExpr, math, argNames);
3096
+ const evalFalseExpr = compileNode(condNode.falseExpr, math, argNames);
3070
3097
  return function evalConditionalNode(scope, args, context) {
3071
3098
  const condition = evalCondition(scope, args, context);
3072
3099
  return testCondition(condition) ? evalTrueExpr(scope, args, context) : evalFalseExpr(scope, args, context);
3073
3100
  };
3074
3101
  }
3075
3102
  function compileObjectNode(node, math, argNames) {
3076
- const keys = Object.keys(node.properties);
3103
+ const properties2 = node.properties;
3104
+ const keys = Object.keys(properties2);
3077
3105
  const evalProps = keys.map((key) => ({
3078
3106
  key,
3079
- evaluate: compileNode(node.properties[key], math, argNames)
3107
+ evaluate: compileNode(properties2[key], math, argNames)
3080
3108
  }));
3081
3109
  return function evalObjectNode(scope, args, context) {
3082
3110
  const result = {};
@@ -3091,10 +3119,11 @@ function compileRangeNode(node, math, argNames) {
3091
3119
  if (!rangeFn) {
3092
3120
  throw new Error('Function "range" missing in math namespace (required for range expressions)');
3093
3121
  }
3094
- const evalStart = compileNode(node.start, math, argNames);
3095
- const evalEnd = compileNode(node.end, math, argNames);
3096
- if (node.step) {
3097
- const evalStep = compileNode(node.step, math, argNames);
3122
+ const rngNode = node;
3123
+ const evalStart = compileNode(rngNode.start, math, argNames);
3124
+ const evalEnd = compileNode(rngNode.end, math, argNames);
3125
+ if (rngNode.step) {
3126
+ const evalStep = compileNode(rngNode.step, math, argNames);
3098
3127
  return function evalRangeNode(scope, args, context) {
3099
3128
  return rangeFn(
3100
3129
  evalStart(scope, args, context),
@@ -3108,8 +3137,9 @@ function compileRangeNode(node, math, argNames) {
3108
3137
  };
3109
3138
  }
3110
3139
  function compileRelationalNode(node, math, argNames) {
3111
- const compiled = node.params.map((p) => compileNode(p, math, argNames));
3112
- const conditionals = node.conditionals;
3140
+ const relNode = node;
3141
+ const compiled = relNode.params.map((p) => compileNode(p, math, argNames));
3142
+ const conditionals = relNode.conditionals;
3113
3143
  return function evalRelationalNode(scope, args, context) {
3114
3144
  let evalLhs;
3115
3145
  let evalRhs = compiled[0](scope, args, context);
@@ -3128,13 +3158,14 @@ function compileRelationalNode(node, math, argNames) {
3128
3158
  };
3129
3159
  }
3130
3160
  function compileFunctionAssignmentNode(node, math, argNames) {
3161
+ const faNode = node;
3131
3162
  const childArgNames = Object.create(argNames);
3132
- for (const param of node.params) {
3163
+ for (const param of faNode.params) {
3133
3164
  childArgNames[param] = true;
3134
3165
  }
3135
- const evalExpr = compileNode(node.expr, math, childArgNames);
3136
- const name21 = node.name;
3137
- const params = node.params;
3166
+ const evalExpr = compileNode(faNode.expr, math, childArgNames);
3167
+ const name21 = faNode.name;
3168
+ const params = faNode.params;
3138
3169
  return function evalFunctionAssignmentNode(scope, args, _context) {
3139
3170
  const fn = function(...fnArgs) {
3140
3171
  const childArgs = Object.create(args);
@@ -3149,19 +3180,20 @@ function compileFunctionAssignmentNode(node, math, argNames) {
3149
3180
  };
3150
3181
  }
3151
3182
  function compileAccessorNode(node, math, argNames) {
3152
- const evalObject = compileNode(node.object, math, argNames);
3153
- if (node.index && node.index.isObjectProperty && node.index.isObjectProperty()) {
3154
- const prop = node.index.getObjectProperty();
3183
+ const accNode = node;
3184
+ const evalObject = compileNode(accNode.object, math, argNames);
3185
+ if (accNode.index && accNode.index.isObjectProperty && accNode.index.isObjectProperty()) {
3186
+ const prop = accNode.index.getObjectProperty();
3155
3187
  return function evalAccessorNode(scope, args, context) {
3156
3188
  const object = evalObject(scope, args, context);
3157
- if (node.optionalChaining && object == null) return void 0;
3189
+ if (accNode.optionalChaining && object == null) return void 0;
3158
3190
  return getSafeProperty(object, prop);
3159
3191
  };
3160
3192
  }
3161
- const evalIndex = compileNode(node.index, math, argNames);
3193
+ const evalIndex = compileNode(accNode.index, math, argNames);
3162
3194
  return function evalAccessorNode(scope, args, context) {
3163
3195
  const object = evalObject(scope, args, context);
3164
- if (node.optionalChaining && object == null) return void 0;
3196
+ if (accNode.optionalChaining && object == null) return void 0;
3165
3197
  const index = evalIndex(scope, args, context);
3166
3198
  if (math.subset) {
3167
3199
  return math.subset(object, index);
@@ -3173,7 +3205,8 @@ function compileAccessorNode(node, math, argNames) {
3173
3205
  };
3174
3206
  }
3175
3207
  function compileIndexNode(node, math, argNames) {
3176
- const evalDimensions = node.dimensions.map(
3208
+ const dimensionNodes = node.dimensions;
3209
+ const evalDimensions = dimensionNodes.map(
3177
3210
  (dim) => compileNode(dim, math, argNames)
3178
3211
  );
3179
3212
  return function evalIndexNode(scope, args, context) {
@@ -3190,11 +3223,12 @@ function testCondition(condition) {
3190
3223
  return !!condition;
3191
3224
  }
3192
3225
  if (condition) {
3193
- if (typeof condition.isZero === "function") {
3194
- return !condition.isZero();
3226
+ const c = condition;
3227
+ if (typeof c.isZero === "function") {
3228
+ return !c.isZero();
3195
3229
  }
3196
- if (condition.re !== void 0 || condition.im !== void 0) {
3197
- return !!(condition.re || condition.im);
3230
+ if (c.re !== void 0 || c.im !== void 0) {
3231
+ return !!(c.re || c.im);
3198
3232
  }
3199
3233
  }
3200
3234
  if (condition === null || condition === void 0) {
@@ -3219,27 +3253,29 @@ function validateAst(node) {
3219
3253
  if (node === null || node === void 0 || typeof node !== "object") {
3220
3254
  return;
3221
3255
  }
3222
- if (node.isAssignmentNode === true) {
3256
+ const n = node;
3257
+ if (n.isAssignmentNode === true) {
3223
3258
  throw new Error(
3224
3259
  "Security: assignment expressions are disabled. Pass `{ unsafe: true }` to opt out."
3225
3260
  );
3226
3261
  }
3227
- if (node.isFunctionAssignmentNode === true) {
3262
+ if (n.isFunctionAssignmentNode === true) {
3228
3263
  throw new Error(
3229
3264
  "Security: function definitions (FunctionAssignmentNode) are disabled. Pass `{ unsafe: true }` to opt out."
3230
3265
  );
3231
3266
  }
3232
- if (node.isFunctionNode === true) {
3233
- const fnName = node.fn && typeof node.fn === "object" && node.fn.isSymbolNode && node.fn.name || (typeof node.name === "string" ? node.name : void 0);
3267
+ if (n.isFunctionNode === true) {
3268
+ const fn = n.fn;
3269
+ const fnName = (fn && typeof fn === "object" && fn.isSymbolNode && typeof fn.name === "string" ? fn.name : void 0) || (typeof n.name === "string" ? n.name : void 0);
3234
3270
  if (fnName && FORBIDDEN_FUNCTIONS.has(fnName)) {
3235
3271
  throw new Error(
3236
3272
  `Security: call to forbidden function "${fnName}" is disabled. Pass \`{ unsafe: true }\` to opt out.`
3237
3273
  );
3238
3274
  }
3239
3275
  }
3240
- if (typeof node.forEach === "function") {
3276
+ if (typeof n.forEach === "function") {
3241
3277
  try {
3242
- node.forEach((child) => validateAst(child));
3278
+ n.forEach((child) => validateAst(child));
3243
3279
  return;
3244
3280
  } catch (err) {
3245
3281
  if (err instanceof Error && err.message.startsWith("Security:")) throw err;
@@ -3259,15 +3295,18 @@ function validateAst(node) {
3259
3295
  "end",
3260
3296
  "step"
3261
3297
  ]) {
3262
- if (key in node) validateAst(node[key]);
3298
+ if (key in n) validateAst(n[key]);
3263
3299
  }
3264
3300
  for (const key of ["args", "items", "params"]) {
3265
- const arr = node[key];
3301
+ const arr = n[key];
3266
3302
  if (Array.isArray(arr)) for (const child of arr) validateAst(child);
3267
3303
  }
3268
- if (Array.isArray(node.blocks)) for (const b of node.blocks) validateAst(b && b.node);
3269
- if (node.properties && typeof node.properties === "object") {
3270
- for (const k of Object.keys(node.properties)) validateAst(node.properties[k]);
3304
+ if (Array.isArray(n.blocks)) {
3305
+ for (const b of n.blocks) validateAst(b && b.node);
3306
+ }
3307
+ if (n.properties && typeof n.properties === "object") {
3308
+ const properties2 = n.properties;
3309
+ for (const k of Object.keys(properties2)) validateAst(properties2[k]);
3271
3310
  }
3272
3311
  }
3273
3312
  function createEvaluate(parseFn, mathScope) {
@@ -3295,6 +3334,128 @@ function compileExpression(parseFn, mathScope, expr, options) {
3295
3334
  return compile(node, mathScope);
3296
3335
  }
3297
3336
 
3337
+ // src/utils/mathml.ts
3338
+ var mathmlSymbols = {
3339
+ alpha: "\u03B1",
3340
+ beta: "\u03B2",
3341
+ gamma: "\u03B3",
3342
+ delta: "\u03B4",
3343
+ epsilon: "\u03B5",
3344
+ zeta: "\u03B6",
3345
+ eta: "\u03B7",
3346
+ theta: "\u03B8",
3347
+ iota: "\u03B9",
3348
+ kappa: "\u03BA",
3349
+ lambda: "\u03BB",
3350
+ mu: "\u03BC",
3351
+ nu: "\u03BD",
3352
+ xi: "\u03BE",
3353
+ omicron: "\u03BF",
3354
+ pi: "\u03C0",
3355
+ rho: "\u03C1",
3356
+ sigma: "\u03C3",
3357
+ tau: "\u03C4",
3358
+ upsilon: "\u03C5",
3359
+ phi: "\u03C6",
3360
+ chi: "\u03C7",
3361
+ psi: "\u03C8",
3362
+ omega: "\u03C9",
3363
+ Gamma: "\u0393",
3364
+ Delta: "\u0394",
3365
+ Theta: "\u0398",
3366
+ Lambda: "\u039B",
3367
+ Xi: "\u039E",
3368
+ Pi: "\u03A0",
3369
+ Sigma: "\u03A3",
3370
+ Upsilon: "\u03A5",
3371
+ Phi: "\u03A6",
3372
+ Psi: "\u03A8",
3373
+ Omega: "\u03A9"
3374
+ };
3375
+ var PRECEDENCE = {
3376
+ equal: 1,
3377
+ unequal: 1,
3378
+ smaller: 1,
3379
+ larger: 1,
3380
+ smallerEq: 1,
3381
+ largerEq: 1,
3382
+ add: 2,
3383
+ subtract: 2,
3384
+ multiply: 3,
3385
+ divide: 3,
3386
+ dotMultiply: 3,
3387
+ dotDivide: 3,
3388
+ mod: 3,
3389
+ unaryMinus: 4,
3390
+ unaryPlus: 4,
3391
+ pow: 5
3392
+ };
3393
+ var INLINE_OP = {
3394
+ add: "+",
3395
+ subtract: "-",
3396
+ equal: "=",
3397
+ unequal: "\u2260",
3398
+ smaller: "<",
3399
+ larger: ">",
3400
+ smallerEq: "\u2264",
3401
+ largerEq: "\u2265",
3402
+ mod: "mod"
3403
+ };
3404
+ function escapeMathML(s) {
3405
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
3406
+ }
3407
+ function mi(name21) {
3408
+ return `<mi>${mathmlSymbols[name21] ?? escapeMathML(name21)}</mi>`;
3409
+ }
3410
+ function toMathMLSymbol(name21) {
3411
+ const us = name21.indexOf("_");
3412
+ if (us > 0 && us < name21.length - 1) {
3413
+ const base = name21.slice(0, us);
3414
+ const sub = name21.slice(us + 1);
3415
+ const subEl = /^\d+$/.test(sub) ? `<mn>${sub}</mn>` : mi(sub);
3416
+ return `<msub>${mi(base)}${subEl}</msub>`;
3417
+ }
3418
+ return mi(name21);
3419
+ }
3420
+ function numberToMathML(value) {
3421
+ if (!Number.isFinite(value)) return `<mn>${escapeMathML(String(value))}</mn>`;
3422
+ const s = String(value);
3423
+ const sci = /^(-?)(\d+(?:\.\d+)?)[eE]([+-]?\d+)$/.exec(s);
3424
+ if (sci) {
3425
+ const sign = sci[1] ? "<mo>-</mo>" : "";
3426
+ const exp = String(parseInt(sci[3], 10));
3427
+ return `<mrow>${sign}<mn>${sci[2]}</mn><mo>&#xD7;</mo><msup><mn>10</mn><mn>${exp}</mn></msup></mrow>`;
3428
+ }
3429
+ if (value < 0) return `<mrow><mo>-</mo><mn>${escapeMathML(String(-value))}</mn></mrow>`;
3430
+ return `<mn>${escapeMathML(s)}</mn>`;
3431
+ }
3432
+ function constantToMathML(value) {
3433
+ const t = typeof value;
3434
+ if (t === "number") return numberToMathML(value);
3435
+ if (t === "bigint") return `<mn>${escapeMathML(String(value))}</mn>`;
3436
+ if (t === "boolean") return `<mi>${value}</mi>`;
3437
+ if (t === "string") return `<mtext>${escapeMathML(value)}</mtext>`;
3438
+ return `<mtext>${escapeMathML(value === null ? "null" : String(value))}</mtext>`;
3439
+ }
3440
+ function inlineOperator(fn, op) {
3441
+ return `<mo>${escapeMathML(INLINE_OP[fn] ?? op)}</mo>`;
3442
+ }
3443
+ function parenthesizeLower(childFragment, childFn, parentFn) {
3444
+ if (childFn !== void 0) {
3445
+ const cp = PRECEDENCE[childFn] ?? 0;
3446
+ const pp = PRECEDENCE[parentFn] ?? 0;
3447
+ if (cp > 0 && cp < pp) return `<mrow><mo>(</mo>${childFragment}<mo>)</mo></mrow>`;
3448
+ }
3449
+ return childFragment;
3450
+ }
3451
+ var MATH_OPEN = '<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">';
3452
+ function mathMLDocument(node) {
3453
+ return `${MATH_OPEN}${node.toMathML()}</math>`;
3454
+ }
3455
+ function mathMLError(src) {
3456
+ return `${MATH_OPEN}<merror><mtext>${escapeMathML(src)}</mtext></merror></math>`;
3457
+ }
3458
+
3298
3459
  // src/node/Node.ts
3299
3460
  var name5 = "Node";
3300
3461
  var dependencies5 = ["mathWithTransform"];
@@ -3581,6 +3742,28 @@ var createNode = /* @__PURE__ */ factory(
3581
3742
  }
3582
3743
  return this._toTex(options);
3583
3744
  }
3745
+ /**
3746
+ * Render this node to a MathML fragment (the MathML analog of `toTex`).
3747
+ * Like `toTex`, this returns the body — wrap it in a `<math>` element with
3748
+ * `mathMLDocument(node)` to render. Never throws: a failing node degrades
3749
+ * to an escaped `<merror>` fragment.
3750
+ * @return {string}
3751
+ */
3752
+ toMathML() {
3753
+ try {
3754
+ return this._toMathML();
3755
+ } catch {
3756
+ return "<merror><mtext>render error</mtext></merror>";
3757
+ }
3758
+ }
3759
+ /**
3760
+ * Internal MathML generator. Overridden by the node types that render as
3761
+ * typeset math; the base falls back to escaped text (best-effort display),
3762
+ * rather than throwing like `_toTex`, so unhandled nodes degrade gracefully.
3763
+ */
3764
+ _toMathML() {
3765
+ return `<mtext>${escapeMathML(this._toString())}</mtext>`;
3766
+ }
3584
3767
  /**
3585
3768
  * Internal function to generate the LaTeX output.
3586
3769
  * This has to be implemented by every Node
@@ -3606,6 +3789,7 @@ var createNode = /* @__PURE__ */ factory(
3606
3789
  throw new TypeError("Object or function expected as callback");
3607
3790
  }
3608
3791
  }
3792
+ return void 0;
3609
3793
  }
3610
3794
  /**
3611
3795
  * Get identifier.
@@ -3629,12 +3813,9 @@ var createNode = /* @__PURE__ */ factory(
3629
3813
 
3630
3814
  // src/transform/utils/errorTransform.ts
3631
3815
  function errorTransform(err) {
3632
- if (err && err.isIndexError) {
3633
- return new IndexError(
3634
- err.index + 1,
3635
- err.min + 1,
3636
- err.max !== void 0 ? err.max + 1 : void 0
3637
- );
3816
+ const e = err;
3817
+ if (e && e.isIndexError) {
3818
+ return new IndexError(e.index + 1, e.min + 1, e.max !== void 0 ? e.max + 1 : void 0);
3638
3819
  }
3639
3820
  return err;
3640
3821
  }
@@ -3669,7 +3850,10 @@ var dependencies6 = ["subset", "Node"];
3669
3850
  var createAccessorNode = /* @__PURE__ */ factory(
3670
3851
  name6,
3671
3852
  dependencies6,
3672
- ({ subset, Node }) => {
3853
+ ({
3854
+ subset,
3855
+ Node
3856
+ }) => {
3673
3857
  const access = accessFactory({ subset });
3674
3858
  function needParenthesis(node) {
3675
3859
  return !(isAccessorNode(node) || isArrayNode(node) || isConstantNode(node) || isFunctionNode(node) || isObjectNode(node) || isParenthesisNode(node) || isSymbolNode(node));
@@ -4010,8 +4194,9 @@ var createArrayNode = /* @__PURE__ */ factory(
4010
4194
  const itemsFormRow = nested || mixedItems;
4011
4195
  const itemSep = itemsFormRow ? "&" : "\\\\";
4012
4196
  const itemsTex = items.map(function(node) {
4013
- if (node.items) {
4014
- return itemsToTex(node.items, !nested);
4197
+ const arrayItems = node.items;
4198
+ if (arrayItems) {
4199
+ return itemsToTex(arrayItems, !nested);
4015
4200
  } else {
4016
4201
  return node.toTex(options);
4017
4202
  }
@@ -4061,7 +4246,10 @@ var dependencies8 = ["subset", "Node"];
4061
4246
  var createAssignmentNode = /* @__PURE__ */ factory(
4062
4247
  name8,
4063
4248
  dependencies8,
4064
- ({ subset, Node }) => {
4249
+ ({
4250
+ subset,
4251
+ Node
4252
+ }) => {
4065
4253
  const access = accessFactory({ subset });
4066
4254
  const assign = assignFactory({ subset });
4067
4255
  function needParenthesis(node, parenthesis, implicit) {
@@ -4186,9 +4374,10 @@ var createAssignmentNode = /* @__PURE__ */ factory(
4186
4374
  return value;
4187
4375
  };
4188
4376
  } else {
4189
- const evalParentObject = this.object.object._compile(math, argNames);
4190
- if (this.object.index.isObjectProperty()) {
4191
- const parentProp = this.object.index.getObjectProperty();
4377
+ const accessorObject = this.object;
4378
+ const evalParentObject = accessorObject.object._compile(math, argNames);
4379
+ if (accessorObject.index.isObjectProperty()) {
4380
+ const parentProp = accessorObject.index.getObjectProperty();
4192
4381
  return function evalAssignmentNode(scope, args, context) {
4193
4382
  const parent = evalParentObject(scope, args, context);
4194
4383
  const childObject = getSafeProperty(parent, parentProp);
@@ -4198,7 +4387,7 @@ var createAssignmentNode = /* @__PURE__ */ factory(
4198
4387
  return value;
4199
4388
  };
4200
4389
  } else {
4201
- const evalParentIndex = this.object.index._compile(math, argNames);
4390
+ const evalParentIndex = accessorObject.index._compile(math, argNames);
4202
4391
  return function evalAssignmentNode(scope, args, context) {
4203
4392
  const parent = evalParentObject(scope, args, context);
4204
4393
  const parentIndex = evalParentIndex(scope, args, parent);
@@ -4297,6 +4486,14 @@ var createAssignmentNode = /* @__PURE__ */ factory(
4297
4486
  * @param {Object} options
4298
4487
  * @return {string}
4299
4488
  */
4489
+ _toMathML() {
4490
+ const obj = this.object;
4491
+ if (obj && isSymbolNode(obj) && !this.index && this.value) {
4492
+ const name21 = obj.name;
4493
+ return `<mrow>${toMathMLSymbol(name21)}<mo>=</mo>${this.value.toMathML()}</mrow>`;
4494
+ }
4495
+ return `<mtext>${escapeMathML(this.toString())}</mtext>`;
4496
+ }
4300
4497
  _toTex(options) {
4301
4498
  const object = this.object.toTex(options);
4302
4499
  const index = this.index ? this.index.toTex(options) : "";
@@ -4318,7 +4515,10 @@ var dependencies9 = ["ResultSet", "Node"];
4318
4515
  var createBlockNode = /* @__PURE__ */ factory(
4319
4516
  name9,
4320
4517
  dependencies9,
4321
- ({ ResultSet, Node }) => {
4518
+ ({
4519
+ ResultSet,
4520
+ Node
4521
+ }) => {
4322
4522
  class BlockNode extends Node {
4323
4523
  blocks;
4324
4524
  /**
@@ -4601,7 +4801,12 @@ var createConditionalNode = /* @__PURE__ */ factory(
4601
4801
  */
4602
4802
  _toString(options) {
4603
4803
  const parenthesis = options && options.parenthesis ? options.parenthesis : "keep";
4604
- const precedence = getPrecedence(this, parenthesis, options && options.implicit, void 0);
4804
+ const precedence = getPrecedence(
4805
+ this,
4806
+ parenthesis,
4807
+ options && options.implicit,
4808
+ void 0
4809
+ );
4605
4810
  let condition = this.condition.toString(options);
4606
4811
  const conditionPrecedence = getPrecedence(
4607
4812
  this.condition,
@@ -4669,7 +4874,12 @@ var createConditionalNode = /* @__PURE__ */ factory(
4669
4874
  */
4670
4875
  _toHTML(options) {
4671
4876
  const parenthesis = options && options.parenthesis ? options.parenthesis : "keep";
4672
- const precedence = getPrecedence(this, parenthesis, options && options.implicit, void 0);
4877
+ const precedence = getPrecedence(
4878
+ this,
4879
+ parenthesis,
4880
+ options && options.implicit,
4881
+ void 0
4882
+ );
4673
4883
  let condition = this.condition.toHTML(options);
4674
4884
  const conditionPrecedence = getPrecedence(
4675
4885
  this.condition,
@@ -5182,6 +5392,9 @@ var createConstantNode = /* @__PURE__ */ factory(
5182
5392
  * @param {Object} options
5183
5393
  * @return {string} str
5184
5394
  */
5395
+ _toMathML() {
5396
+ return constantToMathML(this.value);
5397
+ }
5185
5398
  _toTex(options) {
5186
5399
  const value = this._toString(options);
5187
5400
  const type = typeOf(this.value);
@@ -5220,7 +5433,10 @@ var dependencies12 = ["typed", "Node"];
5220
5433
  var createFunctionAssignmentNode = /* @__PURE__ */ factory(
5221
5434
  name12,
5222
5435
  dependencies12,
5223
- ({ typed, Node }) => {
5436
+ ({
5437
+ typed,
5438
+ Node
5439
+ }) => {
5224
5440
  function needParenthesis(node, parenthesis, implicit) {
5225
5441
  const precedence = getPrecedence(node, parenthesis, implicit, void 0);
5226
5442
  const exprPrecedence = getPrecedence(node.expr, parenthesis, implicit, void 0);
@@ -5457,8 +5673,9 @@ var createFunctionNode = /* @__PURE__ */ factory(
5457
5673
  let latex = "";
5458
5674
  const regex = /\$(?:\{([a-z_][a-z_0-9]*)(?:\[([0-9]+)\])?\}|\$)/gi;
5459
5675
  let inputPos = 0;
5460
- let match;
5461
- while ((match = regex.exec(template)) !== null) {
5676
+ let matchOrNull;
5677
+ while ((matchOrNull = regex.exec(template)) !== null) {
5678
+ const match = matchOrNull;
5462
5679
  latex += template.substring(inputPos, match.index);
5463
5680
  inputPos = match.index;
5464
5681
  if (match[0] === "$$") {
@@ -5499,8 +5716,9 @@ var createFunctionNode = /* @__PURE__ */ factory(
5499
5716
  );
5500
5717
  }
5501
5718
  } else {
5502
- if (isNode(property[match[2]] && property[match[2]])) {
5503
- latex += property[match[2]].toTex(options);
5719
+ const indexed = property[match[2]];
5720
+ if (isNode(indexed && indexed)) {
5721
+ latex += indexed.toTex(options);
5504
5722
  } else {
5505
5723
  throw new TypeError("Template: " + match[1] + "[" + match[2] + "] is not a Node.");
5506
5724
  }
@@ -5648,8 +5866,9 @@ var createFunctionNode = /* @__PURE__ */ factory(
5648
5866
  };
5649
5867
  }
5650
5868
  } else if (isAccessorNode(this.fn) && isIndexNode(this.fn.index) && this.fn.index.isObjectProperty()) {
5651
- const evalObject = this.fn.object._compile(math2, argNames);
5652
- const prop = this.fn.index.getObjectProperty();
5869
+ const accessor = this.fn;
5870
+ const evalObject = accessor.object._compile(math2, argNames);
5871
+ const prop = accessor.index.getObjectProperty();
5653
5872
  const rawArgs = this.args;
5654
5873
  return function evalFunctionNode(scope, args, context) {
5655
5874
  const object = evalObject(scope, args, context);
@@ -5817,6 +6036,23 @@ var createFunctionNode = /* @__PURE__ */ factory(
5817
6036
  * @param {Object} options
5818
6037
  * @return {string} str
5819
6038
  */
6039
+ _toMathML() {
6040
+ const fnNode = this.fn;
6041
+ const name21 = fnNode.name ?? String(this.fn);
6042
+ const args = this.args;
6043
+ if (name21 === "sqrt" && args.length === 1) return `<msqrt>${args[0].toMathML()}</msqrt>`;
6044
+ if (name21 === "cbrt" && args.length === 1) {
6045
+ return `<mroot><mrow>${args[0].toMathML()}</mrow><mn>3</mn></mroot>`;
6046
+ }
6047
+ if (name21 === "nthRoot" && args.length === 2) {
6048
+ return `<mroot><mrow>${args[0].toMathML()}</mrow><mrow>${args[1].toMathML()}</mrow></mroot>`;
6049
+ }
6050
+ if (name21 === "abs" && args.length === 1) {
6051
+ return `<mrow><mo>|</mo>${args[0].toMathML()}<mo>|</mo></mrow>`;
6052
+ }
6053
+ const inner = args.map((a) => a.toMathML()).join("<mo>,</mo>");
6054
+ return `<mrow>${toMathMLSymbol(name21)}<mo>&#x2061;</mo><mrow><mo>(</mo>${inner}<mo>)</mo></mrow></mrow>`;
6055
+ }
5820
6056
  _toTex(options) {
5821
6057
  const args = this.args.map(function(arg) {
5822
6058
  return arg.toTex(options);
@@ -5825,8 +6061,9 @@ var createFunctionNode = /* @__PURE__ */ factory(
5825
6061
  if (latexFunctions[this.name]) {
5826
6062
  latexConverter = latexFunctions[this.name];
5827
6063
  }
5828
- if (math[this.name] && (typeof math[this.name].toTex === "function" || typeof math[this.name].toTex === "object" || typeof math[this.name].toTex === "string")) {
5829
- latexConverter = math[this.name].toTex;
6064
+ const mathEntry = math[this.name];
6065
+ if (mathEntry && (typeof mathEntry.toTex === "function" || typeof mathEntry.toTex === "object" || typeof mathEntry.toTex === "string")) {
6066
+ latexConverter = mathEntry.toTex;
5830
6067
  }
5831
6068
  let customToTex;
5832
6069
  switch (typeof latexConverter) {
@@ -5836,15 +6073,18 @@ var createFunctionNode = /* @__PURE__ */ factory(
5836
6073
  case "string":
5837
6074
  customToTex = expandTemplate(latexConverter, this, options);
5838
6075
  break;
5839
- case "object":
5840
- switch (typeof latexConverter[args.length]) {
6076
+ case "object": {
6077
+ const conv = latexConverter[args.length];
6078
+ switch (typeof conv) {
5841
6079
  case "function":
5842
- customToTex = latexConverter[args.length](this, options);
6080
+ customToTex = conv(this, options);
5843
6081
  break;
5844
6082
  case "string":
5845
- customToTex = expandTemplate(latexConverter[args.length], this, options);
6083
+ customToTex = expandTemplate(conv, this, options);
5846
6084
  break;
5847
6085
  }
6086
+ break;
6087
+ }
5848
6088
  }
5849
6089
  if (typeof customToTex !== "undefined") {
5850
6090
  return customToTex;
@@ -5924,7 +6164,7 @@ var createIndexNode = /* @__PURE__ */ factory(
5924
6164
  const evalDimensions = map(
5925
6165
  this.dimensions,
5926
6166
  function(dimension, i) {
5927
- const needsEnd = dimension.filter((node) => node.isSymbolNode && node.name === "end").length > 0;
6167
+ const needsEnd = dimension.filter((node) => !!(node.isSymbolNode && node.name === "end")).length > 0;
5928
6168
  if (needsEnd) {
5929
6169
  const childArgNames = Object.create(argNames);
5930
6170
  childArgNames.end = true;
@@ -5947,9 +6187,12 @@ var createIndexNode = /* @__PURE__ */ factory(
5947
6187
  );
5948
6188
  const index = getSafeProperty(math, "index");
5949
6189
  return function evalIndexNode(scope, args, context) {
5950
- const dimensions = map(evalDimensions, function(evalDimension) {
5951
- return evalDimension(scope, args, context);
5952
- });
6190
+ const dimensions = map(
6191
+ evalDimensions,
6192
+ function(evalDimension) {
6193
+ return evalDimension(scope, args, context);
6194
+ }
6195
+ );
5953
6196
  return index(...dimensions);
5954
6197
  };
5955
6198
  }
@@ -6253,7 +6496,8 @@ var createOperatorNode = /* @__PURE__ */ factory(
6253
6496
  function startsWithConstant(expr, parenthesis) {
6254
6497
  let curNode = expr;
6255
6498
  if (parenthesis === "auto") {
6256
- while (isParenthesisNode(curNode)) curNode = curNode.content;
6499
+ while (isParenthesisNode(curNode))
6500
+ curNode = curNode.content;
6257
6501
  }
6258
6502
  if (isConstantNode(curNode)) return true;
6259
6503
  if (isOperatorNode(curNode)) {
@@ -6277,19 +6521,14 @@ var createOperatorNode = /* @__PURE__ */ factory(
6277
6521
  }
6278
6522
  });
6279
6523
  }
6280
- let result;
6524
+ let result = [];
6281
6525
  switch (args.length) {
6282
6526
  case 0:
6283
6527
  result = [];
6284
6528
  break;
6285
6529
  case 1:
6286
6530
  {
6287
- const operandPrecedence = getPrecedence(
6288
- args[0],
6289
- parenthesis,
6290
- implicit,
6291
- root
6292
- );
6531
+ const operandPrecedence = getPrecedence(args[0], parenthesis, implicit, root);
6293
6532
  if (latex && operandPrecedence !== null) {
6294
6533
  let operandIdentifier;
6295
6534
  let rootIdentifier;
@@ -6695,6 +6934,27 @@ var createOperatorNode = /* @__PURE__ */ factory(
6695
6934
  * @param {Object} options
6696
6935
  * @return {string} str
6697
6936
  */
6937
+ _toMathML() {
6938
+ const fn = this.fn;
6939
+ const args = this.args;
6940
+ if (fn === "divide" && args.length === 2) {
6941
+ return `<mfrac><mrow>${args[0].toMathML()}</mrow><mrow>${args[1].toMathML()}</mrow></mfrac>`;
6942
+ }
6943
+ if (fn === "pow" && args.length === 2) {
6944
+ return `<msup><mrow>${args[0].toMathML()}</mrow><mrow>${args[1].toMathML()}</mrow></msup>`;
6945
+ }
6946
+ const child = (arg) => parenthesizeLower(arg.toMathML(), isOperatorNode(arg) ? arg.fn : void 0, fn);
6947
+ if (args.length === 1) {
6948
+ const op = this.op === "-" || this.op === "+" ? this.op : escapeMathML(this.op);
6949
+ const operand = child(args[0]);
6950
+ return getAssociativity(this, "keep") === "right" ? `<mrow><mo>${op}</mo>${operand}</mrow>` : `<mrow>${operand}<mo>${op}</mo></mrow>`;
6951
+ }
6952
+ if (args.length >= 2) {
6953
+ const opMml = fn === "multiply" ? this.implicit ? "<mo>&#x2062;</mo>" : "<mo>&#x22C5;</mo>" : inlineOperator(fn, this.op);
6954
+ return `<mrow>${args.map(child).join(opMml)}</mrow>`;
6955
+ }
6956
+ return `<mtext>${escapeMathML(this.toString())}</mtext>`;
6957
+ }
6698
6958
  _toTex(options) {
6699
6959
  const parenthesis = options && options.parenthesis ? options.parenthesis : "keep";
6700
6960
  const implicit = options && options.implicit ? options.implicit : "hide";
@@ -6909,6 +7169,9 @@ var createParenthesisNode = /* @__PURE__ */ factory(
6909
7169
  * @return {string} str
6910
7170
  * @override
6911
7171
  */
7172
+ _toMathML() {
7173
+ return `<mrow><mo>(</mo>${this.content.toMathML()}<mo>)</mo></mrow>`;
7174
+ }
6912
7175
  _toTex(options) {
6913
7176
  if (!options || options && !options.parenthesis || options && options.parenthesis === "keep") {
6914
7177
  return `\\left(${this.content.toTex(options)}\\right)`;
@@ -7364,6 +7627,18 @@ var createRelationalNode = /* @__PURE__ */ factory(
7364
7627
  * @param {Object} options
7365
7628
  * @return {string} str
7366
7629
  */
7630
+ _toMathML() {
7631
+ const params = this.params;
7632
+ const conds = this.conditionals;
7633
+ if (params.length >= 2 && conds.length === params.length - 1) {
7634
+ let out = params[0].toMathML();
7635
+ for (let i = 0; i < conds.length; i++) {
7636
+ out += `${inlineOperator(conds[i], conds[i])}${params[i + 1].toMathML()}`;
7637
+ }
7638
+ return `<mrow>${out}</mrow>`;
7639
+ }
7640
+ return `<mtext>${escapeMathML(this.toString())}</mtext>`;
7641
+ }
7367
7642
  _toTex(options) {
7368
7643
  const parenthesis = options && options.parenthesis ? options.parenthesis : "keep";
7369
7644
  const precedence = getPrecedence(
@@ -7441,7 +7716,7 @@ var createSymbolNode = /* @__PURE__ */ factory(
7441
7716
  _compile(math2, argNames) {
7442
7717
  const name21 = this.name;
7443
7718
  if (argNames[name21] === true) {
7444
- return function(scope, args, _context) {
7719
+ return function(_scope, args, _context) {
7445
7720
  return getSafeProperty(args, name21);
7446
7721
  };
7447
7722
  } else if (name21 in math2) {
@@ -7543,6 +7818,9 @@ var createSymbolNode = /* @__PURE__ */ factory(
7543
7818
  * @return {string} str
7544
7819
  * @override
7545
7820
  */
7821
+ _toMathML() {
7822
+ return toMathMLSymbol(this.name);
7823
+ }
7546
7824
  _toTex(_options) {
7547
7825
  let isUnit2 = false;
7548
7826
  if (typeof math[this.name] === "undefined" && isValuelessUnit(this.name)) {
@@ -7583,10 +7861,14 @@ export {
7583
7861
  createRangeNode,
7584
7862
  createRelationalNode,
7585
7863
  createSymbolNode,
7864
+ escapeMathML,
7586
7865
  getAssociativity,
7587
7866
  getOperator,
7588
7867
  getPrecedence,
7589
7868
  isAssociativeWith,
7590
7869
  keywords,
7591
- properties
7870
+ mathMLDocument,
7871
+ mathMLError,
7872
+ properties,
7873
+ toMathMLSymbol
7592
7874
  };