feat: support negation sign (-)

bump version to 1.1.0
This commit is contained in:
Mahdi Dibaiee
2015-04-22 17:01:10 +04:30
parent c5fed50881
commit e2c61896ca
8 changed files with 78 additions and 78 deletions

View File

@ -118,10 +118,12 @@ const parseExpression = expression => {
// Create an array of separated numbers & operators
while (stream.next()) {
const cur = stream.current();
const cur = stream.current(),
past = stack.length - 1;
if (cur === ' ') {
continue;
}
// it's probably a function with a length more than one
if (!_.isNumber(cur) && !operators[cur] && cur !== '.') {
record += cur;
@ -132,6 +134,19 @@ const parseExpression = expression => {
(_.isNumber(cur) || cur === '.')) {
stack[stack.length - 1] += cur;
} else if (stack[past] === '-') {
const beforeSign = stack[stack.length - 2];
if (operators[beforeSign]) {
stack[past] += cur;
} else if (beforeSign === ')') {
stack[past] = '+';
stack.push(`-${cur}`);
} else if (_.isNumber(beforeSign)) {
stack.push(cur);
} else {
stack[past] += cur;
}
} else {
stack.push(cur);
}