@pie-lib/math-evaluator 2.1.23 → 2.2.1-beta.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.
package/CHANGELOG.md CHANGED
@@ -3,7 +3,7 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
- ## [2.1.23](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.22...@pie-lib/math-evaluator@2.1.23) (2023-06-24)
6
+ ## [2.2.1-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.23...@pie-lib/math-evaluator@2.2.1-beta.0) (2025-07-20)
7
7
 
8
8
  **Note:** Version bump only for package @pie-lib/math-evaluator
9
9
 
@@ -11,7 +11,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
11
11
 
12
12
 
13
13
 
14
- ## [2.1.22](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.20...@pie-lib/math-evaluator@2.1.22) (2023-01-31)
14
+ # [2.2.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.23...@pie-lib/math-evaluator@2.2.0-beta.0) (2025-07-20)
15
15
 
16
16
  **Note:** Version bump only for package @pie-lib/math-evaluator
17
17
 
@@ -19,62 +19,50 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
19
19
 
20
20
 
21
21
 
22
- ## [2.1.20](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.19...@pie-lib/math-evaluator@2.1.20) (2023-01-31)
22
+ # [2.3.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.23...@pie-lib/math-evaluator@2.3.0-beta.0) (2025-07-15)
23
23
 
24
24
  **Note:** Version bump only for package @pie-lib/math-evaluator
25
25
 
26
+ # [2.2.0-beta.0](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.23...@pie-lib/math-evaluator@2.2.0-beta.0) (2025-07-15)
26
27
 
28
+ **Note:** Version bump only for package @pie-lib/math-evaluator
27
29
 
30
+ ## [2.1.23](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.22...@pie-lib/math-evaluator@2.1.23) (2023-06-24)
28
31
 
32
+ **Note:** Version bump only for package @pie-lib/math-evaluator
29
33
 
30
- ## [2.1.19](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.18...@pie-lib/math-evaluator@2.1.19) (2023-01-31)
34
+ ## [2.1.22](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.20...@pie-lib/math-evaluator@2.1.22) (2023-01-31)
31
35
 
32
36
  **Note:** Version bump only for package @pie-lib/math-evaluator
33
37
 
38
+ ## [2.1.20](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.19...@pie-lib/math-evaluator@2.1.20) (2023-01-31)
34
39
 
40
+ **Note:** Version bump only for package @pie-lib/math-evaluator
35
41
 
42
+ ## [2.1.19](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.18...@pie-lib/math-evaluator@2.1.19) (2023-01-31)
36
43
 
44
+ **Note:** Version bump only for package @pie-lib/math-evaluator
37
45
 
38
46
  ## [2.1.18](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.17...@pie-lib/math-evaluator@2.1.18) (2023-01-31)
39
47
 
40
48
  **Note:** Version bump only for package @pie-lib/math-evaluator
41
49
 
42
-
43
-
44
-
45
-
46
50
  ## [2.1.17](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.11...@pie-lib/math-evaluator@2.1.17) (2023-01-31)
47
51
 
48
52
  **Note:** Version bump only for package @pie-lib/math-evaluator
49
53
 
50
-
51
-
52
-
53
-
54
54
  ## [2.1.11](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.9...@pie-lib/math-evaluator@2.1.11) (2023-01-31)
55
55
 
56
56
  **Note:** Version bump only for package @pie-lib/math-evaluator
57
57
 
58
-
59
-
60
-
61
-
62
58
  ## [2.1.9](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.7...@pie-lib/math-evaluator@2.1.9) (2023-01-31)
63
59
 
64
60
  **Note:** Version bump only for package @pie-lib/math-evaluator
65
61
 
66
-
67
-
68
-
69
-
70
62
  ## [2.1.7](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.6...@pie-lib/math-evaluator@2.1.7) (2022-11-23)
71
63
 
72
64
  **Note:** Version bump only for package @pie-lib/math-evaluator
73
65
 
74
-
75
-
76
-
77
-
78
66
  ## [2.1.6](https://github.com/pie-framework/pie-lib/compare/@pie-lib/math-evaluator@2.1.4...@pie-lib/math-evaluator@2.1.6) (2021-06-25)
79
67
 
80
68
  **Note:** Version bump only for package @pie-lib/math-evaluator
@@ -0,0 +1 @@
1
+ []
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "2.1.23",
6
+ "version": "2.2.1-beta.0",
7
7
  "description": "Math evaluator tool for comparing expressions for equality",
8
8
  "keywords": [
9
9
  "math",
@@ -19,11 +19,12 @@
19
19
  "dependencies": {
20
20
  "@pie-framework/math-expressions": "^3.0.0",
21
21
  "jsesc": "^2.5.2",
22
+ "lodash": "^4.17.11",
22
23
  "mathjs": "^7.0.1"
23
24
  },
24
25
  "devDependencies": {
25
- "@pie-lib/test-utils": "^0.2.33"
26
+ "@pie-lib/test-utils": "^0.3.1-beta.0"
26
27
  },
27
28
  "scripts": {},
28
- "gitHead": "4043c98430c000ce3a25357d92845175b076ea21"
29
+ "gitHead": "e2aa3ddac60f49bcb8c2562370f496323642f453"
29
30
  }
