rebuild
This commit is contained in:
parent
adb74a6f31
commit
d360065e1b
103
equation.js
103
equation.js
@ -247,10 +247,14 @@ var Equation = {
|
|||||||
*/
|
*/
|
||||||
equation: function equation(expression) {
|
equation: function equation(expression) {
|
||||||
var stack = parseExpression(expression);
|
var stack = parseExpression(expression);
|
||||||
|
console.log(stack);
|
||||||
var variables = [];
|
var variables = [];
|
||||||
|
|
||||||
stack.forEach(function (a) {
|
stack.forEach(function varCheck(a) {
|
||||||
if (typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase()) {
|
if (Array.isArray(a)) {
|
||||||
|
return a.forEach(varCheck);
|
||||||
|
}
|
||||||
|
if (isVariable(a)) {
|
||||||
// grouped variables like (y) need to have their parantheses removed
|
// grouped variables like (y) need to have their parantheses removed
|
||||||
variables.push(_.removeSymbols(a));
|
variables.push(_.removeSymbols(a));
|
||||||
}
|
}
|
||||||
@ -261,15 +265,30 @@ var Equation = {
|
|||||||
args[_key] = arguments[_key];
|
args[_key] = arguments[_key];
|
||||||
}
|
}
|
||||||
|
|
||||||
expression = expression.replace(/[a-z]*/g, function (a) {
|
stack.forEach(function varCheck(a, i, arr) {
|
||||||
|
if (Array.isArray(a)) {
|
||||||
|
return a.forEach(varCheck);
|
||||||
|
}
|
||||||
|
|
||||||
var index = variables.indexOf(a);
|
var index = variables.indexOf(a);
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
return args[index] || 0;
|
// grouped variables like (y) need to have their parantheses removed
|
||||||
|
arr[i] = args[index];
|
||||||
}
|
}
|
||||||
return a;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return Equation.solve(expression);
|
stack = sortStack(stack);
|
||||||
|
stack = _.parseNumbers(stack);
|
||||||
|
stack = solveStack(stack);
|
||||||
|
// expression = expression.replace(/[a-z]*/g, a => {
|
||||||
|
// let index = variables.indexOf(a);
|
||||||
|
// if (index > -1) {
|
||||||
|
// return args[index] || 0;
|
||||||
|
// }
|
||||||
|
// return a;
|
||||||
|
// });
|
||||||
|
|
||||||
|
return stack;
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -327,33 +346,50 @@ var MIN_PRECEDENCE = Math.min.apply(Math, _toConsumableArray(PRECEDENCES));
|
|||||||
var parseExpression = function parseExpression(expression) {
|
var parseExpression = function parseExpression(expression) {
|
||||||
var stream = new _ReadStream2['default'](expression),
|
var stream = new _ReadStream2['default'](expression),
|
||||||
stack = [],
|
stack = [],
|
||||||
record = '';
|
record = '',
|
||||||
|
cur = undefined,
|
||||||
|
past = undefined;
|
||||||
|
|
||||||
// Create an array of separated numbers & operators
|
// Create an array of separated numbers & operators
|
||||||
while (stream.next()) {
|
while (stream.next()) {
|
||||||
var cur = stream.current(),
|
cur = stream.current();
|
||||||
past = stack.length - 1;
|
past = stack.length - 1;
|
||||||
|
|
||||||
if (cur === ' ') {
|
if (cur === ' ') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// it's probably a function with a length more than one
|
// it's probably a function with a length more than one
|
||||||
if (!_.isNumber(cur) && !_operators2['default'][cur] && cur !== '.') {
|
if (!_.isNumber(cur) && !_operators2['default'][cur] && cur !== '.' && cur !== '(' && cur !== ')') {
|
||||||
|
|
||||||
record += cur;
|
record += cur;
|
||||||
} else if (record.length) {
|
} else if (record.length) {
|
||||||
|
var beforeRecord = past - (record.length - 1);
|
||||||
|
if (isVariable(record) && _.isNumber(stack[beforeRecord])) {
|
||||||
|
stack.push('*');
|
||||||
|
}
|
||||||
stack.push(record, cur);
|
stack.push(record, cur);
|
||||||
record = '';
|
record = '';
|
||||||
} else if (_.isNumber(stack[stack.length - 1]) && (_.isNumber(cur) || cur === '.')) {
|
|
||||||
|
|
||||||
stack[stack.length - 1] += cur;
|
// numbers and decimals
|
||||||
|
} else if (_.isNumber(stack[past]) && (_.isNumber(cur) || cur === '.')) {
|
||||||
|
|
||||||
|
stack[past] += cur;
|
||||||
|
|
||||||
|
// negation sign
|
||||||
} else if (stack[past] === '-') {
|
} else if (stack[past] === '-') {
|
||||||
var beforeSign = stack[stack.length - 2];
|
var beforeSign = stack[past - 1];
|
||||||
|
|
||||||
|
// 2 / -5 is OK, pass
|
||||||
if (_operators2['default'][beforeSign]) {
|
if (_operators2['default'][beforeSign]) {
|
||||||
stack[past] += cur;
|
stack[past] += cur;
|
||||||
|
|
||||||
|
// (2+1) - 5 becomes (2+1) + -5
|
||||||
} else if (beforeSign === ')') {
|
} else if (beforeSign === ')') {
|
||||||
stack[past] = '+';
|
stack[past] = '+';
|
||||||
stack.push('-' + cur);
|
stack.push('-' + cur);
|
||||||
|
|
||||||
|
// 2 - 5 is also OK, pass
|
||||||
} else if (_.isNumber(beforeSign)) {
|
} else if (_.isNumber(beforeSign)) {
|
||||||
stack.push(cur);
|
stack.push(cur);
|
||||||
} else {
|
} else {
|
||||||
@ -364,6 +400,10 @@ var parseExpression = function parseExpression(expression) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (record.length) {
|
if (record.length) {
|
||||||
|
var beforeRecord = past - (record.length - 1);
|
||||||
|
if (isVariable(record) && _.isNumber(stack[beforeRecord])) {
|
||||||
|
stack.push('*');
|
||||||
|
}
|
||||||
stack.push(record);
|
stack.push(record);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -530,7 +570,7 @@ var evaluate = function evaluate(stack) {
|
|||||||
var leftArguments = stack.slice(0, left),
|
var leftArguments = stack.slice(0, left),
|
||||||
rightArguments = stack.slice(left + 1);
|
rightArguments = stack.slice(left + 1);
|
||||||
|
|
||||||
return (_operators$op = _operators2['default'][op]).fn.apply(_operators$op, _toConsumableArray(leftArguments).concat(_toConsumableArray(rightArguments)));
|
return fixFloat((_operators$op = _operators2['default'][op]).fn.apply(_operators$op, _toConsumableArray(leftArguments).concat(_toConsumableArray(rightArguments))));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -590,8 +630,31 @@ var replaceConstants = function replaceConstants(expression) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fixes JavaScript's floating point precisions - Issue #5
|
||||||
|
*
|
||||||
|
* @param {Number} number
|
||||||
|
* The number to fix
|
||||||
|
* @return {Number}
|
||||||
|
* Fixed number
|
||||||
|
*/
|
||||||
|
var fixFloat = function fixFloat(number) {
|
||||||
|
return +number.toFixed(15);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recognizes variables such as x, y, z
|
||||||
|
* @param {String} a
|
||||||
|
* The string to check for
|
||||||
|
* @return {Boolean}
|
||||||
|
* true if variable, else false
|
||||||
|
*/
|
||||||
|
var isVariable = function isVariable(a) {
|
||||||
|
return typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase();
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.isVariable = isVariable;
|
||||||
exports['default'] = Equation;
|
exports['default'] = Equation;
|
||||||
module.exports = exports['default'];
|
|
||||||
},{"./constants":1,"./helpers":2,"./operators":4,"./readstream":5}],4:[function(require,module,exports){
|
},{"./constants":1,"./helpers":2,"./operators":4,"./readstream":5}],4:[function(require,module,exports){
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
@ -704,6 +767,16 @@ exports['default'] = {
|
|||||||
fn: Math.cot,
|
fn: Math.cot,
|
||||||
format: '10',
|
format: '10',
|
||||||
precedence: -1
|
precedence: -1
|
||||||
|
},
|
||||||
|
round: {
|
||||||
|
fn: Math.round,
|
||||||
|
format: '10',
|
||||||
|
precedence: -1
|
||||||
|
},
|
||||||
|
floor: {
|
||||||
|
fn: Math.floor,
|
||||||
|
format: '10',
|
||||||
|
precedence: -1
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
module.exports = exports['default'];
|
module.exports = exports['default'];
|
||||||
|
2
equation.min.js
vendored
2
equation.min.js
vendored
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user