Jos de Jong 6f00715754
Specify import require paths (continuation of #1941) (#1962)
* Add `.js` extension to source file imports

* Specify package `exports` in `package.json`

Specify package type as `commonjs` (It's good to be specific)

* Move all compiled scripts into `lib` directory

Remove ./number.js (You can use the compiled ones in `./lib/*`)

Tell node that the `esm` directory is type `module` and enable tree shaking.

Remove unused files from packages `files` property

* Allow importing of package.json

* Make library ESM first

* - Fix merge conflicts
- Refactor `bundleAny` into `defaultInstance.js` and `browserBundle.cjs`
- Refactor unit tests to be able to run with plain nodejs (no transpiling)
- Fix browser examples

* Fix browser and browserstack tests

* Fix running unit tests on Node 10 (which has no support for modules)

* Fix node.js examples (those are still commonjs)

* Remove the need for `browserBundle.cjs`

* Generate minified bundle only

* [Security] Bump node-fetch from 2.6.0 to 2.6.1 (#1963)

Bumps [node-fetch](https://github.com/bitinn/node-fetch) from 2.6.0 to 2.6.1. **This update includes a security fix.**
- [Release notes](https://github.com/bitinn/node-fetch/releases)
- [Changelog](https://github.com/node-fetch/node-fetch/blob/master/docs/CHANGELOG.md)
- [Commits](https://github.com/bitinn/node-fetch/compare/v2.6.0...v2.6.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

* Cleanup console.log

* Add integration tests to test the entry points (commonjs/esm, full/number only)

* Create backward compatibility error messages in the files moved/removed since v8

* Describe breaking changes in HISTORY.md

* Bump karma from 5.2.1 to 5.2.2 (#1965)

Bumps [karma](https://github.com/karma-runner/karma) from 5.2.1 to 5.2.2.
- [Release notes](https://github.com/karma-runner/karma/releases)
- [Changelog](https://github.com/karma-runner/karma/blob/master/CHANGELOG.md)
- [Commits](https://github.com/karma-runner/karma/compare/v5.2.1...v5.2.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>

Co-authored-by: Lee Langley-Rees <lee@greenimp.co.uk>
Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-09-20 18:01:29 +02:00

85 lines
4.7 KiB
JavaScript

import assert from 'assert'
import approx from '../../../../tools/approx.js'
import math from '../../../../src/defaultInstance.js'
const matrix = math.matrix
const Unit = math.Unit
const unit = math.unit
describe('to', function () {
it('should perform the given unit conversion', function () {
const a = math.unit('500 cm'); a.fixPrefix = true
approx.deepEqual(math.to(unit('5m'), unit('cm')), a)
const b = math.unit('1 foot'); b.fixPrefix = true
approx.deepEqual(math.to(unit('12 inch'), unit('foot')), b)
const c = math.unit('1 inch'); c.fixPrefix = true
approx.deepEqual(math.to(unit('2.54 cm'), unit('inch')), c)
const d = math.unit('68 fahrenheit'); d.fixPrefix = true
approx.deepEqual(math.to(unit('20 celsius'), unit('fahrenheit')), d)
const e = math.unit('0.002 m3'); e.fixPrefix = true
approx.deepEqual(math.to(unit('2 litre'), unit('m3')), e)
})
describe('Array', function () {
it('should perform the given unit conversion, array - scalar', function () {
approx.deepEqual(math.to([unit('1cm'), unit('2 inch'), unit('2km')], unit('foot')), [new Unit(0.032808, 'foot').to('foot'), new Unit(0.16667, 'foot').to('foot'), new Unit(6561.7, 'foot').to('foot')])
approx.deepEqual(math.to(unit('1cm'), [unit('cm'), unit('foot'), unit('km'), unit('m')]), [new Unit(1, 'cm').to('cm'), new Unit(1, 'cm').to('foot'), new Unit(1, 'cm').to('km'), new Unit(1, 'cm').to('m')])
})
it('should perform the given unit conversion, array - array', function () {
approx.deepEqual(math.to([[unit('1cm'), unit('2 inch')], [unit('2km'), unit('1 foot')]], [[unit('foot'), unit('foot')], [unit('cm'), unit('foot')]]), [[unit('1cm').to('foot'), unit('2 inch').to('foot')], [unit('2km').to('cm'), unit('1 foot').to('foot')]])
})
it('should perform the given unit conversion, array - dense matrix', function () {
approx.deepEqual(math.to([[unit('1cm'), unit('2 inch')], [unit('2km'), unit('1 foot')]], matrix([[unit('foot'), unit('foot')], [unit('cm'), unit('foot')]])), matrix([[unit('1cm').to('foot'), unit('2 inch').to('foot')], [unit('2km').to('cm'), unit('1 foot').to('foot')]]))
})
})
describe('DenseMatrix', function () {
it('should perform the given unit conversion, dense matrix - scalar', function () {
approx.deepEqual(math.to(matrix([unit('1cm'), unit('2 inch'), unit('2km')]), unit('foot')), matrix([new Unit(0.032808, 'foot').to('foot'), new Unit(0.16667, 'foot').to('foot'), new Unit(6561.7, 'foot').to('foot')]))
approx.deepEqual(math.to(unit('1cm'), matrix([unit('cm'), unit('foot'), unit('km'), unit('m')])), matrix([new Unit(1, 'cm').to('cm'), new Unit(1, 'cm').to('foot'), new Unit(1, 'cm').to('km'), new Unit(1, 'cm').to('m')]))
})
it('should perform the given unit conversion, dense matrix - array', function () {
approx.deepEqual(math.to(matrix([[unit('1cm'), unit('2 inch')], [unit('2km'), unit('1 foot')]]), [[unit('foot'), unit('foot')], [unit('cm'), unit('foot')]]), matrix([[unit('1cm').to('foot'), unit('2 inch').to('foot')], [unit('2km').to('cm'), unit('1 foot').to('foot')]]))
})
it('should perform the given unit conversion, dense matrix - dense matrix', function () {
approx.deepEqual(math.to(matrix([[unit('1cm'), unit('2 inch')], [unit('2km'), unit('1 foot')]]), matrix([[unit('foot'), unit('foot')], [unit('cm'), unit('foot')]])), matrix([[unit('1cm').to('foot'), unit('2 inch').to('foot')], [unit('2km').to('cm'), unit('1 foot').to('foot')]]))
})
})
it('should throw an error if converting between incompatible units', function () {
assert.throws(function () { math.to(unit('20 kg'), unit('cm')) })
assert.throws(function () { math.to(unit('20 celsius'), unit('litre')) })
assert.throws(function () { math.to(unit('5 cm'), unit('2 m^2')) })
})
it('should throw an error if called with a wrong number of arguments', function () {
assert.throws(function () { math.to(unit('20 kg')) })
assert.throws(function () { math.to(unit('20 kg'), unit('m'), unit('cm')) })
})
it('should throw an error if called with a non-plain unit', function () {
assert.throws(function () { math.unit(5000, 'cm').to('5mm') })
})
it('should throw an error if called with a number', function () {
assert.throws(function () { math.to(5, unit('m')) }, TypeError)
assert.throws(function () { math.to(unit('5cm'), 2) }, /SyntaxError: "2" contains no units/)
})
it('should throw an error if called with a string', function () {
assert.throws(function () { math.to('5cm', unit('cm')) }, TypeError)
})
it('should LaTeX to', function () {
const expression = math.parse('to(2cm,m)')
assert.strictEqual(expression.toTex(), '\\left(2~\\mathrm{cm}\\rightarrow\\mathrm{m}\\right)')
})
})