diff --git a/packages/hooks/src/hooks.ts b/packages/hooks/src/hooks.ts index b7653be..1a02fa4 100644 --- a/packages/hooks/src/hooks.ts +++ b/packages/hooks/src/hooks.ts @@ -2,7 +2,7 @@ import { compose, Middleware } from './compose'; import { HookContext, setManager, HookContextData, HookOptions, convertOptions, setMiddleware } from './base'; -import { copyProperties } from './utils'; +import { copyFnProperties, copyProperties } from './utils'; export function getOriginal (fn: any): any { return typeof fn.original === 'function' ? getOriginal(fn.original) : fn; @@ -51,6 +51,7 @@ export function functionHooks (fn: F, managerOrMiddleware: HookOptions) { return compose(hookChain).call(this, context); }; + copyFnProperties(wrapper, fn); copyProperties(wrapper, fn); setManager(wrapper, manager); diff --git a/packages/hooks/src/utils.ts b/packages/hooks/src/utils.ts index 936cac9..5e3b58c 100644 --- a/packages/hooks/src/utils.ts +++ b/packages/hooks/src/utils.ts @@ -45,3 +45,19 @@ export function copyProperties (target: F, ...originals: any[]) { return target; } + +export function copyFnProperties (target: F, original : any) { + const internalProps = ['name', 'length']; + + try { + for (const prop of internalProps) { + const value = original[prop]; + + Object.defineProperty(target, prop, { value }); + } + } catch (e) { + // Avoid IE error + } + + return target; +} diff --git a/packages/hooks/test/function.test.ts b/packages/hooks/test/function.test.ts index f6f5c4b..ef3169c 100644 --- a/packages/hooks/test/function.test.ts +++ b/packages/hooks/test/function.test.ts @@ -28,6 +28,13 @@ describe('functionHooks', () => { assert.ok(getManager(fn) !== null); }); + it('conserve name and length properties', () => { + const fn = hooks(hello, []); + + assert.strictEqual(fn.length, hello.length); + assert.strictEqual(fn.name, hello.name); + }); + it('throws an error with non function', () => { assert.throws(() => functionHooks({}, middleware([]))); })