built-in-math-eval 0.2.3 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -7
- package/lib/adapter.js +38 -9
- package/lib/eval.js +3 -3
- package/package.json +12 -15
package/README.md
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
[![Build Status][travis-image]][travis-url]
|
|
4
4
|
[![NPM][npm-image]][npm-url]
|
|
5
5
|
[![Coverage Status][coveralls-image]][coveralls-url]
|
|
6
|
-
[]()
|
|
7
6
|
|
|
8
7
|
[](https://github.com/feross/standard)
|
|
9
8
|
|
|
@@ -24,7 +23,7 @@
|
|
|
24
23
|
|
|
25
24
|
## Description
|
|
26
25
|
|
|
27
|
-
This module evaluates the generated code from [math-codegen](https://github.com/
|
|
26
|
+
This module evaluates the generated code from [math-codegen](https://github.com/mauriciopoppe/math-codegen)
|
|
28
27
|
for the built in `Math` namespace providing the necessary adapter methods
|
|
29
28
|
|
|
30
29
|
## Installation
|
|
@@ -59,7 +58,7 @@ all variables are casted to `Number`
|
|
|
59
58
|
|
|
60
59
|
## Examples
|
|
61
60
|
|
|
62
|
-
Also have a look at [test/index.js](https://github.com/
|
|
61
|
+
Also have a look at [test/index.js](https://github.com/mauriciopoppe/built-in-math-eval/blob/master/test/index.js)
|
|
63
62
|
|
|
64
63
|
```javascript
|
|
65
64
|
var compile = require('built-in-math-eval');
|
|
@@ -87,7 +86,7 @@ compile('PI').eval()
|
|
|
87
86
|
|
|
88
87
|
[npm-image]: https://img.shields.io/npm/v/built-in-math-eval.svg?style=flat
|
|
89
88
|
[npm-url]: https://npmjs.org/package/built-in-math-eval
|
|
90
|
-
[travis-image]: https://travis-ci.org/
|
|
91
|
-
[travis-url]: https://travis-ci.org/
|
|
92
|
-
[coveralls-image]: https://coveralls.io/repos/
|
|
93
|
-
[coveralls-url]: https://coveralls.io/r/
|
|
89
|
+
[travis-image]: https://travis-ci.org/mauriciopoppe/built-in-math-eval.svg?branch=master
|
|
90
|
+
[travis-url]: https://travis-ci.org/mauriciopoppe/built-in-math-eval
|
|
91
|
+
[coveralls-image]: https://coveralls.io/repos/mauriciopoppe/built-in-math-eval/badge.svg
|
|
92
|
+
[coveralls-url]: https://coveralls.io/r/mauriciopoppe/built-in-math-eval
|
package/lib/adapter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
module.exports = function () {
|
|
3
|
-
|
|
3
|
+
const math = Object.create(Math)
|
|
4
4
|
|
|
5
5
|
math.factory = function (a) {
|
|
6
6
|
if (typeof a !== 'number') {
|
|
@@ -25,13 +25,42 @@ module.exports = function () {
|
|
|
25
25
|
return a % b
|
|
26
26
|
}
|
|
27
27
|
math.factorial = function (a) {
|
|
28
|
-
|
|
29
|
-
for (
|
|
28
|
+
let res = 1
|
|
29
|
+
for (let i = 2; i <= a; i += 1) {
|
|
30
30
|
res *= i
|
|
31
31
|
}
|
|
32
32
|
return res
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
// taken from https://github.com/josdejong/mathjs/blob/master/lib/function/arithmetic/nthRoot.js
|
|
36
|
+
math.nthRoot = function (a, root) {
|
|
37
|
+
const inv = root < 0
|
|
38
|
+
if (inv) {
|
|
39
|
+
root = -root
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (root === 0) {
|
|
43
|
+
throw new Error('Root must be non-zero')
|
|
44
|
+
}
|
|
45
|
+
if (a < 0 && (Math.abs(root) % 2 !== 1)) {
|
|
46
|
+
throw new Error('Root must be odd when a is negative.')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// edge cases zero and infinity
|
|
50
|
+
if (a === 0) {
|
|
51
|
+
return 0
|
|
52
|
+
}
|
|
53
|
+
if (!isFinite(a)) {
|
|
54
|
+
return inv ? 0 : a
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
let x = Math.pow(Math.abs(a), 1 / root)
|
|
58
|
+
// If a < 0, we require that root is an odd integer,
|
|
59
|
+
// so (-1) ^ (1/root) = -1
|
|
60
|
+
x = a < 0 ? -x : x
|
|
61
|
+
return inv ? 1 / x : x
|
|
62
|
+
}
|
|
63
|
+
|
|
35
64
|
// logical
|
|
36
65
|
math.logicalOR = function (a, b) {
|
|
37
66
|
return a || b
|
|
@@ -39,7 +68,7 @@ module.exports = function () {
|
|
|
39
68
|
math.logicalXOR = function (a, b) {
|
|
40
69
|
/* eslint-disable */
|
|
41
70
|
return a != b
|
|
42
|
-
/* eslint-enable*/
|
|
71
|
+
/* eslint-enable */
|
|
43
72
|
}
|
|
44
73
|
math.logicalAND = function (a, b) {
|
|
45
74
|
return a && b
|
|
@@ -49,17 +78,17 @@ module.exports = function () {
|
|
|
49
78
|
math.bitwiseOR = function (a, b) {
|
|
50
79
|
/* eslint-disable */
|
|
51
80
|
return a | b
|
|
52
|
-
/* eslint-enable*/
|
|
81
|
+
/* eslint-enable */
|
|
53
82
|
}
|
|
54
83
|
math.bitwiseXOR = function (a, b) {
|
|
55
84
|
/* eslint-disable */
|
|
56
85
|
return a ^ b
|
|
57
|
-
/* eslint-enable*/
|
|
86
|
+
/* eslint-enable */
|
|
58
87
|
}
|
|
59
88
|
math.bitwiseAND = function (a, b) {
|
|
60
89
|
/* eslint-disable */
|
|
61
90
|
return a & b
|
|
62
|
-
/* eslint-enable*/
|
|
91
|
+
/* eslint-enable */
|
|
63
92
|
}
|
|
64
93
|
|
|
65
94
|
// relational
|
|
@@ -78,7 +107,7 @@ module.exports = function () {
|
|
|
78
107
|
math.equal = function (a, b) {
|
|
79
108
|
/* eslint-disable */
|
|
80
109
|
return a == b
|
|
81
|
-
/* eslint-enable*/
|
|
110
|
+
/* eslint-enable */
|
|
82
111
|
}
|
|
83
112
|
math.strictlyEqual = function (a, b) {
|
|
84
113
|
return a === b
|
|
@@ -86,7 +115,7 @@ module.exports = function () {
|
|
|
86
115
|
math.notEqual = function (a, b) {
|
|
87
116
|
/* eslint-disable */
|
|
88
117
|
return a != b
|
|
89
|
-
/* eslint-enable*/
|
|
118
|
+
/* eslint-enable */
|
|
90
119
|
}
|
|
91
120
|
math.strictlyNotEqual = function (a, b) {
|
|
92
121
|
return a !== b
|
package/lib/eval.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
const CodeGenerator = require('math-codegen')
|
|
4
|
+
const math = require('./adapter')()
|
|
5
5
|
|
|
6
6
|
function processScope (scope) {
|
|
7
7
|
Object.keys(scope).forEach(function (k) {
|
|
8
|
-
|
|
8
|
+
const value = scope[k]
|
|
9
9
|
scope[k] = math.factory(value)
|
|
10
10
|
})
|
|
11
11
|
}
|
package/package.json
CHANGED
|
@@ -1,40 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "built-in-math-eval",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Evaluate mathematical expression with the built-in math object",
|
|
5
|
-
"bugs": "https://github.com/
|
|
5
|
+
"bugs": "https://github.com/mauriciopoppe/built-in-math-eval/issues",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "index.js",
|
|
8
|
-
"author": "Mauricio Poppe
|
|
8
|
+
"author": "Mauricio Poppe (https://mauriciopoppe.com)",
|
|
9
9
|
"files": [
|
|
10
10
|
"index.js",
|
|
11
11
|
"lib",
|
|
12
|
+
"package-lock.json",
|
|
12
13
|
"LICENSE"
|
|
13
14
|
],
|
|
14
15
|
"repository": {
|
|
15
16
|
"type": "git",
|
|
16
|
-
"url": "https://github.com/
|
|
17
|
+
"url": "https://github.com/mauriciopoppe/built-in-math-eval"
|
|
17
18
|
},
|
|
18
19
|
"keywords": [
|
|
19
20
|
"built-in-math-eval"
|
|
20
21
|
],
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"math-codegen": "^0.
|
|
23
|
+
"math-codegen": "^0.4.1"
|
|
23
24
|
},
|
|
24
25
|
"devDependencies": {
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"mocha": "^2.2.1",
|
|
30
|
-
"mocha-lcov-reporter": "^0.0.2",
|
|
31
|
-
"nodemon": "^1.3.7",
|
|
32
|
-
"standard": "^4.5.4"
|
|
26
|
+
"doctoc": "^2.2.0",
|
|
27
|
+
"jest": "^29.0.2",
|
|
28
|
+
"nodemon": "^2.0.19",
|
|
29
|
+
"standard": "^17.0.0"
|
|
33
30
|
},
|
|
34
31
|
"scripts": {
|
|
35
|
-
"
|
|
32
|
+
"coverage": "jest --coverage",
|
|
36
33
|
"lint": "standard",
|
|
37
|
-
"test": "
|
|
34
|
+
"test": "jest",
|
|
38
35
|
"test:watch": "nodemon --watch lib --watch test --watch index.js --exec 'npm test'",
|
|
39
36
|
"start": "npm run test:watch",
|
|
40
37
|
"toc": "doctoc ."
|