Add support for NumVariable expressions such as 2x fixes #6
This commit is contained in:
58
lib/index.js
58
lib/index.js
@ -47,30 +47,44 @@ let Equation = {
|
||||
*/
|
||||
equation(expression) {
|
||||
let stack = parseExpression(expression);
|
||||
console.log(stack);
|
||||
let variables = [];
|
||||
|
||||
stack.forEach(function varCheck(a) {
|
||||
if (Array.isArray(a)) {
|
||||
return a.forEach(varCheck);
|
||||
}
|
||||
|
||||
if (typeof a === 'string' && !_.isNumber(a) &&
|
||||
!operators[a] && a === a.toLowerCase()) {
|
||||
if (isVariable(a)) {
|
||||
// grouped variables like (y) need to have their parantheses removed
|
||||
variables.push(_.removeSymbols(a));
|
||||
}
|
||||
});
|
||||
|
||||
return function(...args) {
|
||||
expression = expression.replace(/[a-z]*/g, a => {
|
||||
stack.forEach(function varCheck(a, i, arr) {
|
||||
if (Array.isArray(a)) {
|
||||
return a.forEach(varCheck);
|
||||
}
|
||||
|
||||
let index = variables.indexOf(a);
|
||||
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;
|
||||
};
|
||||
},
|
||||
|
||||
@ -118,12 +132,14 @@ const MIN_PRECEDENCE = Math.min(...PRECEDENCES);
|
||||
const parseExpression = expression => {
|
||||
let stream = new ReadStream(expression),
|
||||
stack = [],
|
||||
record = '';
|
||||
record = '',
|
||||
cur, past;
|
||||
|
||||
// Create an array of separated numbers & operators
|
||||
while (stream.next()) {
|
||||
const cur = stream.current(),
|
||||
past = stack.length - 1;
|
||||
cur = stream.current();
|
||||
past = stack.length - 1;
|
||||
|
||||
if (cur === ' ') {
|
||||
continue;
|
||||
}
|
||||
@ -134,8 +150,14 @@ const parseExpression = expression => {
|
||||
|
||||
record += cur;
|
||||
} else if (record.length) {
|
||||
let beforeRecord = past - (record.length - 1);
|
||||
if (isVariable(record) && _.isNumber(stack[beforeRecord])) {
|
||||
stack.push('*');
|
||||
}
|
||||
stack.push(record, cur);
|
||||
record = '';
|
||||
|
||||
// numbers and decimals
|
||||
} else if (_.isNumber(stack[past]) &&
|
||||
(_.isNumber(cur) || cur === '.')) {
|
||||
|
||||
@ -165,6 +187,10 @@ const parseExpression = expression => {
|
||||
}
|
||||
}
|
||||
if (record.length) {
|
||||
let beforeRecord = past - (record.length - 1);
|
||||
if (isVariable(record) && _.isNumber(stack[beforeRecord])) {
|
||||
stack.push('*');
|
||||
}
|
||||
stack.push(record);
|
||||
}
|
||||
|
||||
@ -341,4 +367,16 @@ const 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
|
||||
*/
|
||||
export const isVariable = a => {
|
||||
return typeof a === 'string' && !_.isNumber(a) &&
|
||||
!operators[a] && a === a.toLowerCase();
|
||||
};
|
||||
|
||||
export default Equation;
|
||||
|
Reference in New Issue
Block a user