feat: support negation sign (-)
bump version to 1.1.0
This commit is contained in:
parent
c5fed50881
commit
e2c61896ca
17
dist/index.js
vendored
17
dist/index.js
vendored
@ -154,10 +154,12 @@ var parseExpression = function parseExpression(expression) {
|
||||
|
||||
// Create an array of separated numbers & operators
|
||||
while (stream.next()) {
|
||||
var cur = stream.current();
|
||||
var cur = stream.current(),
|
||||
past = stack.length - 1;
|
||||
if (cur === ' ') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// it's probably a function with a length more than one
|
||||
if (!_.isNumber(cur) && !_operators2['default'][cur] && cur !== '.') {
|
||||
record += cur;
|
||||
@ -167,6 +169,19 @@ var parseExpression = function parseExpression(expression) {
|
||||
} else if (_.isNumber(stack[stack.length - 1]) && (_.isNumber(cur) || cur === '.')) {
|
||||
|
||||
stack[stack.length - 1] += cur;
|
||||
} else if (stack[past] === '-') {
|
||||
var beforeSign = stack[stack.length - 2];
|
||||
|
||||
if (_operators2['default'][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);
|
||||
}
|
||||
|
73
dist/tests/basic.js
vendored
73
dist/tests/basic.js
vendored
@ -1,73 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
|
||||
|
||||
var _expect = require('chai');
|
||||
|
||||
var _M = require('../index.js');
|
||||
|
||||
var _M2 = _interopRequireWildcard(_M);
|
||||
|
||||
describe('Basic math operators', function () {
|
||||
it('should work for add +', function () {
|
||||
_expect.expect(_M2['default'].solve('2+2')).to.equal(4);
|
||||
});
|
||||
|
||||
it('should work for minus -', function () {
|
||||
_expect.expect(_M2['default'].solve('15-3')).to.equal(12);
|
||||
});
|
||||
|
||||
it('should work for divison /', function () {
|
||||
_expect.expect(_M2['default'].solve('20/2')).to.equal(10);
|
||||
});
|
||||
|
||||
it('should work for multiplication *', function () {
|
||||
_expect.expect(_M2['default'].solve('6*3')).to.equal(18);
|
||||
});
|
||||
|
||||
it('should work for power ^', function () {
|
||||
_expect.expect(_M2['default'].solve('5^2')).to.equal(25);
|
||||
});
|
||||
|
||||
it('should work for multi-digit numbers', function () {
|
||||
_expect.expect(_M2['default'].solve('12+15')).to.equal(27);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Precedence', function () {
|
||||
it('Test case 1', function () {
|
||||
_expect.expect(_M2['default'].solve('2+(2+1)*(1+1)^2')).to.equal(14);
|
||||
});
|
||||
|
||||
it('Test case 2', function () {
|
||||
_expect.expect(_M2['default'].solve('2+5*4/2-2')).to.equal(10);
|
||||
});
|
||||
|
||||
it('Test case 3', function () {
|
||||
_expect.expect(_M2['default'].solve('2+(5*4/2)-2')).to.equal(10);
|
||||
});
|
||||
|
||||
it('Test case 4', function () {
|
||||
_expect.expect(_M2['default'].solve('(2+2)^2+(5+1)*4+(2+(4/2)/2)')).to.equal(16 + 24 + 3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Functions', function () {
|
||||
it('should work for with parantheses', function () {
|
||||
_expect.expect(_M2['default'].solve('lg(4) * 5')).to.equal(10);
|
||||
});
|
||||
|
||||
it('should work for without parantheses', function () {
|
||||
_expect.expect(_M2['default'].solve('lg4 * 5')).to.equal(10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Constats', function () {
|
||||
it('should work for constant values', function () {
|
||||
_expect.expect(_M2['default'].solve('sin(PI/2)')).to.equal(1);
|
||||
});
|
||||
|
||||
it('should work for functions as constants', function () {
|
||||
_expect.expect(_M2['default'].solve('RAND')).to.not.equal(_M2['default'].solve('RAND'));
|
||||
});
|
||||
});
|
14
dist/tests/solve.js
vendored
14
dist/tests/solve.js
vendored
@ -34,6 +34,20 @@ describe('Basic math operators', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Negative Numbers', function () {
|
||||
it('should work for negative numbers after operators', function () {
|
||||
_expect.expect(_M2['default'].solve('2 + -5')).to.equal(-3);
|
||||
});
|
||||
|
||||
it('should work for negative numbers after groups', function () {
|
||||
_expect.expect(_M2['default'].solve('1 + (2 - 5) - 2')).to.equal(-4);
|
||||
});
|
||||
|
||||
it('should work for expressions starting with negative numbers', function () {
|
||||
_expect.expect(_M2['default'].solve('-2 + 1')).to.equal(-1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Precedence', function () {
|
||||
it('Test case 1', function () {
|
||||
_expect.expect(_M2['default'].solve('2+(2+1)*(1+1)^2')).to.equal(14);
|
||||
|
17
equation.js
17
equation.js
@ -331,10 +331,12 @@ var parseExpression = function parseExpression(expression) {
|
||||
|
||||
// Create an array of separated numbers & operators
|
||||
while (stream.next()) {
|
||||
var cur = stream.current();
|
||||
var cur = stream.current(),
|
||||
past = stack.length - 1;
|
||||
if (cur === ' ') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// it's probably a function with a length more than one
|
||||
if (!_.isNumber(cur) && !_operators2['default'][cur] && cur !== '.') {
|
||||
record += cur;
|
||||
@ -344,6 +346,19 @@ var parseExpression = function parseExpression(expression) {
|
||||
} else if (_.isNumber(stack[stack.length - 1]) && (_.isNumber(cur) || cur === '.')) {
|
||||
|
||||
stack[stack.length - 1] += cur;
|
||||
} else if (stack[past] === '-') {
|
||||
var beforeSign = stack[stack.length - 2];
|
||||
|
||||
if (_operators2['default'][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);
|
||||
}
|
||||
|
2
equation.min.js
vendored
2
equation.min.js
vendored
File diff suppressed because one or more lines are too long
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);
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "equations",
|
||||
"version": "1.0.1",
|
||||
"version": "1.1.0",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"directories": {
|
||||
|
@ -27,6 +27,20 @@ describe('Basic math operators', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Negative Numbers', () => {
|
||||
it('should work for negative numbers after operators', () => {
|
||||
expect(M.solve('2 + -5')).to.equal(-3);
|
||||
});
|
||||
|
||||
it('should work for negative numbers after groups', () => {
|
||||
expect(M.solve('1 + (2 - 5) - 2')).to.equal(-4);
|
||||
});
|
||||
|
||||
it('should work for expressions starting with negative numbers', () => {
|
||||
expect(M.solve('-2 + 1')).to.equal(-1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Precedence', () => {
|
||||
it('Test case 1', () => {
|
||||
expect(M.solve('2+(2+1)*(1+1)^2')).to.equal(14);
|
||||
|
Loading…
Reference in New Issue
Block a user