Fix #10, add test
This commit is contained in:
parent
a4b45ded0a
commit
9274694d98
6
dist/index.js
vendored
6
dist/index.js
vendored
@ -219,7 +219,7 @@ var parseExpression = function parseExpression(expression) {
|
|||||||
stack.push('-' + cur);
|
stack.push('-' + cur);
|
||||||
|
|
||||||
// 2 - 5 is also OK, pass
|
// 2 - 5 is also OK, pass
|
||||||
} else if (_.isNumber(beforeSign)) {
|
} else if (_.isNumber(beforeSign) || isVariable(beforeSign)) {
|
||||||
stack.push(cur);
|
stack.push(cur);
|
||||||
} else {
|
} else {
|
||||||
stack[past] += cur;
|
stack[past] += cur;
|
||||||
@ -478,8 +478,10 @@ var fixFloat = function fixFloat(number) {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
* true if variable, else false
|
* true if variable, else false
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
var SPECIALS = '()[]{}'.split('');
|
||||||
var isVariable = function isVariable(a) {
|
var isVariable = function isVariable(a) {
|
||||||
return typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase();
|
return typeof a === 'string' && !_.isNumber(a) && !_operators2['default'][a] && a === a.toLowerCase() && SPECIALS.indexOf(a) === -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.isVariable = isVariable;
|
exports.isVariable = isVariable;
|
||||||
|
3
dist/tests/equation.js
vendored
3
dist/tests/equation.js
vendored
@ -13,6 +13,9 @@ describe('Equations', function () {
|
|||||||
var equation = _M2['default'].equation('x+2');
|
var equation = _M2['default'].equation('x+2');
|
||||||
|
|
||||||
_expect.expect(equation(2)).to.equal(4);
|
_expect.expect(equation(2)).to.equal(4);
|
||||||
|
|
||||||
|
var subtraction = _M2['default'].equation('x - 3');
|
||||||
|
_expect.expect(subtraction(10)).to.equal(7);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with multiple variables', function () {
|
it('should work with multiple variables', function () {
|
||||||
|
@ -182,7 +182,7 @@ const parseExpression = expression => {
|
|||||||
stack.push(`-${cur}`);
|
stack.push(`-${cur}`);
|
||||||
|
|
||||||
// 2 - 5 is also OK, pass
|
// 2 - 5 is also OK, pass
|
||||||
} else if (_.isNumber(beforeSign)) {
|
} else if (_.isNumber(beforeSign) || isVariable(beforeSign)) {
|
||||||
stack.push(cur);
|
stack.push(cur);
|
||||||
} else {
|
} else {
|
||||||
stack[past] += cur;
|
stack[past] += cur;
|
||||||
@ -379,9 +379,11 @@ const fixFloat = number => {
|
|||||||
* @return {Boolean}
|
* @return {Boolean}
|
||||||
* true if variable, else false
|
* true if variable, else false
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const SPECIALS = '()[]{}'.split('');
|
||||||
export const isVariable = a => {
|
export const isVariable = a => {
|
||||||
return typeof a === 'string' && !_.isNumber(a) &&
|
return typeof a === 'string' && !_.isNumber(a) &&
|
||||||
!operators[a] && a === a.toLowerCase();
|
!operators[a] && a === a.toLowerCase() && SPECIALS.indexOf(a) === -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Equation;
|
export default Equation;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "equations",
|
"name": "equations",
|
||||||
"version": "1.3.2",
|
"version": "1.3.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"directories": {
|
"directories": {
|
||||||
|
@ -6,6 +6,10 @@ describe('Equations', () => {
|
|||||||
let equation = M.equation('x+2');
|
let equation = M.equation('x+2');
|
||||||
|
|
||||||
expect(equation(2)).to.equal(4);
|
expect(equation(2)).to.equal(4);
|
||||||
|
|
||||||
|
// Issue #10
|
||||||
|
let subtraction = M.equation('x - 3');
|
||||||
|
expect(subtraction(10)).to.equal(7);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should work with multiple variables', () => {
|
it('should work with multiple variables', () => {
|
||||||
|
Loading…
Reference in New Issue
Block a user