diff --git a/docs/custom_bundling.md b/docs/custom_bundling.md index 9a23d2e13..202729dcb 100644 --- a/docs/custom_bundling.md +++ b/docs/custom_bundling.md @@ -8,7 +8,7 @@ Math.js is a large library containing many data types and functions. It is well possible that you only need a small portion of the library. Math.js allows for picking just the functions and data types you need. This gives faster load times and smaller browser bundles. Math.js uses -ES6 modules, and creating small bundles using tree-shaking works out of +ES modules, and creating small bundles using tree-shaking works out of the box when using Webpack for example. This page describes: diff --git a/docs/expressions/security.md b/docs/expressions/security.md index 9234aa11c..a312566a7 100644 --- a/docs/expressions/security.md +++ b/docs/expressions/security.md @@ -15,7 +15,7 @@ the code server side. A user could try to inject malicious JavaScript code via the expression parser. The expression parser of mathjs offers a sandboxed environment to execute expressions which should make this impossible. It's possible -though that there are unknown security vulnerabilties, so it's important +though that there are unknown security vulnerabilities, so it's important to be careful, especially when allowing server side execution of arbitrary expressions. diff --git a/docs/getting_started.md b/docs/getting_started.md index e95ef4f57..e12c51057 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -25,7 +25,7 @@ and instantiated. When creating an instance, one can optionally provide configuration options as described in [Configuration](core/configuration.html). -
| - - Development (version 6.0.3) + + Development (version 6.1.0) | - 1827 kB, uncompressed with comments + 1829 kB, uncompressed with comments |
| - - Production (version 6.0.3) + + Production (version 6.1.0) |
diff --git a/examples/advanced/custom_argument_parsing.js b/examples/advanced/custom_argument_parsing.js
index 752eb7f98..7b66e85f9 100644
--- a/examples/advanced/custom_argument_parsing.js
+++ b/examples/advanced/custom_argument_parsing.js
@@ -93,6 +93,6 @@ console.log(math.integrate(f, 0, 1)) // outputs 0.6667254718034714
console.log(math.evaluate('integrate(x^0.5, x, 0, 1)')) // outputs 0.6667254718034714
// use the function via the expression parser (2)
-let scope = {}
+const scope = {}
math.evaluate('f(x) = 2 * x', scope)
console.log(math.evaluate('integrate(f(x), x, 0, 2)', scope)) // outputs 4.000000000000003
diff --git a/examples/advanced/custom_argument_parsing.js.md b/examples/advanced/custom_argument_parsing.js.md
index 5f9281663..0f05a5c09 100644
--- a/examples/advanced/custom_argument_parsing.js.md
+++ b/examples/advanced/custom_argument_parsing.js.md
@@ -102,7 +102,7 @@ console.log(math.integrate(f, 0, 1)) // outputs 0.6667254718034714
console.log(math.evaluate('integrate(x^0.5, x, 0, 1)')) // outputs 0.6667254718034714
// use the function via the expression parser (2)
-let scope = {}
+const scope = {}
math.evaluate('f(x) = 2 * x', scope)
console.log(math.evaluate('integrate(f(x), x, 0, 2)', scope)) // outputs 4.000000000000003
diff --git a/examples/advanced/custom_relational_functions.js b/examples/advanced/custom_relational_functions.js
new file mode 100644
index 000000000..fbcb2ab96
--- /dev/null
+++ b/examples/advanced/custom_relational_functions.js
@@ -0,0 +1,62 @@
+const { create, all, factory } = require('../..')
+
+// First let's see what the default behavior is:
+// strings are compared by their numerical value
+console.log('default (compare string by their numerical value)')
+const { evaluate } = create(all)
+evaluateAndLog(evaluate, '2 < 10') // true
+evaluateAndLog(evaluate, '"2" < "10"') // true
+evaluateAndLog(evaluate, '"a" == "b"') // Error: Cannot convert "a" to a number
+evaluateAndLog(evaluate, '"a" == "a"') // Error: Cannot convert "a" to a number
+console.log('')
+
+// Suppose we want different behavior for string comparisons. To achieve
+// this we can replace the factory functions for all relational functions
+// with our own. In this simple example we use the JavaScript implementation.
+console.log('custom (compare strings lexically)')
+
+const allWithCustomFunctions = {
+ ...all,
+
+ createEqual: factory('equal', [], () => function equal (a, b) {
+ return a === b
+ }),
+
+ createUnequal: factory('unequal', [], () => function unequal (a, b) {
+ return a !== b
+ }),
+
+ createSmaller: factory('smaller', [], () => function smaller (a, b) {
+ return a < b
+ }),
+
+ createSmallerEq: factory('smallerEq', [], () => function smallerEq (a, b) {
+ return a <= b
+ }),
+
+ createLarger: factory('larger', [], () => function larger (a, b) {
+ return a > b
+ }),
+
+ createLargerEq: factory('largerEq', [], () => function largerEq (a, b) {
+ return a >= b
+ }),
+
+ createCompare: factory('compare', [], () => function compare (a, b) {
+ return a > b ? 1 : a < b ? -1 : 0
+ })
+}
+const evaluateCustom = create(allWithCustomFunctions).evaluate
+evaluateAndLog(evaluateCustom, '2 < 10') // true
+evaluateAndLog(evaluateCustom, '"2" < "10"') // false
+evaluateAndLog(evaluateCustom, '"a" == "b"') // false
+evaluateAndLog(evaluateCustom, '"a" == "a"') // true
+
+// helper function to evaluate an expression and print the results
+function evaluateAndLog (evaluate, expression) {
+ try {
+ console.log(expression, evaluate(expression))
+ } catch (err) {
+ console.error(expression, err.toString())
+ }
+}
diff --git a/examples/advanced/custom_relational_functions.js.md b/examples/advanced/custom_relational_functions.js.md
new file mode 100644
index 000000000..d82d46882
--- /dev/null
+++ b/examples/advanced/custom_relational_functions.js.md
@@ -0,0 +1,76 @@
+---
+layout: default
+---
+
+# Custom relational functions
+
+File: [custom_relational_functions.js](custom_relational_functions.js) (click for a live demo)
+
+```js
+const { create, all, factory } = require('../..')
+
+// First let's see what the default behavior is:
+// strings are compared by their numerical value
+console.log('default (compare string by their numerical value)')
+const { evaluate } = create(all)
+evaluateAndLog(evaluate, '2 < 10') // true
+evaluateAndLog(evaluate, '"2" < "10"') // true
+evaluateAndLog(evaluate, '"a" == "b"') // Error: Cannot convert "a" to a number
+evaluateAndLog(evaluate, '"a" == "a"') // Error: Cannot convert "a" to a number
+console.log('')
+
+// Suppose we want different behavior for string comparisons. To achieve
+// this we can replace the factory functions for all relational functions
+// with our own. In this simple example we use the JavaScript implementation.
+console.log('custom (compare strings lexically)')
+
+const allWithCustomFunctions = {
+ ...all,
+
+ createEqual: factory('equal', [], () => function equal (a, b) {
+ return a === b
+ }),
+
+ createUnequal: factory('unequal', [], () => function unequal (a, b) {
+ return a !== b
+ }),
+
+ createSmaller: factory('smaller', [], () => function smaller (a, b) {
+ return a < b
+ }),
+
+ createSmallerEq: factory('smallerEq', [], () => function smallerEq (a, b) {
+ return a <= b
+ }),
+
+ createLarger: factory('larger', [], () => function larger (a, b) {
+ return a > b
+ }),
+
+ createLargerEq: factory('largerEq', [], () => function largerEq (a, b) {
+ return a >= b
+ }),
+
+ createCompare: factory('compare', [], () => function compare (a, b) {
+ return a > b ? 1 : a < b ? -1 : 0
+ })
+}
+const evaluateCustom = create(allWithCustomFunctions).evaluate
+evaluateAndLog(evaluateCustom, '2 < 10') // true
+evaluateAndLog(evaluateCustom, '"2" < "10"') // false
+evaluateAndLog(evaluateCustom, '"a" == "b"') // false
+evaluateAndLog(evaluateCustom, '"a" == "a"') // true
+
+// helper function to evaluate an expression and print the results
+function evaluateAndLog (evaluate, expression) {
+ try {
+ console.log(expression, evaluate(expression))
+ } catch (err) {
+ console.error(expression, err.toString())
+ }
+}
+
+```
+
+
+
diff --git a/examples/advanced/more_secure_eval.js b/examples/advanced/more_secure_eval.js
index 1d89a85ec..46329ba7c 100644
--- a/examples/advanced/more_secure_eval.js
+++ b/examples/advanced/more_secure_eval.js
@@ -24,12 +24,12 @@ const math = create(all)
const limitedEvaluate = math.evaluate
math.import({
- 'import': function () { throw new Error('Function import is disabled') },
- 'createUnit': function () { throw new Error('Function createUnit is disabled') },
- 'evaluate': function () { throw new Error('Function evaluate is disabled') },
- 'parse': function () { throw new Error('Function parse is disabled') },
- 'simplify': function () { throw new Error('Function simplify is disabled') },
- 'derivative': function () { throw new Error('Function derivative is disabled') }
+ import: function () { throw new Error('Function import is disabled') },
+ createUnit: function () { throw new Error('Function createUnit is disabled') },
+ evaluate: function () { throw new Error('Function evaluate is disabled') },
+ parse: function () { throw new Error('Function parse is disabled') },
+ simplify: function () { throw new Error('Function simplify is disabled') },
+ derivative: function () { throw new Error('Function derivative is disabled') }
}, { override: true })
console.log(limitedEvaluate('sqrt(16)')) // Ok, 4
diff --git a/examples/advanced/more_secure_eval.js.md b/examples/advanced/more_secure_eval.js.md
index f96557ce0..faa1b9338 100644
--- a/examples/advanced/more_secure_eval.js.md
+++ b/examples/advanced/more_secure_eval.js.md
@@ -33,12 +33,12 @@ const math = create(all)
const limitedEvaluate = math.evaluate
math.import({
- 'import': function () { throw new Error('Function import is disabled') },
- 'createUnit': function () { throw new Error('Function createUnit is disabled') },
- 'evaluate': function () { throw new Error('Function evaluate is disabled') },
- 'parse': function () { throw new Error('Function parse is disabled') },
- 'simplify': function () { throw new Error('Function simplify is disabled') },
- 'derivative': function () { throw new Error('Function derivative is disabled') }
+ import: function () { throw new Error('Function import is disabled') },
+ createUnit: function () { throw new Error('Function createUnit is disabled') },
+ evaluate: function () { throw new Error('Function evaluate is disabled') },
+ parse: function () { throw new Error('Function parse is disabled') },
+ simplify: function () { throw new Error('Function simplify is disabled') },
+ derivative: function () { throw new Error('Function derivative is disabled') }
}, { override: true })
console.log(limitedEvaluate('sqrt(16)')) // Ok, 4
diff --git a/examples/advanced/web_server/index.md b/examples/advanced/web_server/index.md
index 2e9600456..5488bec5f 100644
--- a/examples/advanced/web_server/index.md
+++ b/examples/advanced/web_server/index.md
@@ -15,7 +15,7 @@ const math = create(all)
function noImport () {
throw new Error('function import is disabled.')
}
-math.import({ 'import': noImport }, { override: true })
+math.import({ import: noImport }, { override: true })
/**
* Evaluate an expression
diff --git a/examples/advanced/web_server/math_worker.js b/examples/advanced/web_server/math_worker.js
index 878d2ac3f..2c7c0baa4 100644
--- a/examples/advanced/web_server/math_worker.js
+++ b/examples/advanced/web_server/math_worker.js
@@ -6,7 +6,7 @@ const math = create(all)
function noImport () {
throw new Error('function import is disabled.')
}
-math.import({ 'import': noImport }, { override: true })
+math.import({ import: noImport }, { override: true })
/**
* Evaluate an expression
diff --git a/examples/browser/angle_configuration.html b/examples/browser/angle_configuration.html
index 044163290..2e49734a6 100644
--- a/examples/browser/angle_configuration.html
+++ b/examples/browser/angle_configuration.html
@@ -15,7 +15,7 @@
}
-
+
diff --git a/examples/browser/angle_configuration.html.md b/examples/browser/angle_configuration.html.md
index 85637ce75..ffb5c9248 100644
--- a/examples/browser/angle_configuration.html.md
+++ b/examples/browser/angle_configuration.html.md
@@ -24,7 +24,7 @@ File: [angle_configuration.html](angle_configuration.html) (click for a live dem
}
-
+
diff --git a/examples/browser/basic_usage.html b/examples/browser/basic_usage.html
index 1e57eb9a1..1b161980c 100644
--- a/examples/browser/basic_usage.html
+++ b/examples/browser/basic_usage.html
@@ -3,7 +3,7 @@
|