mirror of
https://github.com/Karaka-Management/jsOMS.git
synced 2026-01-28 17:28:40 +00:00
Fixed math processor and added tests
This commit is contained in:
parent
45017a4936
commit
3a314dbda0
|
|
@ -28,12 +28,14 @@
|
|||
}
|
||||
}
|
||||
|
||||
return stack.pop();
|
||||
const result = stack.pop();
|
||||
|
||||
return !isNaN(parseFloat(result)) && isFinite(result) ? result : null;
|
||||
};
|
||||
|
||||
jsOMS.parseValue = function(value)
|
||||
{
|
||||
return value.indexOf('.') === -1 ? parseInt(value) : parseFloat(value);
|
||||
return typeof value === 'string' ? (value.indexOf('.') === -1 ? parseInt(value) : parseFloat(value)) : value;
|
||||
}
|
||||
|
||||
jsOMS.shuntingYard = function(equation)
|
||||
|
|
@ -41,8 +43,8 @@
|
|||
const stack = [];
|
||||
const operators = {
|
||||
'^': {precedence: 4, order: 1},
|
||||
'/': {precedence: 3, order: -1},
|
||||
'*': {precedence: 3, order: -1},
|
||||
'/': {precedence: 3, order: -1},
|
||||
'+': {precedence: 2, order: -1},
|
||||
'-': {precedence: 2, order: -1},
|
||||
};
|
||||
|
|
@ -51,9 +53,11 @@
|
|||
equation = equation.replace(/\s+/g, '');
|
||||
equation = equation.split(/([\+\-\*\/\^\(\)])/).filter(function (n) { return n !== '' });
|
||||
|
||||
let length = equation.length;
|
||||
const length = equation.length;
|
||||
let token;
|
||||
|
||||
for (let i = 0; i < length; ++i) {
|
||||
let token = equation[i];
|
||||
token = equation[i];
|
||||
|
||||
if (!isNaN(parseFloat(token)) && isFinite(token)) {
|
||||
output.push(token);
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ describe('MathProcessorTest', function ()
|
|||
{
|
||||
"use strict";
|
||||
|
||||
beforeEach(function ()
|
||||
describe('testEvaluation', function ()
|
||||
{
|
||||
it('Testing formula evaluation', function ()
|
||||
{
|
||||
expect(jsOMS.mathEvaluate('3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 + 1.5')).toBeCloseTo(4.5, 2);
|
||||
expect(jsOMS.mathEvaluate('invalid')).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function ()
|
||||
{
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -25,10 +25,9 @@
|
|||
<script src="../Account/AccountType.js"></script>
|
||||
|
||||
<script src="../Config/Options.js"></script>
|
||||
|
||||
<script src="../DataStorage/LocalStorage.js"></script>
|
||||
|
||||
<script src="../Event/EventManager.js"></script>
|
||||
<script src="../Math/MathProcessor.js"></script>
|
||||
|
||||
<script src="../Uri/Http.js"></script>
|
||||
<script src="../Uri/UriFactory.js"></script>
|
||||
|
|
@ -41,10 +40,9 @@
|
|||
<script src="Account/AccountTypeTest.js"></script>
|
||||
|
||||
<script src="Config/OptionsTest.js"></script>
|
||||
|
||||
<script src="DataStorage/LocalStorageTest.js"></script>
|
||||
|
||||
<script src="Event/EventManagerTest.js"></script>
|
||||
<script src="Math/MathProcessorTest.js"></script>
|
||||
|
||||
<script src="Uri/HttpTest.js"></script>
|
||||
<script src="Uri/UriFactoryTest.js"></script>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user