Skip to content

Commit

Permalink
Fix #1808: toNumber() not working on a unitless unit
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Apr 8, 2020
1 parent f6a8a23 commit 5cd1849
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/type/unit/Unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
other = this.clone()
}

if (other._isDerived()) {
if (other._isDerived() || other.units.length === 0) {
return other._denormalize(other.value)
} else {
return other._denormalize(other.value, other.units[0].prefix.value)
Expand Down
10 changes: 10 additions & 0 deletions test/unit-tests/type/unit/Unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ describe('Unit', function () {
const u = new Unit(math.fraction(5), 'cm')
assert.strictEqual(u.toNumber('mm'), 50)
})

it('should convert a unit with value only to a number', function () {
const u = Unit.parse('5', { allowNoUnits: true })
assert.strictEqual(u.toNumber(), 5)
})
})

describe('toNumeric', function () {
Expand Down Expand Up @@ -349,6 +354,11 @@ describe('Unit', function () {
assert.strictEqual(u4.fixPrefix, true)
})

it('should convert a unitless quantity', function () {
const u = Unit.parse('5', { allowNoUnits: true })
assert.strictEqual(u.toNumeric(), 5)
})

it('should convert a binary prefixes (1)', function () {
const u1 = new Unit(1, 'Kib')
assert.strictEqual(u1.value, 1024)
Expand Down

0 comments on commit 5cd1849

Please sign in to comment.