Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/type/unit/Unit.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isComplex, isUnit, typeOf } from '../../utils/is.js'
import { isComplex, isMatrix, isUnit, typeOf } from '../../utils/is.js'
import { factory } from '../../utils/factory.js'
import { memoize } from '../../utils/function.js'
import { endsWith } from '../../utils/string.js'
Expand Down Expand Up @@ -1320,6 +1320,14 @@ export const createUnitClass = /* #__PURE__ */ factory(name, dependencies, ({
* @return {Array} An array of units.
*/
Unit.prototype.splitUnit = function (parts) {
// When invoked as a method in the expression parser
// (e.g. `(1 m).splitUnit([ft, in])`), `parts` is passed as a Matrix rather
// than a plain Array. Normalize it to an Array so the iteration below works
// in both cases.
if (isMatrix(parts)) {
parts = parts.toArray()
}

let x = this.clone()
const ret = []
for (let i = 0; i < parts.length; i++) {
Expand Down
9 changes: 9 additions & 0 deletions test/unit-tests/type/unit/function/splitUnit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ describe('splitUnit', function () {

assert.strictEqual(math.evaluate('splitUnit(1 m, [ft, in])').toString(), '3 ft,3.3700787401574765 in')
})

it('should split a unit when parts are passed as a Matrix', function () {
// when splitUnit is called as a method in the expression parser, the parts
// are passed as a Matrix rather than an Array (see #3644)
assert.strictEqual(splitUnit(new Unit(1, 'm'), math.matrix(['ft', 'in'])).toString(), '3 ft,3.3700787401574765 in')

assert.strictEqual(math.evaluate('(1 m).splitUnit([ft, in])').toString(), '3 ft,3.3700787401574765 in')
assert.strictEqual(math.evaluate('(1 m).splitUnit(["ft", "in"])').toString(), '3 ft,3.3700787401574765 in')
})
})