@@ -0,0 +1,413 @@
1
+ import areValuesEqual, { ave, latexToText } from '../index';
2
+ import _ from 'lodash';
3
+
4
+ describe('math-evaluator', () => {
5
+ describe('latexToText', () => {
6
+ const assertLatexToText = (input, expected) => {
7
+ it(`${input} => ${expected}`, () => {
8
+ const result = latexToText(input);
9
+ expect(result).toEqual(expected);
10
+ });
11
+ };
12
+ assertLatexToText('\\frac{ \\frac{1}{3}}{3}', '(1/3)/3');
13
+ assertLatexToText('\\neq', '0 != 0');
14
+ assertLatexToText('10 \\neq 20', '10 != 20');
15
+ assertLatexToText('10 \\neq', '10 != 0');
16
+ assertLatexToText('\\neq 10', '0 != 10');
17
+ assertLatexToText('40%', 'percent(40)');
18
+ assertLatexToText('%', 'percent(0)');
19
+ assertLatexToText('12%', 'percent(12)');
20
+ assertLatexToText('12\\%', 'percent(12)');
21
+ assertLatexToText('-12.5%', '- percent(12.5)');
22
+ assertLatexToText('-12.5\\%', '- percent(12.5)');
23
+ });
24
+
25
+ const assert = (isEqual, opts) => (label) =>
26
+ function() {
27
+ const args = Array.from(arguments);
28
+ const pairs = _.chunk(args, 2);
29
+
30
+ describe(label, () => {
31
+ pairs.forEach(([a, b]) => {
32
+ it(`${a} === ${b} ? ${isEqual}`, () => {
33
+ expect(areValuesEqual(a, b, opts)).toBe(isEqual);
34
+ });
35
+ });
36
+ });
37
+ };
38
+
39
+ const assertEqual = assert(true);
40
+ const assertNotEqual = assert(false);
41
+
42
+ const assertLatexEqual = assert(true, { isLatex: true });
43
+ const assertLatexNotEqual = assert(false, { isLatex: true });
44
+
45
+ const _amjs = (only) => (a, b, equal) => {
46
+ const fn = only ? it.only : it;
47
+ fn(`${a} === ${b} => ${equal}`, () => {
48
+ const as = latexToText(a);
49
+ const bs = latexToText(b);
50
+ expect(ave(as, bs)).toEqual(equal);
51
+ });
52
+ };
53
+
54
+ const assertThroughMathJs = _amjs(false);
55
+ assertThroughMathJs.only = _amjs(true);
56
+
57
+ const _als = (only) => (input, expected) => {
58
+ const fn = only ? it.only : it;
59
+ fn(`${input} => ${expected}`, () => {
60
+ expect(latexToText(input)).toEqual(expected);
61
+ });
62
+ };
63
+
64
+ const assertLatexFromString = _als(false);
65
+ assertLatexFromString.only = _als(true);
66
+
67
+ describe('ch6456', () => {
68
+ assertEqual('percent')('-12.5%', '-12.5%');
69
+ assertEqual('percent')('-12.5\\%', '-12.5%');
70
+ assertNotEqual('percent')('-11.5%', '-12.5%');
71
+ });
72
+
73
+ describe('ch6456 - latex', () => {
74
+ assertLatexEqual('percent')('\\frac{1}{2} -12.5%', '\\frac{2}{4} -12.5%');
75
+ assertLatexEqual('percent')('\\frac{1}{2} -12.5%', '\\frac{2}{4} -12.5\\%');
76
+ assertLatexNotEqual('percent')('\\frac{4}{2} -12.5%', '\\frac{2}{4} -12.5\\%');
77
+ assertLatexNotEqual('percent')('\\frac{2}{2} -12.5%', '\\frac{2}{4} -12.5\\%');
78
+ });
79
+
80
+ describe('PIE-188-math-expressions', () => {
81
+ // it('parses expressions correctly', async () => {
82
+
83
+ // geometry
84
+
85
+ // MULTIPLY WITH VARIABLE BASED
86
+ assertThroughMathJs('\\parallel x', '\\parallel x', true);
87
+ assertThroughMathJs('\\parallel x', '\\parallel x', true);
88
+ assertThroughMathJs('\\parallel x', '\\parallel 0', false);
89
+ assertThroughMathJs('\\overrightarrow{x + 4}', '\\overrightarrow {4 + x}', true);
90
+ assertThroughMathJs('\\overrightarrow{4x^2 + 4}', '\\overrightarrow {4x^2 + 8 - 4}', true);
91
+ assertThroughMathJs('\\overrightarrow{21*2*x}', '\\overrightarrow {42x}', true);
92
+ assertLatexFromString('\\nparallel x', 'nparallel x');
93
+ assertLatexFromString('\\overrightarrow{x}', 'overrightarrow x');
94
+ assertLatexFromString('\\overleftrightarrow{x}', 'overleftrightarrow x');
95
+ assertLatexFromString('\\perp x', 'perp x');
96
+ assertLatexFromString('\\angle x', 'angle x');
97
+ assertLatexFromString('\\overarc x', 'overarc x');
98
+ assertLatexFromString('\\measuredangle x', 'measuredangle x');
99
+ assertLatexFromString('\\triangle x', 'triangle x');
100
+ assertLatexFromString('\\parallelogram x', 'parallelogram x');
101
+ assertLatexFromString('\\odot x', 'odot x');
102
+ assertLatexFromString('\\degree x', 'degree x');
103
+ assertLatexFromString('\\sim x', 'sim x');
104
+ assertLatexFromString('\\cong x', 'cong x');
105
+ assertLatexFromString('\\ncong x', 'ncong x');
106
+ assertLatexFromString('\\napprox x', 'napprox x'); // UNRECOGNIZED BY LEARNOSITY
107
+ assertLatexFromString('\\nim x', 'nim x'); // UNRECOGNIZED BY LEARNOSITY
108
+ assertLatexFromString('\\sim x', 'sim x');
109
+ expect(() => latexToText('\\sim 4', { unknownCommandBehavior: 'error' })).toThrow();
110
+
111
+ // comparisons
112
+
113
+ assertLatexFromString('1 \\lt 2', '1 < 2');
114
+
115
+ assertLatexFromString('1 \\gt 2', '1 > 2');
116
+
117
+ assertLatexFromString('1 \\le 2', '1 ≤ 2');
118
+
119
+ assertLatexFromString('1 \\ge 2', '1 ≥ 2');
120
+
121
+ // exponents
122
+
123
+ assertLatexFromString('2^2', '2^2');
124
+
125
+ assertLatexFromString('2^{3}', '2^3');
126
+
127
+ // roots
128
+
129
+ assertLatexFromString('\\sqrt{2}', 'sqrt(2)');
130
+
131
+ assertLatexFromString('\\sqrt[{3}]{3}', '3^(1/3)');
132
+
133
+ // fractions
134
+
135
+ assertLatexFromString('\\frac{3}{3}', '3/3');
136
+
137
+ assertLatexFromString('\\frac{x}{3}', 'x/3');
138
+
139
+ assertLatexFromString('x\\frac{5}{3}', 'x (5/3)');
140
+
141
+ // ACTUAL OPERATOR BASED
142
+ assertLatexFromString('\\overline{x}', 'overline x');
143
+ assertLatexFromString('\\pm', 'pm');
144
+ assertLatexFromString('\\approx x', 'approx x');
145
+
146
+ // logarithms
147
+
148
+ assertLatexFromString("4'", "4'");
149
+ assertLatexFromString('\\log 4', 'log(4)');
150
+ assertLatexFromString('\\log(4x)', 'log(4 x)');
151
+ assertLatexFromString('\\ln 4', 'ln(4)');
152
+
153
+ assertLatexFromString('|4|', '|4|');
154
+ assertLatexFromString('(4)', '4');
155
+ assertLatexFromString('(4 + x) * 5', '(4 + x) * 5');
156
+ assertLatexFromString('[4]', '4');
157
+ assertLatexFromString('\\mu', 'μ');
158
+ assertLatexFromString('\\Sigma', 'Σ');
159
+ assertLatexFromString('x^{15}', 'x^15');
160
+ assertLatexFromString('x_{15}', 'x_15');
161
+
162
+ // Trigo
163
+
164
+ assertLatexFromString('\\sin(x)', 'sin(x)');
165
+ assertLatexFromString('\\cos(x)', 'cos(x)');
166
+ assertLatexFromString('\\tan(x)', 'tan(x)');
167
+ assertLatexFromString('\\sec(x)', 'sec(x)');
168
+ assertLatexFromString('\\csc(x)', 'csc(x)');
169
+ assertLatexFromString('\\cot(x)', 'cot(x)');
170
+ });
171
+
172
+ // assertNotEqual('custom latex')('1530', `\\odot`);
173
+ assertNotEqual('evaluates simple expressions correctly')('0', 'x', '3x', '4x');
174
+
175
+ assertEqual('evaluates simple expressions correctly')('x', 'x', '2x', '2x');
176
+
177
+ describe('bad frac throws error', () => {
178
+ it('throws an error', () => {
179
+ expect(() => areValuesEqual('\\frac12', '\\frac34', { isLatex: true })).toThrow('Expecting {');
180
+ });
181
+ });
182
+ describe('7119', () => {
183
+ it.each`
184
+ v | isEqual
185
+ ${'4.5'} | ${true}
186
+ ${'3.2 + 1.3'} | ${true}
187
+ ${'(4 - \\frac{4}{5}) + 1.3'} | ${true}
188
+ ${'\\frac{9}{2}'} | ${true}
189
+ ${'4 \\frac{1}{2}'} | ${true}
190
+ ${'\\frac{10}{2} - 0.5'} | ${true}
191
+ ${'4.55'} | ${false}
192
+ `('$a == $isEqual', ({ v, isEqual }) => {
193
+ const result = areValuesEqual('4 \\frac{1}{2}', v, { isLatex: true });
194
+ expect(result).toEqual(isEqual);
195
+ });
196
+
197
+ it('?', () => {
198
+ expect(latexToText('4\\frac{1}{2}')).toEqual('4 + 1/2');
199
+ });
200
+ });
201
+
202
+ assertEqual('evaluates simple expressions correctly 2')(
203
+ '100',
204
+ '100',
205
+ '50 + 50',
206
+ '25 * 4',
207
+ '200 / 2',
208
+ '20 * 5',
209
+ '2.5 * 40',
210
+ '10 * 10',
211
+ );
212
+
213
+ assertNotEqual('evaluates simple expressions correctly 2')(
214
+ '100',
215
+ '0',
216
+ '0',
217
+ '50 * 2',
218
+ '44 + 57',
219
+ '100',
220
+ '44 + 57',
221
+ '50 * 3',
222
+ );
223
+
224
+ assertEqual('evaluates simple expressions correctly 3')(
225
+ '50 + 50',
226
+ '100',
227
+ '100 / 2 + 50',
228
+ '100',
229
+ '2 + 2',
230
+ '4',
231
+ '1/2 + .5',
232
+ '1',
233
+ );
234
+
235
+ assertNotEqual('evaluates simple expressions correctly 3')(
236
+ '50 + 51',
237
+ '100',
238
+ '25 * 2 + 51',
239
+ '100',
240
+ '1/2 + .5',
241
+ '1.9',
242
+ '1/2 + .5',
243
+ '1.1',
244
+ );
245
+
246
+ assertEqual('evaluates simple expressions correctly 4')(
247
+ '50 + 50',
248
+ '100',
249
+ '100 / 2 + 50',
250
+ '100',
251
+ '2 + 2',
252
+ '4',
253
+ '1/2 + .5',
254
+ '1',
255
+ '13/2',
256
+ '13/2',
257
+ '26/4',
258
+ '13/2',
259
+ '6 + 1/2',
260
+ '13/2',
261
+ '6.5',
262
+ '13/2',
263
+ );
264
+
265
+ assertNotEqual('evaluates simple expressions correctly 4')(
266
+ '50 + 51',
267
+ '100',
268
+ '25 * 2 + 51',
269
+ '100',
270
+ '1/2 + .5',
271
+ '1.9',
272
+ '1/2 + .5',
273
+ '1.1',
274
+ '14/2',
275
+ '13/2',
276
+ '6.6',
277
+ '13/2',
278
+ );
279
+
280
+ it.each`
281
+ a | b
282
+ ${'((x^2 + x) / x) - 1'} | ${'x'}
283
+ `('$a == $b', ({ a, b }) => {
284
+ expect(areValuesEqual(a, b)).toBe(true);
285
+ });
286
+
287
+ assertEqual('evaluates simple variable expressions correctly')(
288
+ 'x',
289
+ 'x',
290
+ 'x + 0',
291
+ 'x',
292
+ '(x-2) + 2',
293
+ 'x',
294
+ '((x^2 + x) / x) - 1',
295
+ 'x',
296
+ '2x/2',
297
+ 'x',
298
+ '(x + 2)^2',
299
+ '(x + 2)^2',
300
+ 'x^2 + 4x + 4',
301
+ '(x + 2)^2',
302
+ 'x^2 + 4(x+1)',
303
+ '(x + 2)^2',
304
+ 'x^2 + 8 ((x+1) / 2)',
305
+ '(x + 2)^2',
306
+ );
307
+
308
+ assertNotEqual('evaluates simple variable expressions correctly')(
309
+ 'y',
310
+ 'x',
311
+ 'x + 1',
312
+ 'x',
313
+ 'x^3 + 4x + 4',
314
+ '(x + 2)^2',
315
+ 'x^2 + 4(x+2)',
316
+ '(x + 2)^2',
317
+ );
318
+
319
+ assertEqual('evaluates simple variable expressions correctly 2')(
320
+ '(2 + x)^2',
321
+ '(x + 2)^2',
322
+ 'x^2 + 4x + 4',
323
+ '(x + 2)^2',
324
+ 'x^2 + 4(x+1)',
325
+ '(x + 2)^2',
326
+ 'x^2 + 8 ((x+1) / 2)',
327
+ '(x + 2)^2',
328
+ );
329
+
330
+ assertEqual('evaluates expressions correctly with variable exponents')(
331
+ '(2 + x)^2x',
332
+ '(x + 2)^2x',
333
+ 'y^(2 x)',
334
+ 'y^(x+x)',
335
+ );
336
+
337
+ assertEqual('evaluates function expressions correctly with variable parameters')(
338
+ 'sqrt(4x)',
339
+ 'sqrt(2x+2x)',
340
+ 'sqrt(x^2)',
341
+ 'sqrt(((x+1)^2) - ((x+1)^2) + x^2)',
342
+ );
343
+
344
+ assertNotEqual('evaluates simple variable expressions correctly 2')(
345
+ 'x^3 + 4x + 4',
346
+ '(x + 2)^2',
347
+ 'x^2 + 4(x+2)',
348
+ '(x + 2)^2',
349
+ );
350
+
351
+ it('correctly consumes allowDecimals option', () => {
352
+ expect(areValuesEqual('123', '123', { allowDecimals: true })).toEqual(true);
353
+ expect(areValuesEqual('123', '123.0', { allowDecimals: true })).toEqual(true);
354
+ expect(areValuesEqual('123,0', '123.0', { allowDecimals: true })).toEqual(true);
355
+ expect(areValuesEqual('123,0', '123.0', { allowDecimals: false })).toEqual(false);
356
+ expect(areValuesEqual('123', '123.0', { allowDecimals: false })).toEqual(false);
357
+ expect(areValuesEqual('123', '123,0', { allowDecimals: false })).toEqual(false);
358
+ });
359
+
360
+ it('correctly consumes allowDecimals option for complex examples too', () => {
361
+ expect(areValuesEqual('1500000', '1500000', { allowDecimals: true })).toEqual(true);
362
+ expect(areValuesEqual('1,500,000', '1,500,000', { allowDecimals: true })).toEqual(true);
363
+ expect(areValuesEqual('1500000', '1,500,000', { allowDecimals: true })).toEqual(true);
364
+ expect(areValuesEqual('1500000', '1,500,000.00', { allowDecimals: true })).toEqual(true);
365
+ expect(areValuesEqual('1500000', '1,500,000.0', { allowDecimals: true })).toEqual(true);
366
+ expect(areValuesEqual('1,500,000.0', '1,500,000.00', { allowDecimals: true })).toEqual(true);
367
+ expect(areValuesEqual('1500000', '1,500000', { allowDecimals: true })).toEqual(false);
368
+ expect(areValuesEqual('1500000', '1500,000', { allowDecimals: true })).toEqual(false);
369
+ expect(areValuesEqual('1500000', '1,500,000.01', { allowDecimals: true })).toEqual(false);
370
+ expect(areValuesEqual('1500000', '1,500,00,0', { allowDecimals: true })).toEqual(false);
371
+ expect(areValuesEqual('1500000', '1500,000', { allowDecimals: true })).toEqual(false);
372
+ expect(areValuesEqual('1500000', ',1500,000', { allowDecimals: true })).toEqual(false);
373
+ });
374
+
375
+ it('evaluates simple trigo expressions correctly', () => {
376
+ expect(areValuesEqual('sin(x)', 'sin(x)')).toEqual(true);
377
+ expect(areValuesEqual('tan(x)', 'tan(x)')).toEqual(true);
378
+ });
379
+
380
+ it('correctly creates math expressions 1', () => {
381
+ expect(
382
+ areValuesEqual('f^{-1}\\left(x\\right)=\\sqrt{x-1}+3', 'f^{-1}\\left(x\\right)=\\sqrt{x-1}+3', {
383
+ allowDecimals: true,
384
+ isLatex: true,
385
+ }),
386
+ ).toEqual(true);
387
+ });
388
+
389
+ it('correctly creates math expressions 2', () => {
390
+ expect(
391
+ areValuesEqual('72\\div12=6\\text{eggs}', '72\\div12=6\\text{eggs}', {
392
+ allowDecimals: true,
393
+ isLatex: true,
394
+ }),
395
+ ).toEqual(true);
396
+ });
397
+
398
+ describe('PD-3', () => {
399
+ it.each`
400
+ a | b
401
+ ${'\\left(x\\right)^{ \\frac{1}{\\left(y + 3\\right)}}'} | ${'\\sqrt[\\left(y + 3\\right)]{x}'}
402
+ ${'\\left(x\\right)^{\\frac{1}{3}}'} | ${'\\sqrt[3]{x}'}
403
+ ${'\\left(x\\right)^{\\frac{1}{n}}'} | ${'\\sqrt[n]{x}'}
404
+ ${'\\left(x\\right)^{\\frac{1}{2}}'} | ${'\\sqrt{x}'}
405
+ ${'\\left(x-1\\right)^{\\frac{1}{2}}+3'} | ${'\\sqrt{x-1}+3'}
406
+ ${'f^{-1}\\left(x\\right)=\\left(x-1\\right)^{\\frac{1}{2}}+3'}
407
+ ${'f^{-1}\\left(x\\right)=\\sqrt{x-1}+3'}
408
+ `('$a == $b', ({ a, b }) => {
409
+ const opts = { allowDecimals: true, isLatex: true };
410
+ expect(areValuesEqual(a, b, opts)).toBeTruthy();
411
+ });
412
+ });
413
+ });
package/lib/index.js DELETED
@@ -1,189 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- var _typeof = require("@babel/runtime/helpers/typeof");
6
-
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- exports.textToMathText = exports.latexToText = exports["default"] = exports.ave = void 0;
11
-
12
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
-
14
- var mathjs = _interopRequireWildcard(require("mathjs"));
15
-
16
- var _mathExpressions = _interopRequireDefault(require("@pie-framework/math-expressions"));
17
-
18
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
-
20
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
-
22
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
23
-
24
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
25
-
26
- var decimalCommaRegex = /,/g;
27
- var decimalRegex = /\.|,/g;
28
- var decimalWithThousandSeparatorNumberRegex = /^(?!0+\.00)(?=.{1,9}(\.|$))(?!0(?!\.))\d{1,3}(,\d{3})*(\.\d+)?$/;
29
-
30
- var rationalizeAllPossibleSubNodes = function rationalizeAllPossibleSubNodes(expression) {
31
- return rationalize(mathjs.parse(expression));
32
- };
33
-
34
- var rationalize = function rationalize(tree) {
35
- var transformedTree = tree.transform(function (node) {
36
- try {
37
- var rationalizedNode = mathjs.rationalize(node);
38
- return rationalizedNode;
39
- } catch (_unused) {
40
- return node;
41
- }
42
- });
43
- return transformedTree;
44
- };
45
-
46
- function prepareExpression(string, isLatex) {
47
- var returnValue = string ? string.trim() : '';
48
- returnValue = returnValue.replace(decimalCommaRegex, '.');
49
- returnValue = isLatex ? latexToText("".concat(returnValue)) : textToMathText("".concat(returnValue), {
50
- unknownCommands: 'passthrough'
51
- }).toString();
52
- returnValue = returnValue.replace('=', '==');
53
- return rationalizeAllPossibleSubNodes(returnValue);
54
- }
55
-
56
- var latexToAstOpts = {
57
- missingFactor: function missingFactor(token, e) {
58
- console.warn('missing factor for: ', token.token_type);
59
-
60
- if (token.token_type === 'NUMBER') {
61
- throw e;
62
- }
63
-
64
- return 0;
65
- },
66
- unknownCommandBehavior: 'passthrough'
67
- };
68
- var astToTextOpts = {
69
- unicode_operators: {
70
- ne: function ne(operands) {
71
- return operands.join(' != ');
72
- },
73
- '%': function _(operands) {
74
- return "percent(".concat(operands[0], ")");
75
- }
76
- }
77
- };
78
-
79
- var latexToText = function latexToText(latex) {
80
- var extraOtps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
81
- var la = new _mathExpressions["default"].converters.latexToAstObj(_objectSpread(_objectSpread({}, latexToAstOpts), extraOtps));
82
- var at = new _mathExpressions["default"].converters.astToTextObj(_objectSpread(_objectSpread({}, astToTextOpts), extraOtps));
83
- var ast = la.convert(latex);
84
- return at.convert(ast);
85
- };
86
-
87
- exports.latexToText = latexToText;
88
-
89
- var textToMathText = function textToMathText(latex) {
90
- var extraOtps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
91
- var la = new _mathExpressions["default"].converters.textToAstObj(_objectSpread(_objectSpread({}, latexToAstOpts), extraOtps));
92
- var at = new _mathExpressions["default"].converters.astToTextObj(_objectSpread(_objectSpread({}, astToTextOpts), extraOtps));
93
- var ast = la.convert(latex);
94
- return at.convert(ast);
95
- };
96
-
97
- exports.textToMathText = textToMathText;
98
-
99
- function shouldRationalizeEntireTree(tree) {
100
- var shouldDoIt = true; // we need to iterate the entire tree to check for some conditions that might make rationalization impossible
101
-
102
- try {
103
- tree.traverse(function (node) {
104
- // if we have a variable as an exponent for power operation, we should not rationalize
105
- // try to see if there are power operations with variable exponents
106
- if (node.type === 'OperatorNode' && node.fn === 'pow') {
107
- var exponent = node.args[1]; // try to see if it can be compiled and evaluated - if it's a non-numerical value, it'll throw an error
108
-
109
- exponent.compile().eval();
110
- } // we cannot have variables for unresolved function calls either
111
-
112
-
113
- if (node.type === 'FunctionNode') {
114
- //try to see if the argument that the function is called with can be resolved as non-variable
115
- var functionParameter = node.args[0]; // if it holds variables, this will throw an error
116
-
117
- functionParameter.compile().eval();
118
- }
119
- });
120
- mathjs.rationalize(tree);
121
- } catch (_unused2) {
122
- shouldDoIt = false;
123
- }
124
-
125
- return shouldDoIt;
126
- }
127
-
128
- function containsDecimal() {
129
- var expression = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
130
- return expression.match(decimalRegex);
131
- }
132
-
133
- var SIMPLIFY_RULES = [{
134
- l: 'n1^(1/n2)',
135
- r: 'nthRoot(n1, n2)'
136
- }, {
137
- l: 'sqrt(n1)',
138
- r: 'nthRoot(n1, 2)'
139
- }];
140
-
141
- var simplify = function simplify(v) {
142
- return mathjs.simplify(v, SIMPLIFY_RULES.concat(mathjs.simplify.rules));
143
- }; //.concat(SIMPLIFY_RULES));
144
-
145
-
146
- var areValuesEqual = function areValuesEqual(valueOne, valueTwo) {
147
- var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
148
- var allowDecimals = options.allowDecimals,
149
- isLatex = options.isLatex,
150
- inverse = options.inverse;
151
- var valueOneToUse = valueOne;
152
- var valueTwoToUse = valueTwo;
153
-
154
- if (allowDecimals === false) {
155
- if (containsDecimal(valueOne) || containsDecimal(valueTwo)) {
156
- return false;
157
- }
158
- } else if (allowDecimals === true) {
159
- if (containsDecimal(valueOne) && decimalWithThousandSeparatorNumberRegex.test(valueOne)) {
160
- valueOneToUse = valueOne.replace(decimalCommaRegex, '');
161
- }
162
-
163
- if (containsDecimal(valueTwo) && decimalWithThousandSeparatorNumberRegex.test(valueTwo)) {
164
- valueTwoToUse = valueTwo.replace(decimalCommaRegex, '');
165
- }
166
- }
167
-
168
- var preparedValueOne = prepareExpression(valueOneToUse, isLatex, allowDecimals);
169
- var preparedValueTwo = prepareExpression(valueTwoToUse, isLatex, allowDecimals);
170
- var one = shouldRationalizeEntireTree(preparedValueOne) ? mathjs.rationalize(preparedValueOne) : preparedValueOne;
171
- var two = shouldRationalizeEntireTree(preparedValueTwo) ? mathjs.rationalize(preparedValueTwo) : preparedValueTwo;
172
- one = simplify(one);
173
- two = simplify(two);
174
- var equals = one.equals(two);
175
- return inverse ? !equals : equals;
176
- };
177
-
178
- var ave = function ave(a, b) {
179
- var am = mathjs.parse(a);
180
- var bm = mathjs.parse(b);
181
- var arm = simplify(am);
182
- var brm = simplify(bm);
183
- return arm.equals(brm);
184
- };
185
-
186
- exports.ave = ave;
187
- var _default = areValuesEqual;
188
- exports["default"] = _default;
189
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.js"],"names":["decimalCommaRegex","decimalRegex","decimalWithThousandSeparatorNumberRegex","rationalizeAllPossibleSubNodes","expression","rationalize","mathjs","parse","tree","transformedTree","transform","node","rationalizedNode","prepareExpression","string","isLatex","returnValue","trim","replace","latexToText","textToMathText","unknownCommands","toString","latexToAstOpts","missingFactor","token","e","console","warn","token_type","unknownCommandBehavior","astToTextOpts","unicode_operators","ne","operands","join","latex","extraOtps","la","me","converters","latexToAstObj","at","astToTextObj","ast","convert","textToAstObj","shouldRationalizeEntireTree","shouldDoIt","traverse","type","fn","exponent","args","compile","eval","functionParameter","containsDecimal","match","SIMPLIFY_RULES","l","r","simplify","v","concat","rules","areValuesEqual","valueOne","valueTwo","options","allowDecimals","inverse","valueOneToUse","value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AAC4D,aAAD,EAAgB1D,OAAhB,EAAyBuD,aAAzB,CAA1C;AAEA,MAAIO,GAAG,GAAG9B,2BAA2B,CAAC4B,gBAAD,CAA3B,GAAgDrE,MAAM,CAACD,WAAP,CAAmBsE,gBAAnB,CAAhD,GAAuFA,gBAAjG;AACA,MAAIG,GAAG,GAAG/B,2BAA2B,CAAC6B,gBAAD,CAA3B,GAAgDtE,MAAM,CAACD,WAAP,CAAmBuE,gBAAnB,CAAhD,GAAuFA,gBAAjG;AAEAC,EAAAA,GAAG,GAAGf,QAAQ,CAACe,GAAD,CAAd;AACAC,EAAAA,GAAG,GAAGhB,QAAQ,CAACgB,GAAD,CAAd;AAEA,MAAMC,MAAM,GAAGF,GAAG,CAACE,MAAJ,CAAWD,GAAX,CAAf;AAEA,SAAOP,OAAO,GAAG,CAACQ,MAAJ,GAAaA,MAA3B;AACD,CAtCD;;AAwCO,IAAMC,GAAG,GAAG,SAANA,GAAM,CAACC,CAAD,EAAIC,CAAJ,EAAU;AAC3B,MAAMC,EAAE,GAAG7E,MAAM,CAACC,KAAP,CAAa0E,CAAb,CAAX;AACA,MAAMG,EAAE,GAAG9E,MAAM,CAACC,KAAP,CAAa2E,CAAb,CAAX;AAEA,MAAMG,GAAG,GAAGvB,QAAQ,CAACqB,EAAD,CAApB;AACA,MAAMG,GAAG,GAAGxB,QAAQ,CAACsB,EAAD,CAApB;AACA,SAAOC,GAAG,CAACN,MAAJ,CAAWO,GAAX,CAAP;AACD,CAPM;;;eASQpB,c","sourcesContent":["/* eslint-disable no-console */\nimport * as mathjs from 'mathjs';\nimport me from '@pie-framework/math-expressions';\n\nconst decimalCommaRegex = /,/g;\nconst decimalRegex = /\\.|,/g;\nconst decimalWithThousandSeparatorNumberRegex = /^(?!0+\\.00)(?=.{1,9}(\\.|$))(?!0(?!\\.))\\d{1,3}(,\\d{3})*(\\.\\d+)?$/;\n\nconst rationalizeAllPossibleSubNodes = (expression) => rationalize(mathjs.parse(expression));\nconst rationalize = (tree) => {\n const transformedTree = tree.transform((node) => {\n try {\n const rationalizedNode = mathjs.rationalize(node);\n return rationalizedNode;\n } catch {\n return node;\n }\n });\n return transformedTree;\n};\n\nfunction prepareExpression(string, isLatex) {\n let returnValue = string ? string.trim() : '';\n\n returnValue = returnValue.replace(decimalCommaRegex, '.');\n\n returnValue = isLatex\n ? latexToText(`${returnValue}`)\n : textToMathText(`${returnValue}`, { unknownCommands: 'passthrough' }).toString();\n\n returnValue = returnValue.replace('=', '==');\n return rationalizeAllPossibleSubNodes(returnValue);\n}\n\nconst latexToAstOpts = {\n missingFactor: (token, e) => {\n console.warn('missing factor for: ', token.token_type);\n if (token.token_type === 'NUMBER') {\n throw e;\n }\n return 0;\n },\n unknownCommandBehavior: 'passthrough',\n};\n\nconst astToTextOpts = {\n unicode_operators: {\n ne: function(operands) {\n return operands.join(' != ');\n },\n '%': function(operands) {\n return `percent(${operands[0]})`;\n },\n },\n};\n\nexport const latexToText = (latex, extraOtps = {}) => {\n const la = new me.converters.latexToAstObj({ ...latexToAstOpts, ...extraOtps });\n\n const at = new me.converters.astToTextObj({ ...astToTextOpts, ...extraOtps });\n\n const ast = la.convert(latex);\n\n return at.convert(ast);\n};\n\nexport const textToMathText = (latex, extraOtps = {}) => {\n const la = new me.converters.textToAstObj({ ...latexToAstOpts, ...extraOtps });\n\n const at = new me.converters.astToTextObj({ ...astToTextOpts, ...extraOtps });\n\n const ast = la.convert(latex);\n\n return at.convert(ast);\n};\n\nfunction shouldRationalizeEntireTree(tree) {\n let shouldDoIt = true;\n\n // we need to iterate the entire tree to check for some conditions that might make rationalization impossible\n try {\n tree.traverse((node) => {\n // if we have a variable as an exponent for power operation, we should not rationalize\n // try to see if there are power operations with variable exponents\n if (node.type === 'OperatorNode' && node.fn === 'pow') {\n const exponent = node.args[1];\n\n // try to see if it can be compiled and evaluated - if it's a non-numerical value, it'll throw an error\n exponent.compile().eval();\n }\n\n // we cannot have variables for unresolved function calls either\n if (node.type === 'FunctionNode') {\n //try to see if the argument that the function is called with can be resolved as non-variable\n const functionParameter = node.args[0];\n\n // if it holds variables, this will throw an error\n functionParameter.compile().eval();\n }\n });\n\n mathjs.rationalize(tree);\n } catch {\n shouldDoIt = false;\n }\n\n return shouldDoIt;\n}\n\nfunction containsDecimal(expression = '') {\n return expression.match(decimalRegex);\n}\nconst SIMPLIFY_RULES = [\n { l: 'n1^(1/n2)', r: 'nthRoot(n1, n2)' },\n { l: 'sqrt(n1)', r: 'nthRoot(n1, 2)' },\n];\n\nconst simplify = (v) => mathjs.simplify(v, SIMPLIFY_RULES.concat(mathjs.simplify.rules)); //.concat(SIMPLIFY_RULES));\n\nconst areValuesEqual = (valueOne, valueTwo, options = {}) => {\n const {\n // if explicitly set to false, having a decimal value in either side will result in a false equality\n // regardless of mathematical correctness\n allowDecimals,\n isLatex, // if the passed in values are latex, they need to be escaped\n inverse, // returns inverse for the comparison result\n } = options;\n\n let valueOneToUse = valueOne;\n let valueTwoToUse = valueTwo;\n\n if (allowDecimals === false) {\n if (containsDecimal(valueOne) || containsDecimal(valueTwo)) {\n return false;\n }\n } else if (allowDecimals === true) {\n if (containsDecimal(valueOne) && decimalWithThousandSeparatorNumberRegex.test(valueOne)) {\n valueOneToUse = valueOne.replace(decimalCommaRegex, '');\n }\n\n if (containsDecimal(valueTwo) && decimalWithThousandSeparatorNumberRegex.test(valueTwo)) {\n valueTwoToUse = valueTwo.replace(decimalCommaRegex, '');\n }\n }\n\n const preparedValueOne = prepareExpression(valueOneToUse, isLatex, allowDecimals);\n const preparedValueTwo = prepareExpression(valueTwoToUse, isLatex, allowDecimals);\n\n let one = shouldRationalizeEntireTree(preparedValueOne) ? mathjs.rationalize(preparedValueOne) : preparedValueOne;\n let two = shouldRationalizeEntireTree(preparedValueTwo) ? mathjs.rationalize(preparedValueTwo) : preparedValueTwo;\n\n one = simplify(one);\n two = simplify(two);\n\n const equals = one.equals(two);\n\n return inverse ? !equals : equals;\n};\n\nexport const ave = (a, b) => {\n const am = mathjs.parse(a);\n const bm = mathjs.parse(b);\n\n const arm = simplify(am);\n const brm = simplify(bm);\n return arm.equals(brm);\n};\n\nexport default areValuesEqual;\n"],"file":"index.js"}