Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 26dbb30
Showing
9 changed files
with
375 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.idea | ||
.c9 | ||
_site | ||
coverage | ||
*swp | ||
node_modules | ||
*.log | ||
dist | ||
|
||
# for now, we just exclude package-lock.json though it's not best practice | ||
# until it's more stable | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## not yet released, version 1.0.0 | ||
|
||
- Inital version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2017 Jos de Jong | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# mathjs-expression-parser | ||
|
||
Just want to use the expression parser of `mathjs` for simple, numeric calculations? Here you go... | ||
|
||
This custom build of `mathjs` contains just the expression parser and basic arithmetic functions for numbers. The expression parser contains full functionality for parsing, compiling, evaluating, and transforming expression trees. Support for Matrices, BigNumbers, Fractions, Complex numbers, Units, and all functions and constants that come with mathjs are excluded. | ||
|
||
The size of `mathjs-expression-parser` is `28 KiB` when minified and gzipped (about a quarter of the size of `mathjs`). | ||
|
||
|
||
## Install | ||
|
||
``` | ||
npm install mathjs-expression-parser | ||
``` | ||
|
||
|
||
## Use | ||
|
||
### node.js | ||
|
||
```js | ||
var math = require('mathjs-expression-parser') | ||
|
||
var expr = '2.4 + sqrt(x)'; | ||
evaluate('result', math.eval(expr, {x : 16})); // 6.4 | ||
``` | ||
|
||
### browser | ||
|
||
```html | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>mathjs-expression-parser | basic usage</title> | ||
<script src="../dist/mathjs-expression-parser.js"></script> | ||
</head> | ||
<body> | ||
|
||
<script> | ||
var expr = '2.4 + sqrt(x)'; | ||
evaluate('result', math.eval(expr, {x : 16})); // 6.4 | ||
</script> | ||
|
||
</body> | ||
</html> | ||
``` | ||
|
||
See the `examples` folder for more examples | ||
|
||
|
||
## Test | ||
|
||
To run unit tests, install dependencies, then run: | ||
|
||
``` | ||
npm test | ||
``` | ||
|
||
|
||
## Build | ||
|
||
To build the bundled and minified library, install dependencies, then run: | ||
|
||
``` | ||
npm run build | ||
``` | ||
|
||
|
||
## Publish | ||
|
||
- Update version number in `package.json` | ||
- Describe changes in `CHANGELOG.md` | ||
- Commit changes to git | ||
- Publish `npm publish` | ||
- Add git tag for current version | ||
|
||
|
||
## Included functionality | ||
|
||
Category | Functions / operators | ||
----------- | ----- | ||
Core | `import`, `config` | ||
Expression | `parse`, `compile`, `eval` | ||
Operators | `+`, `-`, `*`, `/`, `%`, `mod`, `|`, `^`, `&`, `~`, `<<`, `>>`, `>>>`, `and`, `or`, `xor`, `not`, `==`, `!=`, `<`, `>`, `<=`, `>=` | ||
Arithmetic | `abs`, `exp`, `log`, `sqrt`, `ceil`, `floor`, `random`, `round` | ||
Trigonometry| `tan`, `sin`, `cos`, `acos`, `asin`, `atan`, `atan2` | ||
Statistics | `max`, `min` | ||
Constants | `pi`, `e`, `true`, `false`, `null` | ||
String | `format` | ||
Objects | Creating objects and accessing properties | ||
|
||
Note: on new browsers there are probably more functions available, since all functions and constants from `Math` are imported. | ||
|
||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
var math = require('../index') | ||
|
||
function evaluate (expr, scope) { | ||
console.log(expr, '=', math.eval(expr, scope || {})); | ||
} | ||
|
||
evaluate('2.4 + 5'); | ||
evaluate('sqrt(16)'); | ||
evaluate('sin(pi / 4) ^ 2'); | ||
evaluate('2x', {x: 4}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>mathjs-expression-parser | basic usage</title> | ||
<script src="../dist/mathjs-expression-parser.js"></script> | ||
</head> | ||
<body> | ||
|
||
<script> | ||
function evaluate (expr, scope) { | ||
document.write(expr + ' = ' + math.eval(expr, scope || {}) + '<br>'); | ||
} | ||
|
||
evaluate('2.4 + 5'); | ||
evaluate('sqrt(16)'); | ||
evaluate('sin(pi / 4) ^ 2'); | ||
evaluate('2x', {x: 4}); | ||
</script> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Load the math.js core | ||
var core = require('mathjs/core'); | ||
|
||
// Create a new, empty math.js instance | ||
// It will only contain methods `import` and `config` | ||
var math = core.create(); | ||
|
||
math.import(require('mathjs/lib/expression/function/parse')); | ||
math.import(require('mathjs/lib/expression/function/compile')); | ||
math.import(require('mathjs/lib/expression/function/eval')); | ||
|
||
math.import(require('mathjs/lib/function/string/format')); | ||
|
||
// create simple functions for all operators | ||
math.import({ | ||
// arithmetic | ||
add: function (a, b) { return a + b }, | ||
subtract: function (a, b) { return a - b }, | ||
multiply: function (a, b) { return a * b }, | ||
divide: function (a, b) { return a / b }, | ||
mod: function (a, b) { return a % b }, | ||
unaryPlus: function (a) { return a }, | ||
unaryMinus: function (a) { return -a }, | ||
|
||
// bitwise | ||
bitOr: function (a, b) { return a | b }, | ||
bitXor: function (a, b) { return a ^ b }, | ||
bitAnd: function (a, b) { return a & b }, | ||
bitNot: function (a) { return ~a }, | ||
leftShift: function (a, b) { return a << b }, | ||
rightArithShift: function (a, b) { return a >> b }, | ||
rightLogShift: function (a, b) { return a >>> b }, | ||
|
||
// logical | ||
or: function (a, b) { return !!(a || b) }, | ||
xor: function (a, b) { return !!a !== !!b }, | ||
and: function (a, b) { return !!(a && b) }, | ||
not: function (a) { return !a }, | ||
|
||
// relational | ||
equal: function (a, b) { return a == b }, | ||
unequal: function (a, b) { return a != b }, | ||
smaller: function (a, b) { return a < b }, | ||
larger: function (a, b) { return a > b }, | ||
smallerEq: function (a, b) { return a <= b }, | ||
largerEq: function (a, b) { return a >= b }, | ||
|
||
// matrix | ||
// matrix: function (a) { return a }, | ||
matrix: function () { | ||
throw new Error('Matrices not supported') | ||
}, | ||
index: function () { | ||
// TODO: create a simple index function | ||
throw new Error('Matrix indexes not supported') | ||
}, | ||
|
||
// add pi and e as lowercase | ||
pi: Math.PI, | ||
e: Math.E, | ||
'true': true, | ||
'false': false, | ||
'null': null | ||
}) | ||
|
||
// import everything from Math (like trigonometric functions) | ||
var allFromMath = {}; | ||
Object.getOwnPropertyNames(Math).forEach(function (name) { | ||
allFromMath[name] = Math[name]; | ||
}); | ||
math.import(allFromMath); | ||
|
||
module.exports = math; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "mathjs-expression-parser", | ||
"version": "1.0.0", | ||
"description": "Just the expression parser of mathjs", | ||
"main": "index.js", | ||
"directories": { | ||
"dist": "dist", | ||
"example": "example", | ||
"test": "test" | ||
}, | ||
"homepage": "http://mathjs.org", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/josdejong/mathjs-expression-parser.git" | ||
}, | ||
"scripts": { | ||
"test": "mocha", | ||
"mkdist": "mkdirp dist", | ||
"bundle": "browserify ./index.js -s math -o dist/mathjs-expression-parser.js", | ||
"minify": "uglifyjs --compress --mangle --source-map -o dist/mathjs-expression-parser.min.js -- dist/mathjs-expression-parser.js", | ||
"build": "npm run mkdist && npm run bundle && npm run minify", | ||
"prepublishOnly": "npm run test && npm run build" | ||
}, | ||
"keywords": [ | ||
"mathjs", | ||
"expression", | ||
"parser" | ||
], | ||
"author": "Jos de Jong", | ||
"license": "MIT", | ||
"dependencies": { | ||
"mathjs": "3.*" | ||
}, | ||
"devDependencies": { | ||
"browserify": "14.4.0", | ||
"mkdirp": "0.5.1", | ||
"mocha": "3.4.2", | ||
"uglify-js": "3.0.19", | ||
"webpack": "3.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
var assert = assert = require('assert'); | ||
var math = require ('../index'); | ||
|
||
describe('mathjs-expression-parser', function() { | ||
|
||
it('parse', function() { | ||
var scope = {a: 4}; | ||
var tree = math.parse('2a'); | ||
assert.equal(tree.compile().eval(scope), 8); | ||
}); | ||
|
||
it('compile', function() { | ||
var scope = {a: 4}; | ||
var expr = math.compile('2a'); | ||
assert.equal(expr.eval(scope), 8); | ||
}); | ||
|
||
it('eval', function() { | ||
var scope = {a: 4}; | ||
assert.equal(math.eval('a', scope), 4); | ||
}); | ||
|
||
it('operators', function() { | ||
assert.equal(math.eval('2 + 3'), 5); | ||
assert.equal(math.eval('5 - 2'), 3); | ||
assert.equal(math.eval('2 * 3'), 6); | ||
assert.equal(math.eval('6 / 2'), 3); | ||
assert.equal(math.eval('+2'), 2); | ||
assert.equal(math.eval('-2'), -2); | ||
assert.equal(math.eval('8 % 3'), 2); | ||
assert.equal(math.eval('8 mod 3'), 2); | ||
|
||
assert.equal(math.eval('5 | 2'), 7); | ||
assert.equal(math.eval('6 & 2'), 2); | ||
assert.equal(math.eval('6 ^| 2'), 4); | ||
assert.equal(math.eval('~6'), -7); | ||
assert.equal(math.eval('8 << 2'), 32); | ||
assert.equal(math.eval('8 >> 2'), 2); | ||
assert.equal(math.eval('8 >>> 2'), 2); | ||
|
||
assert.equal(math.eval('true and false'), false); | ||
assert.equal(math.eval('true or false'), true); | ||
assert.equal(math.eval('true xor false'), true); | ||
assert.equal(math.eval('not true'), false); | ||
|
||
assert.equal(math.eval('2 == 2'), true); | ||
assert.equal(math.eval('2 != 2'), false); | ||
assert.equal(math.eval('2 < 2'), false); | ||
assert.equal(math.eval('2 > 2'), false); | ||
assert.equal(math.eval('2 <= 2'), true); | ||
assert.equal(math.eval('2 >= 2'), true); | ||
}); | ||
|
||
it('arithmetic', function() { | ||
assert.equal(math.eval('abs(-4)'), 4); | ||
assert.equal(math.eval('exp(1)'), Math.E); | ||
assert.equal(math.eval('log(e)'), 1); | ||
assert.equal(math.eval('sqrt(16)'), 4); | ||
assert.equal(math.eval('ceil(3.2)'), 4); | ||
assert.equal(math.eval('floor(3.8)'), 3); | ||
assert.equal(math.eval('round(3.8)'), 4); | ||
assert.equal(typeof math.eval('random()'), 'number'); | ||
}); | ||
|
||
it('trigonometry', function() { | ||
assert.equal(math.eval('tan(2)'), Math.tan(2)); | ||
assert.equal(math.eval('sin(2)'), Math.sin(2)); | ||
assert.equal(math.eval('cos(2)'), Math.cos(2)); | ||
assert.equal(math.eval('acos(0.5)'), Math.acos(0.5)); | ||
assert.equal(math.eval('asin(0.5)'), Math.asin(0.5)); | ||
assert.equal(math.eval('atan(0.5)'), Math.atan(0.5)); | ||
assert.equal(math.eval('atan2(2,2)'), Math.atan2(2,2)); | ||
}); | ||
|
||
it('statistics', function() { | ||
assert.equal(math.eval('max(1,4,2)'), 4); | ||
assert.equal(math.eval('min(4,1,2)'), 1); | ||
}); | ||
|
||
it('variables', function() { | ||
var scope = {a: 2}; | ||
assert.deepEqual(math.eval('a', scope), 2); | ||
assert.deepEqual(math.eval('b=4', scope), 4); | ||
assert.deepEqual(scope, {a: 2, b: 4}) | ||
}); | ||
|
||
it('object', function() { | ||
assert.deepEqual(math.eval('{a: 2, b: 4}'), {a: 2, b: 4}); | ||
assert.deepEqual(math.eval('obj.a', {obj: {a: 2, b: 4}}), 2); | ||
|
||
var scope = {obj: {a: 2}} | ||
assert.deepEqual(math.eval('obj.a=4', scope), 4); | ||
assert.deepEqual(scope, {obj: {a: 4}}) | ||
}); | ||
|
||
it('constants', function() { | ||
assert.equal(math.eval('pi'), Math.PI); | ||
assert.equal(math.eval('e'), Math.E); | ||
assert.equal(math.eval('true'), true); | ||
assert.equal(math.eval('false'), false); | ||
assert.equal(math.eval('null'), null); | ||
}); | ||
|
||
it('format', function() { | ||
assert.equal(math.format(math.pi, 3), '3.14'); | ||
}); | ||
}); |