Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert CJS files to ESM #3204

Merged
merged 17 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module.exports = {
env: {
browser: true,
es2021: true,
node: true
node: true,
mocha: true
},
extends: ['eslint:recommended'],
parserOptions: {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: [18.x, 20.x]
node-version: [18.x, 20.x, 22.x]

steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/convert_fraction_to_bignumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

// Create an empty math.js instance, with only typed
// (every instance contains `import` and `config` also out of the box)
const { create, typedDependencies, all } = require('../..')
import { create, typedDependencies, all } from '../../lib/esm/index.js'
const math = create({
typedDependencies
})
Expand All @@ -30,7 +30,7 @@ math.config({ number: 'Fraction' })
// this conversion:
// - must be inserted in the conversions list before the conversion Fraction -> number
// - must be added to the conversions before loading functions into math.js
math.typed.conversions.unshift({
math.typed.addConversion({
from: 'Fraction',
to: 'BigNumber',
convert: function (fraction) {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/custom_argument_parsing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* will be invoked with unevaluated arguments, allowing the function
* to process the arguments in a customized way.
*/
const { create, all } = require('../..')
import { create, all } from '../../lib/esm/index.js'
const math = create(all)

/**
Expand Down
3 changes: 2 additions & 1 deletion examples/advanced/custom_datatype.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// This example demonstrates importing a custom data type,
// and extending an existing function (add) with support for this data type.

const { create, factory, all } = require('../..')
import { all, create, factory } from '../../lib/esm/index.js'

const math = create(all)

// factory function which defines a new data type CustomValue
Expand Down
4 changes: 2 additions & 2 deletions examples/advanced/custom_evaluate_using_factories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// we use the number only implementation in order to not pull in
// the `Unit` class for example. when using as library,
// use require('mathjs/number')
const { create, evaluateDependencies, factory } = require('../../lib/cjs/number.js')
// use import 'mathjs/number'
import { create, evaluateDependencies, factory } from '../../lib/esm/number.js'

// custom implementations of all functions you want to support
const add = (a, b) => a + b
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/custom_evaluate_using_import.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// we use the number only implementation in order to not pull in
// the `Unit` class for example. when using as library,
// use require('mathjs/number')
const { create, evaluateDependencies } = require('../../lib/cjs/number.js')
import { create, evaluateDependencies } from '../../lib/esm/number.js'

// custom implementations of all functions you want to support
const add = (a, b) => a + b
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
create,
fractionDependencies,
addDependencies,
create,
divideDependencies,
formatDependencies
formatDependencies,
fractionDependencies
} from '../../lib/esm/index.js'

const config = {
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/custom_relational_functions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { create, all, factory } = require('../..')
import { all, create, factory } from '../../lib/esm/index.js'

// First let's see what the default behavior is:
// strings are compared by their numerical value
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/custom_scope_objects.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { create, all } = require('../..')
import { all, create } from '../../lib/esm/index.js'

const math = create(all)

Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/expression_trees.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { parse, ConstantNode } = require('../..')
import { parse, ConstantNode } from '../../lib/esm/index.js'

// Filter an expression tree
console.log('Filter all symbol nodes "x" in the expression "x^2 + x/4 + 3*y"')
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/function_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* *transform* for the function. A transform is a function wrapping around a
* function to be transformed or completely replaces a function.
*/
const { create, all } = require('../..')
import { all, create } from '../../lib/esm/index.js'
const math = create(all)

// create a function
Expand Down
2 changes: 1 addition & 1 deletion examples/advanced/more_secure_eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// functionality, these functions can be disabled, as demonstrated in this
// example.

const { create, all } = require('../..')
import { all, create } from '../../lib/esm/index.js'
const math = create(all)

const limitedEvaluate = math.evaluate
Expand Down
3 changes: 1 addition & 2 deletions examples/advanced/use_bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// in mathjs. It's just a proof of concept, for full support you will
// have to defined more functions and define conversions from and to
// other data types.

const { create, all, factory } = require('../..')
import { all, create, factory } from '../../lib/esm/index.js'
const math = create(all)

// we can also add conversions here from number or string to BigInt
Expand Down
4 changes: 1 addition & 3 deletions examples/algebra.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
// math.js has support for symbolic computation (CAS). It can parse
// expressions in an expression tree and do algebraic operations like
// simplification and derivation on this tree.

// load math.js (using node.js)
const { simplify, parse, derivative } = require('..')
import { simplify, parse, derivative } from '../lib/esm/index.js'

// simplify an expression
console.log('simplify expressions')
Expand Down
55 changes: 35 additions & 20 deletions examples/basic_usage.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
// basic usage

// load math.js (using node.js)
const math = require('..')
import {
add,
atan2,
chain,
derivative,
e,
evaluate,
format,
log,
matrix,
multiply,
pi,
pow,
round,
sqrt,
subtract,
unit
} from '../lib/esm/index.js'

// functions and constants
console.log('functions and constants')
print(math.round(math.e, 3)) // 2.718
print(math.atan2(3, -3) / math.pi) // 0.75
print(math.log(10000, 10)) // 4
print(math.sqrt(-4)) // 2i
print(math.pow([[-1, 2], [3, 1]], 2)) // [[7, 0], [0, 7]]
print(math.derivative('x^2 + x', 'x')) // 2 * x + 1
print(round(e, 3)) // 2.718
print(atan2(3, -3) / pi) // 0.75
print(log(10000, 10)) // 4
print(sqrt(-4)) // 2i
print(pow([[-1, 2], [3, 1]], 2)) // [[7, 0], [0, 7]]
print(derivative('x^2 + x', 'x')) // 2 * x + 1
console.log()

// expressions
console.log('expressions')
print(math.evaluate('1.2 * (2 + 4.5)')) // 7.8
print(math.evaluate('12.7 cm to inch')) // 5 inch
print(math.evaluate('sin(45 deg) ^ 2')) // 0.5
print(math.evaluate('9 / 3 + 2i')) // 3 + 2i
print(math.evaluate('det([-1, 2; 3, 1])')) // -7
print(evaluate('1.2 * (2 + 4.5)')) // 7.8
print(evaluate('12.7 cm to inch')) // 5 inch
print(evaluate('sin(45 deg) ^ 2')) // 0.5
print(evaluate('9 / 3 + 2i')) // 3 + 2i
print(evaluate('det([-1, 2; 3, 1])')) // -7
console.log()

// chained operations
console.log('chained operations')
const a = math.chain(3)
const a = chain(3)
.add(4)
.multiply(2)
.done()
Expand All @@ -33,10 +48,10 @@ console.log()

// mixed use of different data types in functions
console.log('mixed use of data types')
print(math.add(4, [5, 6])) // number + Array, [9, 10]
print(math.multiply(math.unit('5 mm'), 3)) // Unit * number, 15 mm
print(math.subtract([2, 3, 4], 5)) // Array - number, [-3, -2, -1]
print(math.add(math.matrix([2, 3]), [4, 5])) // Matrix + Array, [6, 8]
print(add(4, [5, 6])) // number + Array, [9, 10]
print(multiply(unit('5 mm'), 3)) // Unit * number, 15 mm
print(subtract([2, 3, 4], 5)) // Array - number, [-3, -2, -1]
print(add(matrix([2, 3]), [4, 5])) // Matrix + Array, [6, 8]
console.log()

/**
Expand All @@ -45,5 +60,5 @@ console.log()
*/
function print (value) {
const precision = 14
console.log(math.format(value, precision))
console.log(format(value, precision))
}
5 changes: 1 addition & 4 deletions examples/bignumbers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/* eslint-disable no-loss-of-precision */

// BigNumbers

const { create, all } = require('..')
import { create, all } from '../lib/esm/index.js'

// configure the default type of numbers as BigNumbers
const config = {
Expand Down
26 changes: 12 additions & 14 deletions examples/chaining.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
// chaining

// load math.js (using node.js)
const math = require('..')
import { chain, format, index, pi } from '../lib/esm/index.js'

// create a chained operation using the function `chain(value)`
// end a chain using done(). Let's calculate (3 + 4) * 2
const a = math.chain(3)
const a = chain(3)
.add(4)
.multiply(2)
.done()
print(a) // 14

// Another example, calculate square(sin(pi / 4))
const b = math.chain(math.pi)
const b = chain(pi)
.divide(4)
.sin()
.square()
Expand All @@ -23,25 +21,25 @@ print(b) // 0.5
// these are demonstrated in the following examples

// toString will return a string representation of the chain's value
const chain = math.chain(2).divide(3)
const str = chain.toString()
const myChain = chain(2).divide(3)
const str = myChain.toString()
print(str) // "0.6666666666666666"

// a chain has a function .valueOf(), which returns the value hold by the chain.
// This allows using it in regular operations. The function valueOf() acts the
// same as function done().
print(chain.valueOf()) // 0.66666666666667
print(chain + 2) // 2.6666666666667
print(myChain.valueOf()) // 0.66666666666667
print(myChain + 2) // 2.6666666666667

// the function subset can be used to get or replace sub matrices
const array = [[1, 2], [3, 4]]
const v = math.chain(array)
.subset(math.index(1, 0))
const v = chain(array)
.subset(index(1, 0))
.done()
print(v) // 3

const m = math.chain(array)
.subset(math.index(0, 0), 8)
const m = chain(array)
.subset(index(0, 0), 8)
.multiply(3)
.done()
print(m) // [[24, 6], [9, 12]]
Expand All @@ -52,5 +50,5 @@ print(m) // [[24, 6], [9, 12]]
*/
function print (value) {
const precision = 14
console.log(math.format(value, precision))
console.log(format(value, precision))
}
4 changes: 1 addition & 3 deletions examples/complex_numbers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// complex numbers

// load js (using node.js)
const { complex, add, multiply, sin, sqrt, pi, equal, sort, format } = require('..')
import { complex, add, multiply, sin, sqrt, pi, equal, sort, format } from '../lib/esm/index.js'

// create a complex number with a numeric real and complex part
console.log('create and manipulate complex numbers')
Expand Down
Loading