Skip to content

Commit

Permalink
fix: function clone not throwing an error in case of an unsupported…
Browse files Browse the repository at this point in the history
… type like a function
  • Loading branch information
josdejong committed Oct 25, 2023
1 parent 8f8e506 commit 0222ff7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/utils/object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBigNumber } from './is.js'
import { isBigNumber, isObject } from './is.js'

/**
* Clone an object
Expand Down Expand Up @@ -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})`)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/unit-tests/utils/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\)/)
})
})

Expand Down

0 comments on commit 0222ff7

Please sign in to comment.