@teachinglab/omd 0.7.8 → 0.7.9

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/omdTable.js +32 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@teachinglab/omd",
3
- "version": "0.7.8",
3
+ "version": "0.7.9",
4
4
  "description": "omd",
5
5
  "main": "./index.js",
6
6
  "module": "./index.js",
package/src/omdTable.js CHANGED
@@ -172,22 +172,46 @@ export class omdTable extends jsvgGroup
172
172
  this.data = [];
173
173
  this.headers = ['x', 'y'];
174
174
 
175
- // Basic normalization for inline math
176
175
  let expression = this.equation;
177
- if (expression.toLowerCase().startsWith('y=')) {
178
- expression = expression.substring(2).trim();
176
+ // Remove "y=" from start or "=y" from end, case insensitive, handling spaces
177
+ expression = expression.replace(/^\s*y\s*=\s*/i, '').replace(/\s*=\s*y\s*$/i, '');
178
+
179
+ try {
180
+ // Use math.js if available (preferred)
181
+ if (typeof math !== 'undefined' && math.compile) {
182
+ const compiled = math.compile(expression);
183
+ for (let x = this.xMin; x <= this.xMax; x += this.stepSize) {
184
+ try {
185
+ let y = compiled.evaluate({ x });
186
+ // Round to 2 decimal places for display
187
+ y = Math.round(y * 100) / 100;
188
+ this.data.push([x, y]);
189
+ } catch (e) {
190
+ // Skip points that fail to evaluate
191
+ }
192
+ }
193
+ return;
194
+ }
195
+ } catch (e) {
196
+ console.warn("math.js evaluation failed, falling back to simple parser:", e);
179
197
  }
198
+
199
+ // Fallback to simple regex-based parsing
180
200
  expression = expression
181
201
  .replace(/(\d)([a-z])/gi, '$1*$2')
182
202
  .replace(/([a-z])(\d)/gi, '$1*$2')
183
203
  .replace(/\^/g, '**');
184
204
 
185
- const evaluateExpression = new Function('x', `return ${expression};`);
205
+ try {
206
+ const evaluateExpression = new Function('x', `return ${expression};`);
186
207
 
187
- for (let x = this.xMin; x <= this.xMax; x += this.stepSize) {
188
- let y = evaluateExpression(x);
189
- y = Math.round(y * 100) / 100;
190
- this.data.push([x, y]);
208
+ for (let x = this.xMin; x <= this.xMax; x += this.stepSize) {
209
+ let y = evaluateExpression(x);
210
+ y = Math.round(y * 100) / 100;
211
+ this.data.push([x, y]);
212
+ }
213
+ } catch (e) {
214
+ console.error("Error generating table data from equation:", e);
191
215
  }
192
216
  }
193
217