deep-clone-fn
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,2 +0,1 @@ | ||
/* eslint-disable @typescript-eslint/no-base-to-string */ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
@@ -7,5 +6,4 @@ /* eslint-disable @typescript-eslint/ban-types */ | ||
const copyProperty = (to, from, property, ignoreNonConfigurable) => { | ||
// `Function#length` should reflect the parameters of `to` not `from` since we keep its body. | ||
// `Function#prototype` is non-writable and non-configurable so can never be modified. | ||
if (property === 'length' || property === 'prototype') { | ||
if (property === 'prototype') { | ||
return; | ||
@@ -22,6 +20,6 @@ } | ||
} | ||
Object.defineProperty(to, property, { | ||
...fromDescriptor, | ||
value: clone(fromDescriptor?.value), | ||
}); | ||
Object.defineProperty(to, property, fromDescriptor); | ||
if (fromDescriptor.writable) { | ||
to[property] = clone(from[property]); | ||
} | ||
}; | ||
@@ -46,3 +44,3 @@ // `Object.defineProperty()` throws if the property exists, is not configurable and either: | ||
}; | ||
const wrappedToString = (withName, fromBody) => `/* Wrapped ${withName}*/\n${fromBody}`; | ||
const clonedToString = (fromBody) => fromBody; | ||
const toStringDescriptor = Object.getOwnPropertyDescriptor(Function.prototype, 'toString'); | ||
@@ -53,6 +51,6 @@ const toStringName = Object.getOwnPropertyDescriptor(Function.prototype.toString, 'name'); | ||
// Calling `from.toString()` early also allows caching it in case `to.toString()` is called several times. | ||
const changeToString = (to, from, name) => { | ||
const withName = name === '' ? '' : `with ${name.trim()}() `; | ||
const newToString = wrappedToString.bind(null, withName, from.toString()); | ||
// Ensure `to.toString.toString` is non-enumerable and has the same `same` | ||
const changeToString = (to, from) => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
const newToString = clonedToString.bind(null, from.toString()); | ||
// Ensure `to.toString.toString` is non-enumerable and has the same `name` | ||
Object.defineProperty(newToString, 'name', toStringName); | ||
@@ -66,3 +64,2 @@ Object.defineProperty(to, 'toString', { | ||
const fnToClone = fn; | ||
const { name } = fnToClone; | ||
function clonedFn(...args) { | ||
@@ -76,5 +73,5 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call | ||
changePrototype(clonedFn, fnToClone); | ||
changeToString(clonedFn, fnToClone, name); | ||
changeToString(clonedFn, fnToClone); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return clonedFn; | ||
} |
{ | ||
"name": "deep-clone-fn", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Deep clone a function.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
6462
82