diff --git a/index.d.ts b/index.d.ts index b4047d5..dfd25c2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,6 @@ declare const mimicFn: { /** - Make a function mimic another one. It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set. + Make a function mimic another one. It will copy over the properties `name`, `displayName`, and any custom properties you may have set. The `length` property won't be copied. @param to - Mimicking function. @param from - Function to mimic. diff --git a/index.js b/index.js index 1a59705..6613793 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,11 @@ 'use strict'; +const shouldCopyProperty = property => property !== 'length'; + const mimicFn = (to, from) => { - for (const prop of Reflect.ownKeys(from)) { + const props = Reflect.ownKeys(from).filter(shouldCopyProperty); + + for (const prop of props) { Object.defineProperty(to, prop, Object.getOwnPropertyDescriptor(from, prop)); } diff --git a/readme.md b/readme.md index d7ae227..94c56ca 100644 --- a/readme.md +++ b/readme.md @@ -39,7 +39,7 @@ console.log(wrapper.unicorn); ## API -It will copy over the properties `name`, `length`, `displayName`, and any custom properties you may have set. +It will copy over the properties `name`, `displayName`, and any custom properties you may have set. The `length` property won't be copied. ### mimicFn(to, from) @@ -61,7 +61,7 @@ Function to mimic. ## Related - [rename-fn](https://github.com/sindresorhus/rename-fn) - Rename a function -- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name, length and other properties +- [keep-func-props](https://github.com/ehmicky/keep-func-props) - Wrap a function without changing its name and other properties --- diff --git a/test.js b/test.js index 991f20f..2ad4520 100644 --- a/test.js +++ b/test.js @@ -15,7 +15,7 @@ test('main', t => { t.is(mimickFn(wrapper, foo), wrapper); t.is(wrapper.name, 'foo'); - t.is(wrapper.length, 1); + t.is(wrapper.length, 0); t.is(wrapper.unicorn, '🦄'); t.is(wrapper[symbol], '✨'); });