feat: support negation sign (-)
bump version to 1.1.0
This commit is contained in:
17
lib/index.js
17
lib/index.js
@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user