From 0222ff7aa9f85559a6f0a2daa6aa0c292849937a Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 25 Oct 2023 14:25:05 +0200 Subject: [PATCH] fix: function `clone` not throwing an error in case of an unsupported type like a function --- src/utils/object.js | 9 ++++++--- test/unit-tests/utils/object.test.js | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/utils/object.js b/src/utils/object.js index c9fb764b99..c5dbc692e2 100644 --- a/src/utils/object.js +++ b/src/utils/object.js @@ -1,4 +1,4 @@ -import { isBigNumber } from './is.js' +import { isBigNumber, isObject } from './is.js' /** * Clone an object @@ -34,10 +34,13 @@ export function clone (x) { if (x instanceof Date) return new Date(x.valueOf()) if (isBigNumber(x)) return x // bignumbers are immutable - if (x instanceof RegExp) throw new TypeError('Cannot clone ' + x) // TODO: clone a RegExp // object - return mapObject(x, clone) + if (isObject(x)) { + return mapObject(x, clone) + } + + throw new TypeError(`Cannot clone: unknown type of value (value: ${x})`) } /** diff --git a/test/unit-tests/utils/object.test.js b/test/unit-tests/utils/object.test.js index 7d43afb91c..1bf1422ace 100644 --- a/test/unit-tests/utils/object.test.js +++ b/test/unit-tests/utils/object.test.js @@ -79,7 +79,8 @@ describe('object', function () { }) it('should throw an error in case of an unsupported type', function () { - assert.throws(function () { clone(/a regexp/) }, /Cannot clone/) + assert.throws(function () { clone(/a regexp/) }, /Cannot clone: unknown type of value \(value: \/a regexp\/\)/) + assert.throws(function () { clone(() => 42) }, /Cannot clone: unknown type of value \(value: \(\) => 42\)/) }) })