mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
* feat: toBest tests * feat: toBest function initial * fix: tests and function changes * fix: dependencies * new way to handle * updated tests * test updated * tests fixed * cleaned useless tests * cleaned useless condition * fix: missing comments * fix: moved tests * other fix and clean * change test to work with npx mocha "path" -g "toBest" * deleted check large and small value * deleted precision test and fix options to bestprefix * fix: moved toBest tests in correct file * fix: math create * fix: aligned files pre-mr * fix: toBest docs test * fix: lint issues * fix: comments * fix: added a helper function to simplify the test readability * fix: unit.md and units.md docs updated * fix: test on units.length * added post process denormalize for tobest function to string returned value * removed this.clone into format * feat improve incompatible units test * fix assign prefixes * fixed examples * changed order of params of assertUnit * fixed comments and added toBest in knowProblems at doc.test * refactor: update toBest functionality and adjust related tests for accuracy * refactor: remove unnecessary offset initialization in prefix application --------- Co-authored-by: Luca Quercetti <l.quercetti@wecodeyou.it> Co-authored-by: Elia Alesiani <elia.alesiani@gmail.com> Co-authored-by: Jos de Jong <wjosdejong@gmail.com> Co-authored-by: lucaQ <lucaquercetti@gmail.com> Co-authored-by: EliaAlesiani <105418798+EliaAlesiani@users.noreply.github.com>
80 lines
2.9 KiB
JavaScript
80 lines
2.9 KiB
JavaScript
import math from '../../../../src/defaultInstance.js'
|
|
import assert from 'assert'
|
|
|
|
const Unit = math.create().Unit
|
|
|
|
function assertUnit (actualUnit, expectedValue, expectedPrefix, expectedName) {
|
|
assert.equal(actualUnit.value, expectedValue)
|
|
assert.equal(actualUnit.units[0].prefix.name, expectedPrefix)
|
|
assert.equal(actualUnit.units[0].unit.name, expectedName)
|
|
assert.equal(actualUnit.units.length, 1)
|
|
}
|
|
|
|
describe('toBest', function () {
|
|
it('should return the best unit without any parameters', function () {
|
|
assertUnit(new Unit(2 / 3, 'cm').toBest(), 0.006666666666666666, 'c', 'm')
|
|
})
|
|
|
|
it('should format a unit without any value', function () {
|
|
assertUnit(new Unit(null, 'cm').toBest(), null, 'c', 'm')
|
|
})
|
|
|
|
it('should return the best unit with only given unit array - valorized and empty', function () {
|
|
const unit1 = new Unit(10, 'm')
|
|
assertUnit(unit1.toBest(['km', 'mm', 'cm']), 10, 'm', 'm')
|
|
|
|
const unit2 = new Unit(5, 'm')
|
|
assertUnit(unit2.toBest(['cm', 'mm']), 5, 'c', 'm')
|
|
})
|
|
|
|
it('should return the best unit with valueless unit as parameter', function () {
|
|
assertUnit(new Unit(1000, 'cm').toBest([new Unit(null, 'km')]), 10, 'k', 'm')
|
|
})
|
|
|
|
it('should return the best unit with given array and offset', function () {
|
|
assertUnit(new Unit(10, 'm').toBest(['mm', 'km'], { offset: 1.5 }), 10, 'm', 'm')
|
|
})
|
|
|
|
it('should handle negative values correctly', function () {
|
|
assertUnit(new Unit(-1000, 'cm').toBest(), -10, 'c', 'm')
|
|
})
|
|
|
|
it('should handle zero values correctly', function () {
|
|
assertUnit(new Unit(0, 'km').toBest(), 0, 'k', 'm')
|
|
assertUnit(new Unit(0, 'cm').toBest(['km', 'm', 'cm', 'mm']), 0, 'k', 'm')
|
|
})
|
|
|
|
it('should throw error for first parameter not being an array', function () {
|
|
assert.throws(
|
|
() => new Unit(2 / 3, 'cm').toBest(new Unit(null, 'cm')),
|
|
/Invalid unit type. Expected string or Unit./
|
|
)
|
|
})
|
|
|
|
it('should return the correct string representation', function () {
|
|
assert.equal(new Unit(2 / 3, 'cm').toBest().toString(), '0.6666666666666666 cm')
|
|
assert.equal(new Unit(5, 'm').toBest(['cm', 'mm']).toString(), '500 cm')
|
|
assert.equal(new Unit(1000, 'cm').toBest(['m', 'km']).toString(), '10 m')
|
|
})
|
|
|
|
const incompatibleUnits = [
|
|
['length to mass', 'm', ['kg']],
|
|
['length to mass times length', 'm', ['kg m']],
|
|
['force to power', 'N', ['W']],
|
|
['mass to temperature', 'kg', ['degC']],
|
|
['amount of substance to angle', 'mol', ['rad']],
|
|
['area to volume', 'm^2', ['m^3']],
|
|
['frequency to pressure', 'Hz', ['Pa']],
|
|
['energy to luminous intensity', 'J', ['cd']]
|
|
]
|
|
|
|
incompatibleUnits.forEach(([description, sourceUnit, targetUnits]) => {
|
|
it(`should throw error when converting ${description}`, function () {
|
|
assert.throws(
|
|
() => new Unit(1, sourceUnit).toBest(targetUnits),
|
|
/Invalid unit type. Expected compatible string or Unit./
|
|
)
|
|
})
|
|
})
|
|
})
|