Skip to content

Commit

Permalink
fix: #3323 support functions in function clone
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Nov 20, 2024
1 parent 7c9c65a commit e00d367
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,6 @@ Brooks Smith <42363318+smith120bh@users.noreply.github.com>
Jmar L. Pineda <63294460+Galm007@users.noreply.github.com>
gauravchawhan <117282267+gauravchawhan@users.noreply.github.com>
Neeraj Kumawat <42885320+nkumawat34@users.noreply.github.com>
Emmanuel Ferdman <emmanuelferdman@gmail.com>

# Generated by tools/update-authors.js
5 changes: 5 additions & 0 deletions src/utils/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export function clone (x) {
return mapObject(x, clone)
}

if (type === 'function') {
// we assume that the function is immutable
return x
}

throw new TypeError(`Cannot clone: unknown type of value (value: ${x})`)
}

Expand Down
7 changes: 7 additions & 0 deletions test/unit-tests/function/utils/clone.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ describe('clone', function () {
assert.strictEqual(b.valueOf()[2].re, 2)
})

it('should clone a function', function () {
const f = () => 42
const a = math.matrix([f])
const b = math.clone(a)
assert.strictEqual(b.get([0]), f)
})

it('should LaTeX clone', function () {
const expression = math.parse('clone(1)')
assert.strictEqual(expression.toTex(), '\\mathrm{clone}\\left(1\\right)')
Expand Down
1 change: 0 additions & 1 deletion test/unit-tests/utils/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ describe('object', function () {

it('should throw an error in case of an unsupported type', function () {
assert.throws(function () { clone(/a regexp/) }, /Cannot clone: unknown type of value/)
assert.throws(function () { clone(() => 42) }, /Cannot clone: unknown type of value/)
})
})

Expand Down

0 comments on commit e00d367

Please sign in to comment.