Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e00d367

Browse files
committed
fix: josdejong#3323 support functions in function clone
1 parent 7c9c65a commit e00d367

4 files changed

Lines changed: 13 additions & 1 deletion

File tree

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,5 +255,6 @@ Brooks Smith <[email protected]>
255255
Jmar L. Pineda <[email protected]>
256256
gauravchawhan <[email protected]>
257257
Neeraj Kumawat <[email protected]>
258+
Emmanuel Ferdman <[email protected]>
258259

259260
# Generated by tools/update-authors.js

src/utils/object.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export function clone (x) {
4040
return mapObject(x, clone)
4141
}
4242

43+
if (type === 'function') {
44+
// we assume that the function is immutable
45+
return x
46+
}
47+
4348
throw new TypeError(`Cannot clone: unknown type of value (value: ${x})`)
4449
}
4550

test/unit-tests/function/utils/clone.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ describe('clone', function () {
8888
assert.strictEqual(b.valueOf()[2].re, 2)
8989
})
9090

91+
it('should clone a function', function () {
92+
const f = () => 42
93+
const a = math.matrix([f])
94+
const b = math.clone(a)
95+
assert.strictEqual(b.get([0]), f)
96+
})
97+
9198
it('should LaTeX clone', function () {
9299
const expression = math.parse('clone(1)')
93100
assert.strictEqual(expression.toTex(), '\\mathrm{clone}\\left(1\\right)')

test/unit-tests/utils/object.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ describe('object', function () {
8484

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

0 commit comments

Comments
 (0)