feat: 1.3.7, solves #15

This commit is contained in:
Mahdi Dibaiee 2020-06-09 14:58:42 +01:00
parent db3ec53031
commit e30d6a44b3
11 changed files with 8391 additions and 1550 deletions

16
dist/constants.js vendored
View File

@ -1,17 +1,17 @@
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
/*
* Constats
* Keys must be UPPERCASE
* Values Can be a constant value or a function returning a value
* this function doesn't take any arguments (use case: random constats)
*/
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports['default'] = {
PI: Math.PI,
E: Math.E,
RAND: Math.random
'PI': Math.PI,
'E': Math.E,
'RAND': Math.random
};
module.exports = exports['default'];

64
dist/helpers.js vendored
View File

@ -17,17 +17,7 @@ var isNumber = function isNumber(a) {
};
exports.isNumber = isNumber;
var parseNumbers = (function (_parseNumbers) {
function parseNumbers(_x) {
return _parseNumbers.apply(this, arguments);
}
parseNumbers.toString = function () {
return _parseNumbers.toString();
};
return parseNumbers;
})(function (a) {
var parseNumbers = function parseNumbers(a) {
return a.map(function (b) {
if (isNumber(b)) {
return parseFloat(b);
@ -37,7 +27,7 @@ var parseNumbers = (function (_parseNumbers) {
}
return b;
});
});
};
exports.parseNumbers = parseNumbers;
var dive = function dive(arr, n) {
@ -49,18 +39,8 @@ var dive = function dive(arr, n) {
};
exports.dive = dive;
var deep = (function (_deep) {
function deep(_x2, _x3) {
return _deep.apply(this, arguments);
}
deep.toString = function () {
return _deep.toString();
};
return deep;
})(function (arr, n) {
var index = arguments[2] === undefined ? 0 : arguments[2];
var deep = function deep(arr, n) {
var index = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2];
if (n < 2) {
return { arr: arr, index: index };
@ -68,10 +48,10 @@ var deep = (function (_deep) {
var d = arr.reduce(function (a, b, i) {
if (Array.isArray(b)) {
var _deep2 = deep(b, n - 1, i);
var _deep = deep(b, n - 1, i);
var _arr = _deep2.arr;
var x = _deep2.index;
var _arr = _deep.arr;
var x = _deep.index;
var merged = a.concat(_arr);
index = x;
@ -81,20 +61,10 @@ var deep = (function (_deep) {
}, []);
return { arr: d, index: index };
});
exports.deep = deep;
var diveTo = (function (_diveTo) {
function diveTo(_x4, _x5, _x6) {
return _diveTo.apply(this, arguments);
}
diveTo.toString = function () {
return _diveTo.toString();
};
return diveTo;
})(function (arr, indexes, replace) {
exports.deep = deep;
var diveTo = function diveTo(arr, indexes, replace) {
var answer = [];
if (indexes.some(Array.isArray)) {
var _iteratorNormalCompletion = true;
@ -127,20 +97,10 @@ var diveTo = (function (_diveTo) {
}
return answer;
});
exports.diveTo = diveTo;
var flatten = (function (_flatten) {
function flatten(_x7) {
return _flatten.apply(this, arguments);
}
flatten.toString = function () {
return _flatten.toString();
};
return flatten;
})(function (arr) {
exports.diveTo = diveTo;
var flatten = function flatten(arr) {
if (!Array.isArray(arr) || !arr.some(Array.isArray)) {
return arr;
}
@ -148,7 +108,7 @@ var flatten = (function (_flatten) {
return arr.reduce(function (a, b) {
return a.concat(flatten(b));
}, []);
});
};
exports.flatten = flatten;
var removeSymbols = function removeSymbols(string) {

64
dist/index.js vendored
View File

@ -1,32 +1,34 @@
'use strict';
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
var _slicedToArray = function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } };
var _toConsumableArray = function (arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } };
Object.defineProperty(exports, '__esModule', {
value: true
});
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i['return']) _i['return'](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError('Invalid attempt to destructure non-iterable instance'); } }; })();
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
require('babel/polyfill');
var _ReadStream = require('./readstream');
var _readstream = require('./readstream');
var _ReadStream2 = _interopRequireWildcard(_ReadStream);
var _readstream2 = _interopRequireDefault(_readstream);
var _operators = require('./operators');
var _operators2 = _interopRequireWildcard(_operators);
var _operators2 = _interopRequireDefault(_operators);
var _constants = require('./constants');
var _constants2 = _interopRequireWildcard(_constants);
var _constants2 = _interopRequireDefault(_constants);
var _import = require('./helpers');
var _helpers = require('./helpers');
var _ = _interopRequireWildcard(_import);
var _ = _interopRequireWildcard(_helpers);
var Equation = {
/**
@ -118,17 +120,13 @@ var Equation = {
}
};
var solveStack = (function (_solveStack) {
function solveStack(_x) {
return _solveStack.apply(this, arguments);
}
var solveStack = function solveStack(_x) {
var _again = true;
solveStack.toString = function () {
return _solveStack.toString();
};
_function: while (_again) {
var stack = _x;
_again = false;
return solveStack;
})(function (stack) {
// if an operator takes an expression argument, we should not dive into it
// and solve the expression inside
var hasExpressionArgument = stack.some(function (a) {
@ -143,11 +141,15 @@ var solveStack = (function (_solveStack) {
return solveStack(group);
});
return solveStack(stack);
_x = stack;
_again = true;
hasExpressionArgument = undefined;
continue _function;
} else {
return evaluate(stack);
}
});
}
};
var PRECEDENCES = Object.keys(_operators2['default']).map(function (key) {
return _operators2['default'][key].precedence;
@ -173,7 +175,7 @@ var parseExpression = function parseExpression(expression) {
// separately
expression = expression.replace(/,/g, ')(');
var stream = new _ReadStream2['default'](expression),
var stream = new _readstream2['default'](expression),
stack = [],
record = '',
cur = undefined,
@ -304,17 +306,7 @@ var formatInfo = function formatInfo(operator) {
* @return {Array}
* Grouped expression based on precedences
*/
var sortStack = (function (_sortStack) {
function sortStack(_x2) {
return _sortStack.apply(this, arguments);
}
sortStack.toString = function () {
return _sortStack.toString();
};
return sortStack;
})(function (stack) {
var sortStack = function sortStack(stack) {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
@ -371,7 +363,7 @@ var sortStack = (function (_sortStack) {
}
return stack;
});
};
/**
* Evaluates the given math expression.

32
dist/operators.js vendored
View File

@ -1,14 +1,14 @@
'use strict';
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
Object.defineProperty(exports, '__esModule', {
value: true
});
var _Equation = require('./index');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _Equation2 = _interopRequireWildcard(_Equation);
var _index = require('./index');
var _index2 = _interopRequireDefault(_index);
/*
* Operators and Functions
@ -77,7 +77,7 @@ exports['default'] = {
'!': {
fn: function fn(a) {
var sum = 1;
for (var i = 0; i < a; ++i) {
for (var i = 1; i <= a; ++i) {
sum *= i;
}
return sum;
@ -85,61 +85,61 @@ exports['default'] = {
format: '01',
precedence: 2
},
log: {
'log': {
fn: Math.log,
format: '10',
precedence: -1
},
ln: {
'ln': {
fn: Math.log,
format: '10',
precedence: -1
},
lg: {
'lg': {
fn: function fn(a) {
return Math.log(a) / Math.log(2);
},
format: '10',
precedence: -1
},
sin: {
'sin': {
fn: Math.sin,
format: '10',
precedence: -1
},
cos: {
'cos': {
fn: Math.cos,
format: '10',
precedence: -1
},
tan: {
'tan': {
fn: Math.tan,
format: '10',
precedence: -1
},
cot: {
'cot': {
fn: Math.cot,
format: '10',
precedence: -1
},
round: {
'round': {
fn: Math.round,
format: '10',
precedence: -1
},
floor: {
'floor': {
fn: Math.floor,
format: '10',
precedence: -1
},
sigma: {
'sigma': {
fn: function fn(from, to, expression) {
var expr = expression.join('').replace(/,/g, '');
var regex = new RegExp(ITERATOR_SIGN, 'g');
var sum = 0;
for (var i = from; i <= to; i++) {
sum += _Equation2['default'].solve(expr.replace(regex, i));
sum += _index2['default'].solve(expr.replace(regex, i));
}
return sum;
},

16
dist/readstream.js vendored
View File

@ -33,23 +33,13 @@ exports['default'] = function (string) {
drain: function drain() {
return buffer.splice(0, buffer.length);
},
replace: (function (_replace) {
function replace(_x, _x2, _x3) {
return _replace.apply(this, arguments);
}
replace.toString = function () {
return _replace.toString();
};
return replace;
})(function (start, end, replace) {
replace: function replace(start, end, _replace) {
var temp = string.split('');
temp.splice(start, end, replace);
temp.splice(start, end, _replace);
string = temp.join('');
i = i - (end - start);
}),
},
go: function go(n) {
i += n;
},

View File

@ -1,46 +1,46 @@
'use strict';
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _expect = require('chai');
var _chai = require('chai');
var _M = require('../index.js');
var _indexJs = require('../index.js');
var _M2 = _interopRequireWildcard(_M);
var _indexJs2 = _interopRequireDefault(_indexJs);
describe('Equations', function () {
it('should work with one variable', function () {
var equation = _M2['default'].equation('x+2');
var equation = _indexJs2['default'].equation('x+2');
_expect.expect(equation(2)).to.equal(4);
(0, _chai.expect)(equation(2)).to.equal(4);
// Issue #10
var subtraction = _M2['default'].equation('x - 3');
_expect.expect(subtraction(10)).to.equal(7);
var subtraction = _indexJs2['default'].equation('x - 3');
(0, _chai.expect)(subtraction(10)).to.equal(7);
});
it('should work with multiple variables', function () {
var equation = _M2['default'].equation('x+y');
_expect.expect(equation(2, 4)).to.equal(6);
var equation = _indexJs2['default'].equation('x+y');
(0, _chai.expect)(equation(2, 4)).to.equal(6);
});
it('should work with multiple instances of the same variable', function () {
var equation = _M2['default'].equation('x*x');
_expect.expect(equation(4)).to.equal(16);
var equation = _indexJs2['default'].equation('x*x');
(0, _chai.expect)(equation(4)).to.equal(16);
});
it('should only accept lowercase letters', function () {
var equation = _M2['default'].equation('X+2');
_expect.expect(equation).to['throw']();
var equation = _indexJs2['default'].equation('X+2');
(0, _chai.expect)(equation).to['throw']();
});
it('should work with NumVariable expressions like 2x', function () {
var equation = _M2['default'].equation('2x + 6y');
_expect.expect(equation(4, 3)).to.equal(8 + 18);
var equation = _indexJs2['default'].equation('2x + 6y');
(0, _chai.expect)(equation(4, 3)).to.equal(8 + 18);
});
it('Test case', function () {
var equation = _M2['default'].equation('2+x*(y+4)+z^2');
_expect.expect(equation(2, 4, 3)).to.equal(27);
var equation = _indexJs2['default'].equation('2+x*(y+4)+z^2');
(0, _chai.expect)(equation(2, 4, 3)).to.equal(27);
});
});

60
dist/tests/solve.js vendored
View File

@ -1,116 +1,116 @@
'use strict';
var _interopRequireWildcard = function (obj) { return obj && obj.__esModule ? obj : { 'default': obj }; };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _expect = require('chai');
var _chai = require('chai');
var _M = require('../index.js');
var _indexJs = require('../index.js');
var _M2 = _interopRequireWildcard(_M);
var _indexJs2 = _interopRequireDefault(_indexJs);
describe('Basic math operators', function () {
it('should work for add +', function () {
_expect.expect(_M2['default'].solve('2+2')).to.equal(4);
(0, _chai.expect)(_indexJs2['default'].solve('2+2')).to.equal(4);
});
it('should work for minus -', function () {
_expect.expect(_M2['default'].solve('15-3')).to.equal(12);
(0, _chai.expect)(_indexJs2['default'].solve('15-3')).to.equal(12);
});
it('should work for divison /', function () {
_expect.expect(_M2['default'].solve('20/2')).to.equal(10);
(0, _chai.expect)(_indexJs2['default'].solve('20/2')).to.equal(10);
});
it('should work for multiplication *', function () {
_expect.expect(_M2['default'].solve('6*3')).to.equal(18);
(0, _chai.expect)(_indexJs2['default'].solve('6*3')).to.equal(18);
});
it('should work for power ^', function () {
_expect.expect(_M2['default'].solve('5^2')).to.equal(25);
(0, _chai.expect)(_indexJs2['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);
(0, _chai.expect)(_indexJs2['default'].solve('12+15')).to.equal(27);
});
it('should deal with floating precision of javascript - #5', function () {
_expect.expect(_M2['default'].solve('0.2 + 0.1')).to.equal(0.3);
_expect.expect(_M2['default'].solve('0.2 + 0.4')).to.equal(0.6);
_expect.expect(_M2['default'].solve('round(floor(1.23456789/0.2)) * 0.2')).to.equal(1.2);
_expect.expect(_M2['default'].solve('1.23456789 - (1.23456789 % 0.2)')).to.equal(1.2);
(0, _chai.expect)(_indexJs2['default'].solve('0.2 + 0.1')).to.equal(0.3);
(0, _chai.expect)(_indexJs2['default'].solve('0.2 + 0.4')).to.equal(0.6);
(0, _chai.expect)(_indexJs2['default'].solve('round(floor(1.23456789/0.2)) * 0.2')).to.equal(1.2);
(0, _chai.expect)(_indexJs2['default'].solve('1.23456789 - (1.23456789 % 0.2)')).to.equal(1.2);
});
});
describe('Negative Numbers', function () {
it('should work for negative numbers after operators', function () {
_expect.expect(_M2['default'].solve('2 + -5')).to.equal(-3);
(0, _chai.expect)(_indexJs2['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);
(0, _chai.expect)(_indexJs2['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);
(0, _chai.expect)(_indexJs2['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);
(0, _chai.expect)(_indexJs2['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);
(0, _chai.expect)(_indexJs2['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);
(0, _chai.expect)(_indexJs2['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);
(0, _chai.expect)(_indexJs2['default'].solve('(2+2)^2+(5+1)*4+(2+(4/2)/2)')).to.equal(16 + 24 + 3);
});
});
describe('Functions', function () {
it('should work with parantheses', function () {
_expect.expect(_M2['default'].solve('lg(4) * 5')).to.equal(10);
(0, _chai.expect)(_indexJs2['default'].solve('lg(4) * 5')).to.equal(10);
});
it('should work without parantheses', function () {
_expect.expect(_M2['default'].solve('lg4 * 5')).to.equal(10);
(0, _chai.expect)(_indexJs2['default'].solve('lg4 * 5')).to.equal(10);
});
it('should work for wrapped functions', function () {
_expect.expect(_M2['default'].solve('(lg4)*2')).to.equal(4);
(0, _chai.expect)(_indexJs2['default'].solve('(lg4)*2')).to.equal(4);
});
});
describe('Constats', function () {
it('should work for constant values', function () {
_expect.expect(_M2['default'].solve('sin(PI/2)')).to.equal(1);
(0, _chai.expect)(_indexJs2['default'].solve('sin(PI/2)')).to.equal(1);
});
it('should work for functions as constants - retry on fail', function () {
_expect.expect(_M2['default'].solve('RAND')).to.not.equal(_M2['default'].solve('RAND'));
(0, _chai.expect)(_indexJs2['default'].solve('RAND')).to.not.equal(_indexJs2['default'].solve('RAND'));
});
});
describe('Sigma', function () {
it('should work with simple expressions', function () {
_expect.expect(_M2['default'].solve('sigma(0, 5, @)')).to.equal(15);
(0, _chai.expect)(_indexJs2['default'].solve('sigma(0, 5, @)')).to.equal(15);
});
it('should work with more complex expressions', function () {
_expect.expect(_M2['default'].solve('sigma(0, 2, 2@+5)')).to.equal(21);
(0, _chai.expect)(_indexJs2['default'].solve('sigma(0, 2, 2@+5)')).to.equal(21);
});
it('should work without an iterator sign', function () {
_expect.expect(_M2['default'].solve('sigma(0, 2, 5*2)')).to.equal(30);
(0, _chai.expect)(_indexJs2['default'].solve('sigma(0, 2, 5*2)')).to.equal(30);
});
it('should work with negative start / end points', function () {
_expect.expect(_M2['default'].solve('sigma(-5, -2, @)')).to.equal(-14);
(0, _chai.expect)(_indexJs2['default'].solve('sigma(-5, -2, @)')).to.equal(-14);
});
});

File diff suppressed because it is too large Load Diff

7
equation.min.js vendored

File diff suppressed because one or more lines are too long

6544
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,14 @@
{
"name": "equations",
"version": "1.3.6",
"version": "1.3.7",
"description": "",
"main": "dist/index.js",
"directories": {
"test": "dist/tests"
},
"scripts": {
"test": "grunt test"
"test": "grunt test",
"build": "grunt build"
},
"keywords": [
"Math",