diff --git a/AUTHORS b/AUTHORS index 725e839e75..6adbc67a81 100644 --- a/AUTHORS +++ b/AUTHORS @@ -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 # Generated by tools/update-authors.js diff --git a/src/utils/object.js b/src/utils/object.js index ffb374022b..fb62f6711d 100644 --- a/src/utils/object.js +++ b/src/utils/object.js @@ -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})`) } diff --git a/test/unit-tests/function/utils/clone.test.js b/test/unit-tests/function/utils/clone.test.js index 6b82d1ec54..baead46bb1 100644 --- a/test/unit-tests/function/utils/clone.test.js +++ b/test/unit-tests/function/utils/clone.test.js @@ -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)') diff --git a/test/unit-tests/utils/object.test.js b/test/unit-tests/utils/object.test.js index 103200ce6d..5f84ae8e3b 100644 --- a/test/unit-tests/utils/object.test.js +++ b/test/unit-tests/utils/object.test.js @@ -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/) }) })